选中和取消选中相同的 UITableViewCell [英] Check and uncheck the same UITableViewCell

查看:57
本文介绍了选中和取消选中相同的 UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 tableView 中取消选中单元格.就像复选框一样,一旦我按下一个单元格,它就会保持选中状态,如果我再次按下同一个单元格,它就会保持未选中状态.可能吗?

I need to uncheck cell in my tableView. Like a checkbox, once i pressed a cell it stay selected, if i pressed again the same cell it stay unselected. Is it possible?

我发现的只是一些取消选择单元格的方法

All I found is some method for deselecting cells

[table deselectRowAtIndexPath:NSIndexPath animated:YES];

但是没用

更新:我不需要检查很多单元格,如果点击选定的单元格,我需要取消选中单元格,如果我点击其他(未选定的)单元格,选定的应该将状态更改为未选定(默认情况下它已经在单元格中工作)

upd: i don't need to check many cells, all i need to uncheck cell if a tap on selected cell, and if i tap on other(unselected) cell, selected should change state to unselected(it's already work by default in cells)

推荐答案

加载数据源的位置这样做

Where your datasource is load do this

arrSelected = [[NSMutableArray alloc] init];
for (int i = 0; i <arrSong.count; i++) {
    [arrSelected addObject:[NSNumber numberWithBool:NO]];
}

在 indexpath 的单元格中执行此操作

In your cell for indexpath do this

if ([[arrSelected objectAtIndex:indexPath.row] boolValue] == YES) {
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else{
    [cell setAccessoryType:UITableViewCellAccessoryNone];
}

要选择/取消选择在你的tableview中执行此操作:didselect

To select/deselect do thisin you tableview:didselect

    if ([[arrSelected objectAtIndex:indexPath.row] boolValue] == YES) {
        [arrSelected replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:NO]];
    }
    else{
        [arrSelected replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]];
    }

这篇关于选中和取消选中相同的 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆