UITableView在滚动后混合Cell顺序

时间:2014-11-26 22:14:49

标签: ios objective-c uitableview

我已经实现了一个可以在第一个视图上正常工作的UITableView,当内容足够滚动并滚动它时,它会混合单元格的顺序。

存储内容的数组是NSArray而不是NSMutableArray。

有没有人知道这个问题,或者有人知道如何解决这个问题吗?

提前致谢。

以下是cellForRowAtIndexPath的代码:

static NSString *simpleTableIdentifier = @"TableItem";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableItem"];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier];
}

self.tabBarItem.badgeValue = nil;


if (screensize.height-200 > ([fixObjects count]/3)*44) {
    tableData.scrollEnabled = NO;
} else {
    tableData.scrollEnabled = YES;
}

if ([fixObjects count] == 0) {
    NSLog(@"Array empty");
    cell.textLabel.text = @"No entrys found.";
    cell.detailTextLabel.text = @"";
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.textLabel setBackgroundColor:[UIColor clearColor]];
    [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.2]];
    [cell setBackgroundView:nil];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.detailTextLabel.textColor = [UIColor whiteColor];
} else {
    if (position >= 0 && position < ([fixObjects count]-1)) {
        cell.textLabel.text = [NSString stringWithFormat:@"%@ m in %@", [fixObjects objectAtIndex:position], [fixObjects objectAtIndex:position+1]];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [fixObjects objectAtIndex:position+2]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.textLabel setBackgroundColor:[UIColor clearColor]];
        [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.2]];
        [cell setBackgroundView:nil];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.detailTextLabel.textColor = [UIColor whiteColor];

        position += 3;
    }
}
return cell;

1 个答案:

答案 0 :(得分:1)

正如我们在评论中所说的那样,我真的无法告诉你确切的解决方案,但是位置变量真的很可疑。

请将以下日志添加到&#39; cell.detailTextLabel.text&#39;代码并在滚动期间检查控制台日志:

NSLog(@"cell index - %@ - position - %@ - textLabel - %@",@(indexPath.row),position, cell.textLabel.text);
NSInteger positionCandidate = indexPath.row*3; 
NSString* textCandidate = [NSString stringWithFormat:@"%@ m in %@", [fixObjects objectAtIndex:positionCandidate], [fixObjects objectAtIndex:positionCandidate+1]];
NSLog(@"cell index - %@ - posCandidate - %@ - textCandidate - %@",@(indexPath.row),positionCandidate, textCandidate);

我希望这可以帮助一点!

祝你好运!

相关问题