Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我的自定义UITableViewCells正在影响彼此的状态_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 我的自定义UITableViewCells正在影响彼此的状态

Ios 我的自定义UITableViewCells正在影响彼此的状态,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我创建了一个自定义UITableViewCell,其中包含一个UISwitch、一个UIStepper和两个标签 当我在模拟器中运行我的应用程序时,tableview会列出这个自定义单元格的每个实例。我注意到,当我在第一个单元格中切换开关并增加其步进(影响一个标签)时,第九个单元格也会受到相同的影响 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexP

我创建了一个自定义UITableViewCell,其中包含一个UISwitch、一个UIStepper和两个标签

当我在模拟器中运行我的应用程序时,tableview会列出这个自定义单元格的每个实例。我注意到,当我在第一个单元格中切换开关并增加其步进(影响一个标签)时,第九个单元格也会受到相同的影响

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *items = [self arrayForSection:indexPath.section];

    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if(indexPath.section == 0){
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.notificationTitle.text = [items objectAtIndex:indexPath.row];
    return cell;
}
我在这个tableview中还有两个部分,并设置第一个部分,以关闭选择样式


到底发生了什么?如何防止它发生?

创建自定义单元格的部分在哪里?你是这样做的,还是只是因为你在粘贴时错过了它

尝试以下操作(希望您使用的是NIB文件来创建自定义单元格):


使用此
[tableView dequeueReusableCellWithIdentifier:questionTableIdentifier]时您实际上是在重用已生成的单元格实例(如果有任何实例需要重用,请创建一个新实例)。UITableView以这种方式工作以节省内存。如果你有一个非常大的单元数量,它仍然只会消耗大约相同数量的内存,就像只有足够的内存覆盖屏幕一样。为了解决您的问题,您需要将单元格的状态保留在其他位置,而不是单元格本身。可能是tableviewcontroller或viewcontroller中的数据结构。然后在tableview要显示单元格时设置值

如果你使用不可重复使用的细胞,那么你可以这样做

@property(nonatomic, strong)NSArray *cells;

- (id)init
{
    self = [super init];
    if ( self )
    {
        _cells = @[@[[[YourCell alloc] init],
                    [[YourCell alloc] init],
                    [[YourCell alloc] init]
                   ],
                   [@[[YourCell alloc] init],
                    [[YourCell alloc] init],
                    [[YourCell alloc] init]]];
    }

    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _cells[indexPath.section][indexPath.row];
}

假设您有2个分区,每个分区中有3个单元格。

它链接到相关tableviewcontroller中的原型单元格。我应该为这个自定义单元类创建一个nib吗?是的,我认为有必要为这个单元创建一个关联的数据结构。我会在服务器运行几天后进行测试。那么我应该为这些单元格创建一个唯一的标识符吗?我只需要10到15个。如果你想使用可重用的单元格,你不能把实际的单元格放在数据结构中,而只是对单元格的状态进行评级。但是如果你只有少量的细胞,那么不使用可重复使用的细胞实际上就容易多了。我将更新我的答案来说明我的意思。进一步看,数据结构确实是一种更好的方法。最后,对单元格进行的每一次更新都只会向服务器发送一个调用,以更新关联用户的设置。我的视图控制器将只下载内容并更新每个单元格。然后将再次调用以更新服务器上的更改。
@property(nonatomic, strong)NSArray *cells;

- (id)init
{
    self = [super init];
    if ( self )
    {
        _cells = @[@[[[YourCell alloc] init],
                    [[YourCell alloc] init],
                    [[YourCell alloc] init]
                   ],
                   [@[[YourCell alloc] init],
                    [[YourCell alloc] init],
                    [[YourCell alloc] init]]];
    }

    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _cells[indexPath.section][indexPath.row];
}