滚动时UITableView子视图出现在错误的部分?

Jon*_*asG 0 objective-c uitableview ios

在UITableView中,我添加了一个UIView作为子视图,但仅用于第1节.第1节的内容是从plist加载的,plist包含可变内容.如果有足够的行允许滚动,则会发生以下情况:我滚动到底部,然后备份,UITextField随机出现在0部分的单元格中.我不知道为什么会这样!所以我做的是这个(在'cellForRowAtIndexPath'中):

if (indexPath.section == 0) {
    //do stuff
}
else if (indexPath.section == 1) {
    d = [UIView alloc] init];
    [cell.contentView addSubview:d];
}
Run Code Online (Sandbox Code Playgroud)

当我滚动时,这完全搞砸了.子视图出现在第0部分,在那里他们是shoudnt,然后didSelectRowAtIdexPath我重新加载第1部分,然后子视图甚至出现两次(相互之间)......它是一个完整的MESS!拜托,请帮忙.......

biz*_*tes 5

没有看到任何代码,这似乎是一个与可重用单元有关的问题.发生的情况是,滚动屏幕的单元格将重新用于要显示的新内容.所以我认为你需要cellForRowAtIndexPath对第0和第1部分进行区分,并且基本上为它们使用不同的单元格集.

编辑:好的,我想在这里解决你的问题

UITableViewCell *cell;

if (indexPath.section == 0) {
    cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithoutSubview"];
    if (cell ==nil ) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:@"CellWithoutSubview"] autorelease];
    }

    //do stuff with cell like set text or whatever
}
else if (indexPath.section == 1) {
    cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithSubview"];
    if (cell ==nil ) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:@"CellWithSubview"] autorelease];

        d = [[UIView alloc] init];
        [cell.contentView addSubview:d];
        [d release];
    }


}

return cell;
Run Code Online (Sandbox Code Playgroud)

因此,现在您将为tableview提供两种类型的单元格,这些单元格将在没有子视图的情况下重复使用,另一种在子视图中重复使用.