如何在编辑模式下在UITableView中选择多行?

时间:2015-11-28 11:30:59

标签: ios swift uitableview multipleselection

我想问一下,我怎样才能转发到编辑模式,我可以选择多行,例如在消息应用中,当您单击右上角按钮“选择”时,您可以通过点击圈子选择多个消息

像这样:

enter image description here

我搜索了很多,真的,但找不到任何东西。谁能帮我?一些建议

3 个答案:

答案 0 :(得分:41)

设置

tableView.allowsMultipleSelectionDuringEditing = true

截图

演示代码

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
    }
}

答案 1 :(得分:6)

如果您正在使用Storyboard,请执行以下操作:

enter image description here

答案 2 :(得分:2)

NSMutableArray *selected;
  

在viewcontroller.h文件中取消它..

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

使用上面的代码在所选数组中添加相同数量的“NO”,因为您必须将YOUR_ARRAY替换为您在表格中显示的数据数组。

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

    cell.accessoryType=UITableViewCellAccessoryCheckmark;

}

else
{
    cell.accessoryType=UITableViewCellAccessoryNone;
}
  

将上面的代码放在你的 - (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"];
}

}

也让它正常工作..