tableView没有正确地重用单元格

时间:2013-03-17 07:40:43

标签: iphone ios uitableview

这是我的tableView代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CounterCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"CounterCell" owner:self options:nil];
        cell = nibLoadedCell2;
    }

    Program *program = [memory getProgramAtIndex:[memory chosenProgram]];
    NSMutableArray *arrayOfIntervals = [[NSMutableArray alloc] init];
    arrayOfIntervals = program.arrayOfIntervals;
    NSMutableDictionary *interval = [arrayOfIntervals objectAtIndex:indexPath.row];


    UITextField *intervalName = (UITextField *)[cell viewWithTag:1];
    intervalName.text = [interval objectForKey:@"nameOfInterval"];
    [intervalName addTarget:self action:@selector(returnKey:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [intervalName addTarget:self action:@selector(editingEnded:) forControlEvents:UIControlEventEditingDidEnd];
    [intervalName addTarget:self action:@selector(editTextField:) forControlEvents:UIControlEventEditingDidBegin];

    timeInSeconds = [[interval objectForKey:@"timeInSeconds"] intValue];

    if (indexPath.row == 0) {
        firstCellGeneralTime = timeInSeconds;
    }

    NSString *time = [self timeTextWithTimeInSeconds:timeInSeconds];

    UILabel *intervalTime = (UILabel *)[cell viewWithTag:2];
    intervalTime.text = time;

    if (indexPath.row == 0) {
        majorLabel.text = time;
    }

    UILabel *hourString = (UILabel *)[cell viewWithTag:11];
    hourString.text = [NSString stringWithFormat:@"%d",hoursInt];

    UILabel *minuteString = (UILabel *)[cell viewWithTag:12];
    minuteString.text = [NSString stringWithFormat:@"%d",minutesInt];

    UILabel *secondString = (UILabel *)[cell viewWithTag:13];
    secondString.text = [NSString stringWithFormat:@"%d",secondsInt];



    UIButton *editTime = (UIButton *)[cell viewWithTag:21];
    [editTime addTarget:self action:@selector(openPicker:) forControlEvents:UIControlEventTouchUpInside];




    return cell;
}

我的表格视图显示少于3个单元格,并且在某一点上我删除了第一个单元格,而第四个单元格正在显示。

问题在于第4个细胞与我的第3个细胞完全相同,尽管它应该是不同的。

它确实重复使用它。

我不知道我做错了什么。

这是删除单元格的代码:

NSIndexPath *firstCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *arrayOfIndexPath = [[NSArray alloc] initWithObjects:firstCellIndexPath, nil];
[atableView deleteRowsAtIndexPaths:arrayOfIndexPath withRowAnimation:UITableViewRowAnimationTop];

2 个答案:

答案 0 :(得分:0)

删除单元格时,您必须从interval数组中删除第一个program.arrayOfIntervals

答案 1 :(得分:0)

找到它,有问题的代码是:

Program *program = [memory getProgramAtIndex:[memory chosenProgram]];
NSMutableArray *arrayOfIntervals = [[NSMutableArray alloc] init];
arrayOfIntervals = program.arrayOfIntervals;
NSMutableDictionary *interval = [arrayOfIntervals objectAtIndex:indexPath.row];

我正在创建一个新程序并从中获取间隔的详细信息,而不是使用我删除第一个间隔的程序...

我感到很蠢......