首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么UITableViewCellAccessoryCheckMark不走?

为什么UITableViewCellAccessoryCheckMark不走?
EN

Stack Overflow用户
提问于 2012-11-04 08:58:48
回答 2查看 744关注 0票数 0

好的,所以我正在慢慢地弄清楚。我还有一个问题要解决。我使用一个字符串,并说明如果字符串等于单元格文本,则在加载tableView时在其上放置一个复选标记。

下面是我的代码:

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if ([cell.textLabel.text isEqualToString:transferData]) {

    cell.accessoryType = UITableViewCellAccessoryCheckmark;

}

然后,我告诉它删除该复选标记,并在被选中时相应地添加复选标记:

代码语言:javascript
运行
复制
- (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一起加载的复选标记不会消失。为什么会这样呢?

EN

Stack Overflow用户

回答已采纳

发布于 2012-11-04 09:10:17

您正在犯一个常见的错误。

选择单元格时,直接设置复选标记的状态。您应该做的是在数据源中设置复选标记的状态,并让表单元格从数据源配置自身。

独占选中表视图的编辑示例

代码语言:javascript
运行
复制
- (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,你可以下载它来查看整个项目:

票数 3
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13215073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档