首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UITableViewCell accessoryType UITableViewCellAccessoryCheckmark重复

UITableViewCell accessoryType UITableViewCellAccessoryCheckmark重复
EN

Stack Overflow用户
提问于 2014-10-20 20:41:32
回答 1查看 277关注 0票数 0

我有一个允许多个选择的UITableView,但是由于某种原因,当我滚动UITableView时,我在滚动时重复使用UITableViewCellAccessoryCheckmark所做的选择。

这是我正在使用的代码

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.backgroundColor = [UIColor clearColor];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = [sortedMachineNames objectAtIndex:indexPath.row];

    return cell;
}

#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [selectedMachinesMArray addObject:cell.textLabel.text];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    cell.accessoryType = UITableViewCellAccessoryNone;
    NSUInteger index = [selectedMachinesMArray indexOfObject:cell.textLabel.text];
    if (index!=NSNotFound) {
        [selectedMachinesMArray removeObjectAtIndex:index];
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-20 21:36:01

在cellforIndexAtPath中,检查单元格的当前状态并更改可访问类型的值,如下所示:

代码语言:javascript
运行
复制
if(marked)
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
     cell.accessoryType = UITableViewCellAccessoryNone;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26474556

复制
相关文章

相似问题

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