Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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 修改CellForRowAtIndexPath中的一个单元格会更改多个单元格_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 修改CellForRowAtIndexPath中的一个单元格会更改多个单元格

Ios 修改CellForRowAtIndexPath中的一个单元格会更改多个单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个通过tableview显示的名称数组。您最多可以选择3个名称,并且不能重新选择相同的名称。为此,我在CellForRowatineXpath中实现了以下代码:。当我运行代码时,名称显示得很好,但是有多个红细胞的名称我没有选择 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIden

我有一个通过tableview显示的名称数组。您最多可以选择3个名称,并且不能重新选择相同的名称。为此,我在CellForRowatineXpath中实现了以下代码:。当我运行代码时,名称显示得很好,但是有多个红细胞的名称我没有选择

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TableCell";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
    NSArray *sectionNames = [names objectForKey:sectionTitle];
    NSString *name = [sectionNames objectAtIndex:indexPath.row];
    cell.textLabel.text = name;

    if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3]) {
        [cell setUserInteractionEnabled:NO];
        cell.backgroundColor = [UIColor redColor];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}
读到一个类似的问题,他们说这是因为单元格被重用了——但是如果这是真的,那么tableview如何在正确的位置显示正确的名称呢

我试图将代码简化成这样,但仍然没有成功,因为有多个红细胞

myIP = [NSIndexPath indexPathForRow:0 inSection:0];


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TableCell";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
    NSArray *sectionNames = [names objectForKey:sectionTitle];
    NSString *name = [sectionNames objectAtIndex:indexPath.row];
    cell.textLabel.text = name;

    if (indexPath == myIP) {
        cell.backgroundColor = [UIColor redColor];
    }

    return cell;
}

如果需要,我可以发布截图。注意:预期名称正确地标记为红色。

该问题是由于单元格重复使用而发生的。当一个红色背景的单元格被重新使用时,它仍然是红色背景,您不能在代码中的任何地方重新设置它。您需要为CellForRowatineXpath:方法中使用的if条件加上else


滚动时是否会发生这种情况?看起来你正在将背景设置为红色,但没有将其设置为clearColor之前的任何颜色?if用于选定的单元格,但现在正在用于非选定的单元格。这是因为您正在为每个单元格设置名称,但仅为myIP设置背景色。当您滚动时,它将使用以前设置的背景色,除非您为其指定新的颜色。您能写下nameSectionTitles和names对象的真实内容吗?因为if条件:if[name isequaltstring:self.name1]| |[name isequaltstring:self.name2]| |[name isequaltstring:self.name3]{在多种情况下都是真的。通常,在一个if分支中所做的每一个更改都应该在其他每个分支中重置/更改。
if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3])
{
    [cell setUserInteractionEnabled:NO];
    cell.backgroundColor = [UIColor redColor];
    cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
    [cell setUserInteractionEnabled:YES];
    cell.backgroundColor = [UIColor clearColor];
    // Other stuffs
}