How to select multiple rows in UITableView in edit mode?

馋奶兔 提交于 2020-11-30 11:00:53

问题


I want to ask, how can I forward to edit mode, where I can to select multiple rows, as in Messages app, when you click on top right button "Select", when you can choose multiple messages by tapping on circles.

Like this:

I searched a lot, really, but couldn't find anything. Can anyone help me? Some advices


回答1:


Set

tableView.allowsMultipleSelectionDuringEditing = true

Screenshot

Demo code

class TableviewController:UITableViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.allowsMultipleSelectionDuringEditing = true
        tableView.setEditing(true, animated: false)
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        cell.textLabel?.text = "\(indexPath.row)"
        return cell
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
}



回答2:


If you're using Storyboards do this:




回答3:


NSMutableArray *selected;

decleare it in you viewcontroller.h file..

selected =[[NSMutableArray alloc]init];
for (int i=0; i<[YOUR_ARRAY count]; i++) // Number of Rows count
        {
            [selected addObject:@"NO"];
        }

add same number of "NO" in selected array using above code for that you had to replace YOUR_ARRAY with your data array that you show in table.

    if(![[selected objectAtIndex:indexPath.row] isEqualToString:@"NO"])
{

    cell.accessoryType=UITableViewCellAccessoryCheckmark;

}

else
{
    cell.accessoryType=UITableViewCellAccessoryNone;
}

put above code in your -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];


if (cell.accessoryType == UITableViewCellAccessoryNone) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [selected replaceObjectAtIndex:path.row withObject:@"YES"];
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
    [selected replaceObjectAtIndex:path.row withObject:@"NO"];
}

}

also put this to work correctly..



来源:https://stackoverflow.com/questions/33970807/how-to-select-multiple-rows-in-uitableview-in-edit-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!