我需要使用复选框单元格的帮助。我目前将对象添加到表视图中。看起来没问题,直到我尝试构建和运行程序,但我无法选中该复选框。我目前正在使用一个表格视图,它显示项目运行时,每个项目都有一个复选框,这样我就可以有多个选择。
我是 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。如果您仅在选中某个单元格时设置样式,则其他不一定选中的单元格在您滚动时会带有复选标记。希望这能让您了解如何使用复选标记。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)