好的,所以我正在慢慢地弄清楚。我还有一个问题要解决。我使用一个字符串,并说明如果字符串等于单元格文本,则在加载tableView时在其上放置一个复选标记。
下面是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ([cell.textLabel.text isEqualToString:transferData]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}然后,我告诉它删除该复选标记,并在被选中时相应地添加复选标记:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
cell.accessoryType = UITableViewCellAccessoryNone;
UITableViewCell *cellCheck = [tableView
cellForRowAtIndexPath:indexPath];
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
transferData = cellCheck.textLabel.text;
NSLog(@"%@", transferData);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:indexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}除了第一次加载之外,一切都运行正常。由于某些原因,当我在另一个单元格上选择时,最初与tableView一起加载的复选标记不会消失。为什么会这样呢?
发布于 2012-11-04 09:10:17
您正在犯一个常见的错误。
选择单元格时,直接设置复选标记的状态。您应该做的是在数据源中设置复选标记的状态,并让表单元格从数据源配置自身。
独占选中表视图的编辑示例
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *changedIndexPaths = nil;
NSIndexPath *currentCheckedIndexPath = [self indexPathOfCurrentCheckedObject];
if (currentCheckedIndexPath && ![currentCheckedIndexPath isEqual:indexPath]) {
// There is currently a checked index path - unselect the data source and
// add it to the changed index array.
[[self.tableData objectAtIndex:currentCheckedIndexPath.row] setChecked:NO];
changedIndexPaths = @[indexPath, currentCheckedIndexPath];
} else{
changedIndexPaths = @[indexPath];
}
[[self.tableData objectAtIndex:indexPath.row] setChecked:YES];
[self.tableView reloadRowsAtIndexPaths:changedIndexPaths withRowAnimation:UITableViewRowAnimationNone];
}我有一个新的sample app,你可以下载它来查看整个项目:
https://stackoverflow.com/questions/13215073
复制相似问题