Ios 与可重复使用的单元格混淆(滚动后受影响的单元格不正确)

Ios 与可重复使用的单元格混淆(滚动后受影响的单元格不正确),ios,ios4,uitableview,Ios,Ios4,Uitableview,我肯定这个问题不是唯一的,但我还没有找到其他来源来解决它。事实上,这些已经引起了更多的混乱。如果可以的话,请把灯照亮 问题的关键是,我想在一个7节30ish行表的一个单元格中滑动(-50)一个UILabel,其中包含自定义单元格。滚动后,其他几行正在移动此标签,并且随着滚动的继续,它们将继续移动得越来越远 你会注意到-50 x坐标在两个地方进行测试。第一个,在cell==nil中,永远不会被调用。为什么?我以为那是解决这个问题的地方 也不知道如何调用initWithStyle(不用于定制)或in

我肯定这个问题不是唯一的,但我还没有找到其他来源来解决它。事实上,这些已经引起了更多的混乱。如果可以的话,请把灯照亮

问题的关键是,我想在一个7节30ish行表的一个单元格中滑动(-50)一个UILabel,其中包含自定义单元格。滚动后,其他几行正在移动此标签,并且随着滚动的继续,它们将继续移动得越来越远

你会注意到-50 x坐标在两个地方进行测试。第一个,在cell==nil中,永远不会被调用。为什么?我以为那是解决这个问题的地方

也不知道如何调用initWithStyle(不用于定制)或initWithCoder(在情节提要中创建),即使单元格为nil

    NSArray *a = [[userDetailData objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];

    NSString *key = [[[a objectAtIndex:0] allKeys] objectAtIndex:0];
    NSString *value = [[a objectAtIndex:0] valueForKey:key];

    static NSString *CellIdentifier = @"MasterCell";

    UsersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // If cell does not exist, create it, otherwise customize existing cell for this row
    if (cell == nil) {
        // Create cell
        cell = [[UsersTableViewCell alloc] initWithCoder:<#(NSCoder *)#>reuseIdentifier:CellIdentifier];

        // Configure cell:
        NSLog(@"Key: %@   %d, %d", key, [indexPath section], [indexPath row]);
        // Prepare the date field to move left for checkmark or ex-mark

        if ([key isEqualToString:@"Date"]) {
            CGRect frame = cell.comboLabel.frame;
            frame.origin.x -= 50;
            cell.comboLabel.frame = frame;
            NSLog(@"In Date...  minus 50");
        }
    }

    // Customize cell:
    CGRect frame = cell.comboLabel.frame;
    if ([key isEqualToString:@"Date"]) {
        frame.origin.x -= 50;
        cell.comboLabel.frame = frame;
        NSLog(@"   %d, %d\n   cell: %@\n", [indexPath section], [indexPath row], cell);
    }
    else {
        frame.origin.x += 0;
        cell.comboLabel.frame = frame;
    }

    // Reset color
    cell.comboLabel.textColor = [UIColor blackColor];

    // Set the Item
    cell.nameLabel.text = [NSString stringWithFormat: @" %@",key];
    // Give the cell an accessory indicator
    ..... continue on with logic.
NSArray*a=[[userDetailData objectAtIndex:[indepath节]]objectAtIndex:[indepath行]];
NSString*key=[[a objectAtIndex:0]所有键]objectAtIndex:0];
NSString*value=[[a objectAtIndex:0]valueForKey:key];
静态NSString*CellIdentifier=@“MasterCell”;
UsersTableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//如果单元格不存在,请创建它,否则请为此行自定义现有单元格
如果(单元格==nil){
//创建单元
cell=[[UsersTableViewCell alloc]initWithCoder:reuseIdentifier:CellIdentifier];
//配置单元:
NSLog(@“Key:%@%d,%d”,Key,[indexPath节],[indexPath行];
//准备日期字段向左移动以进行复选标记或ex-mark
如果([key isEqualToString:@“Date”]){
CGRect frame=cell.comboLabel.frame;
frame.origin.x-=50;
cell.comboLabel.frame=frame;
NSLog(@“输入日期…减去50”);
}
}
//自定义单元格:
CGRect frame=cell.comboLabel.frame;
如果([key isEqualToString:@“Date”]){
frame.origin.x-=50;
cell.comboLabel.frame=frame;
NSLog(@%d,%d\n单元格:%@\n,[indexPath节],[indexPath行],单元格);
}
否则{
frame.origin.x+=0;
cell.comboLabel.frame=frame;
}
//重置颜色
cell.comboLabel.textColor=[UIColor blackColor];
//设置项目
cell.namelab.text=[NSString stringWithFormat:@“%@”,键];
//给电池一个辅助指示器
..... 继续讲逻辑。

单元格不是nil,因为您刚刚在if语句之前将其分配给了一个可重用的单元格

我会考虑用一个标识符“日期单元”来创建一个辅助原型单元,如果你创建的单元格有键“日期”

,可以做第二组可重用的单元格。 在这里:


事实上,如果您只是在故事板上自定义原型单元,您甚至可能不需要对单元进行大部分自定义。

OK。我明白你提出的想法。这就是为什么我们可以在故事板中有多个“原型单元”?是的,当我有一个包含一些不同类型数据的表时,我会使用它们。我只是为每种类型的数据创建了一个不同的原型,并在cellForRowAtIndexPath函数中对它们进行相应的排序。
NSArray *a = [[userDetailData objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];

NSString *key = [[[a objectAtIndex:0] allKeys] objectAtIndex:0];
NSString *value = [[a objectAtIndex:0] valueForKey:key];

static NSString *CellIdentifier = @"MasterCell";
static NSString *DateCellIdentifier = @"Date Cell";

UsersTableViewCell *cell;

//Customize Cell

if ([key isEqualToString:@"Date"]) {

    cell = [tableView dequeueReusableCellWithIdentifier:DateCellIdentifier];
    CGRect frame = cell.comboLabel.frame;

    frame.origin.x -= 50;
    cell.comboLabel.frame = frame;
    NSLog(@"   %d, %d\n   cell: %@\n", [indexPath section], [indexPath row], cell);
}
else {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    frame.origin.x += 0;
    cell.comboLabel.frame = frame;
}

// Reset color
cell.comboLabel.textColor = [UIColor blackColor];

// Set the Item
cell.nameLabel.text = [NSString stringWithFormat: @" %@",key];
// Give the cell an accessory indicator
..... continue on with logic.