objective-c - 为什么 UITableViewCell 附件复选标记不会消失?

标签 objective-c ios xcode uitableview

好的,我正在慢慢弄清楚。我还有一个问题。我正在使用一个字符串,并表示如果该字符串等于单元格文本,则在加载 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 的复选标记不会消失。这是为什么?

最佳答案

你犯了一个常见的错误。

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

排他检查 TableView 的编辑示例

- (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您可以下载以查看整个项目:

关于objective-c - 为什么 UITableViewCell 附件复选标记不会消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13215073/

相关文章:

iphone - iphone-AVAudioPlayer重叠

xcode - 更改 plist 不能快速工作

ios - Realm 是否有一些方便的方法可以删除所有具有相同属性的 RLMObject?

ios - 在 awakeFromNib() 上 - 错误实例成员 'button' 不能在类型 'CustomView' 上使用

ios - 使用 .m3u8 和 .ts 文件以 iPhone 作为服务器进行直播

ios - 使用 Parse : kPFErrorPaymentDisabled 在 App Purchase 中进行沙盒测试

objective-c - 我怎样才能做一个不区分大小写的 NSFetchedResultsController 类型,它也忽略像 "the"这样的词?

ios - 应用程序在iOS 10及更早版本上使用 “Could not instantiate class named _UIScrollViewLayoutGuide”崩溃

objective-c - 循环的多个条件?

ios - 为什么导航 Controller 在 - (BOOL)shouldPerformSegueWithIdentifier : is implemented? 时不工作