好的,所以我正在慢慢地弄清楚。我还有一个问题要解决。我使用一个字符串,并说明如果字符串等于单元格文本,则在加载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,你可以下载它来查看整个项目:
发布于 2012-11-04 09:03:18
您需要:
if (self.selectedPath && [indexPath isEqual:self.selectedPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}细胞得到重用。如果您有条件地设置任何单元格属性,则必须始终具有'else‘部分才能重置该属性。
编辑:在cellForRowAtIndexPath:方法中进行上述更改后,在didSelectRowAtIndexPath:方法中执行以下操作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *oldSelection = self.selectedPath;
if (self.selectedPath) {
UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.selectedPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
self.selectedPath = nil;
}
if (oldSelection == nil || ![indexPath isEqual:oldSelection]) {
UITableViewCell* checkCell = [tableView cellForRowAtIndexPath:indexPath];
checkCell.accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath];
}并摆脱didDeselectRowAtIndexPath:方法。
当然,您还需要NSIndexPath *类型的selectedPath属性。
此代码允许您选择0或1行。
https://stackoverflow.com/questions/13215073
复制相似问题