我需要使用复选框单元格的帮助。我目前将该对象添加到 TableView 中。看起来没问题,直到我尝试构建和运行程序,但我无法选中该复选框。我目前正在使用一个表格 View ,它显示项目运行时,每个项目都有一个复选框,这样我就可以有多个选择。
我是 xcode 新手,这个问题已经困扰我一周了。我尝试了谷歌,但仍然没有成功。
非常感谢任何片段、答案或解释。
最佳答案
首先我们需要编辑这个方法:- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
。假设您生成了一个基于导航的应用程序,该方法应该已经存在,只是被注释掉了。我不知道您实现的确切细节,但您必须以某种方式跟踪 tableView 中每个单元格的复选框状态。例如,如果您有一个 BOOL 数组,则以下代码将起作用:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (checkboxArray[indexPath.row])
checkboxArray[indexPath.row] = NO;
else
checkboxArray[indexPath.row] = YES;
[self.tableView reloadData];
}
现在我们知道哪些单元格旁边需要有复选标记,下一步是修改单元格的显示方式。 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
处理每个单元格的绘制。在前面的示例的基础上,这就是显示复选框的方式:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (checkboxArray[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
cell.accessoryType = UITableViewCellAccessoryNone;
// Configure the cell.
return cell;
}
如果我们不调用 reloadData,复选标记将不会显示,直到它离开屏幕并重新出现。由于单元格的重用方式,您每次都需要显式设置accessoryType。如果您仅在选中某个单元格时设置样式,则其他不一定会选中的单元格在您滚动时会带有复选标记。希望这能让您大致了解如何使用复选标记。
关于iphone - TableView 中的复选框单元格 : User can't check it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1576658/