首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UITableViewController更改一个可重用单元会影响其他单元

UITableViewController更改一个可重用单元会影响其他单元
EN

Stack Overflow用户
提问于 2014-07-15 20:28:22
回答 4查看 2.8K关注 0票数 5

我有一个非常简单的UITableViewController子类,用于向用户显示单元格中字母表中的字符。当用户按下单元格时,它将其附件类型设置为复选标记。

代码语言:javascript
运行
复制
#import "MTGTableViewController.h"

@interface MTGTableViewController ()

@property (nonatomic, strong) NSArray *data;

@end

@implementation MTGTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _data = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];

    // Configure the cell...
    cell.textLabel.text = _data[indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

@end

表视图工作正常。我在我的原型单元格的情节提要中设置了我的reuseIdentifier属性,在我开始选择单元之前,这一切看起来都很好。

问题:当我选择任何单元格时,比如"A“单元格,其他尚未可见的单元格在向下滚动时也会给出复选标记。更糟糕的是,当我向上和向下滚动时,有时单元格"A“上的复选标记被移除并交给单元格"B”。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-07-15 20:37:18

不久前,我也遇到了这个问题,您需要添加另一个数组来跟踪标记了哪个单元格。只需创建一个标记为NSIndexPaths的数组即可。有点像:

代码语言:javascript
运行
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     if ([arrayOfMarked containsObject:indexPath]) {
         cell.accessoryType = UITableViewCellAccessoryNone;
     } else {
         cell.accessoryType = UITableViewCellAccessoryCheckmark;
     }
}
票数 3
EN

Stack Overflow用户

发布于 2014-07-15 20:32:38

这是因为表视图重用单元格的方式。您需要确保在调用dequeueReusableCellWithIdentifier后清除附件项。例:

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];

    // Configure the cell...
    cell.textLabel.text = _data[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;
}

不过,该单元格将“忘记”,因为这段代码选择了它。因此,您需要实际更改accessoryType设置为检查单元格是否被选中的行。

票数 6
EN

Stack Overflow用户

发布于 2017-06-29 14:47:58

For Swift 3:

我发现的一个简化版本是在"cellForRowAt“函数中使用swifts cellForRowAt。

代码语言:javascript
运行
复制
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell

    //To keep from reusing checkmark in reusableCell
    if let selectedIndexPaths = tableView.indexPathsForSelectedRows {
        if selectedIndexPaths.contains(indexPath) {
            cell.accessoryType = .checkmark
        } else {
            cell.accessoryType = .none
        }
    }

    cell.contactsOutlet.text = namesOrganized[indexPath.section].names[indexPath.row]
    return cell
}

然后在didSelect和didDeselect中:

代码语言:javascript
运行
复制
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath)
    cell?.accessoryType = UITableViewCellAccessoryType.checkmark
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath)
    cell?.accessoryType = UITableViewCellAccessoryType.none
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24767569

复制
相关文章

相似问题

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