Как выбрать несколько строк в UITableView в режиме редактирования?

Я хочу спросить, как я могу переслать в режим редактирования, где я могу выбрать несколько строк, как в приложении "Сообщения", когда вы нажимаете на верхнюю правую кнопку "Выбрать", когда вы можете выбрать несколько сообщений, нажав на круги.

такой:

enter image description here

Я искал много, но ничего не нашел. Кто-нибудь может мне помочь? Некоторые советы

3 ответов


Set

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

Если вы используете раскадровки этого:

enter image description here


NSMutableArray *selected;

decleare это в вас viewcontroller.H-файл..

selected =[[NSMutableArray alloc]init];
for (int i=0; i<[YOUR_ARRAY count]; i++) // Number of Rows count
        {
            [selected addObject:@"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"];
}

}

также поставьте это работать правильно..