Ios tableview中可能存在的错误

Ios tableview中可能存在的错误,ios,uitableview,Ios,Uitableview,我有一个UITableViewController并实现以下功能: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.tableView cellForRowAtIndexPath:self.oldIndexPath].accessoryType = UITableViewCellAccessoryNone; NSLog(@"%@", inde

我有一个UITableViewController并实现以下功能:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[self.tableView cellForRowAtIndexPath:self.oldIndexPath].accessoryType = UITableViewCellAccessoryNone;

NSLog(@"%@", indexPath);
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.oldIndexPath = indexPath;
}
我注意到的是,复选标记出现在两个单元格上,其中一个单元格有索引路径,例如,如果我选择了索引路径为
{length=2,path=0-2}
的单元格,向下滚动时,我注意到复选标记在
{length=2,path=0-14}
上也可见

表视图中只有1个部分,大约有18-19行

有人能帮我核实一下吗/或者我该睡觉了:/

附言:在模拟器和设备上进行了测试

单元的创建如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
// Configure the cell... 
cell.textLabel.text = self.stationsDictionary[@"urls"][indexPath.row][@"title"]; 
return cell; 
}

因为您的单元格是可重用的(我想),所以您需要将其设置为

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.tableView cellForRowAtIndexPath:self.oldIndexPath].accessoryType = UITableViewCellAccessoryNone;

    NSLog(@"%@", indexPath);

    self.oldIndexPath = indexPath;
    for (UITableViewCell *cell in tableView.visibleCells) {
        if ([tableView indexPathForCell:cell].section == indexPath.section && [tableView indexPathForCell:cell].row == indexPath.row) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
}

或者,被重用的单元将保留上一个单元(被重用的单元)的状态,可以进行检查


常见错误;)

因为您的单元格是可重用的(我想),所以您需要在

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.tableView cellForRowAtIndexPath:self.oldIndexPath].accessoryType = UITableViewCellAccessoryNone;

    NSLog(@"%@", indexPath);

    self.oldIndexPath = indexPath;
    for (UITableViewCell *cell in tableView.visibleCells) {
        if ([tableView indexPathForCell:cell].section == indexPath.section && [tableView indexPathForCell:cell].row == indexPath.row) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
}

或者,被重用的单元将保留上一个单元(被重用的单元)的状态,可以进行检查


常见错误;)

因为您的单元格是可重用的(我想),所以您需要在

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.tableView cellForRowAtIndexPath:self.oldIndexPath].accessoryType = UITableViewCellAccessoryNone;

    NSLog(@"%@", indexPath);

    self.oldIndexPath = indexPath;
    for (UITableViewCell *cell in tableView.visibleCells) {
        if ([tableView indexPathForCell:cell].section == indexPath.section && [tableView indexPathForCell:cell].row == indexPath.row) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
}

或者,被重用的单元将保留上一个单元(被重用的单元)的状态,可以进行检查


常见错误;)

因为您的单元格是可重用的(我想),所以您需要在

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.tableView cellForRowAtIndexPath:self.oldIndexPath].accessoryType = UITableViewCellAccessoryNone;

    NSLog(@"%@", indexPath);

    self.oldIndexPath = indexPath;
    for (UITableViewCell *cell in tableView.visibleCells) {
        if ([tableView indexPathForCell:cell].section == indexPath.section && [tableView indexPathForCell:cell].row == indexPath.row) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
}

或者,被重用的单元将保留上一个单元(被重用的单元)的状态,可以进行检查


常见错误;)

当您从
cellforrowatinexpath
将单元格出列时,请确保重置其状态-在您的情况下,
accessoryType
属性

由于单元格是重用的,所以当索引为2的单元格不可见时,将重用该单元格以显示索引为14的行

因此,如果要保留每个单元格的状态,则需要保存每个单元格状态(如果只需要保存
accessoryType
属性,则需要在
DidSelectRowatineXpath
中保存),然后恢复到
CellForRowatineXpath
中的单元格中。
您可以使用
NSArray
,将单元格索引用作数组索引,以保存单元格状态,甚至可以使用
UITableViewCellAccessoryType
或布尔值的“普通”数组从
cellForRowAtIndexPath
中退出单元格队列时,请确保重置其状态-在您的情况下是
accessoryType
属性

由于单元格是重用的,所以当索引为2的单元格不可见时,将重用该单元格以显示索引为14的行

因此,如果要保留每个单元格的状态,则需要保存每个单元格状态(如果只需要保存
accessoryType
属性,则需要在
DidSelectRowatineXpath
中保存),然后恢复到
CellForRowatineXpath
中的单元格中。
您可以使用
NSArray
,将单元格索引用作数组索引,以保存单元格状态,甚至可以使用
UITableViewCellAccessoryType
或布尔值的“普通”数组从
cellForRowAtIndexPath
中退出单元格队列时,请确保重置其状态-在您的情况下是
accessoryType
属性

由于单元格是重用的,所以当索引为2的单元格不可见时,将重用该单元格以显示索引为14的行

因此,如果要保留每个单元格的状态,则需要保存每个单元格状态(如果只需要保存
accessoryType
属性,则需要在
DidSelectRowatineXpath
中保存),然后恢复到
CellForRowatineXpath
中的单元格中。
您可以使用
NSArray
,将单元格索引用作数组索引,以保存单元格状态,甚至可以使用
UITableViewCellAccessoryType
或布尔值的“普通”数组从
cellForRowAtIndexPath
中退出单元格队列时,请确保重置其状态-在您的情况下是
accessoryType
属性

由于单元格是重用的,所以当索引为2的单元格不可见时,将重用该单元格以显示索引为14的行

因此,如果要保留每个单元格的状态,则需要保存每个单元格状态(如果只需要保存
accessoryType
属性,则需要在
DidSelectRowatineXpath
中保存),然后恢复到
CellForRowatineXpath
中的单元格中。
您可以使用
NSArray
,使用单元格索引作为数组索引,来保存单元格状态,甚至可以使用
UITableViewCellAccessoryType
或布尔值的“普通”数组。这看起来像是rowForRowAtIndexPath方法中的一个错误,您在创建单元格时可能做错了什么。这就是我创建单元格的方式:```-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{static NSString*CellIdentifier=@“Cell”;UITableViewCell*Cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];//配置单元格…Cell.textLabel.text=self.StationDictionary[@“url”][indexPath.row][@“title”];return cell;}```我认为在调用cellForRowAtIndexPath之前应该检查可见单元格:请移动问题中的代码这看起来像rowForRowAtIndexPath方法中的一个bug,您在创建单元格时可能做错了什么这就是我创建单元格的方式:```-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{static NSString*CellIdentifier=@“Cell”;UITableViewCell*Cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];//配置单元格…Cell.textLabel.text=self.StationDictionary[@“URL”][indexPath.row][@“title”];return cell;}```我认为在调用CellForRowatineXpath之前应该检查可见单元格:请移动问题中的代码这看起来像是RowForRowatineXpath方法中的错误,您在创建单元格时可能做错了这就是我创建