开发者

UITableView subview appears in wrong section when scrolling?

开发者 https://www.devze.com 2023-04-03 22:33 出处:网络
In a UITableView I add a UIView as subview but ONLY for section 1. Section 1\'s content is loaded from a plist and the plist contains mutable content. If there are enough rows to allow scrolling, then

In a UITableView I add a UIView as subview but ONLY for section 1. Section 1's content is loaded from a plist and the plist contains mutable content. If there are enough rows to allow scrolling, then the following happens: I scroll to the bottom, and back up, and the UITextField appears randomly on some of section 0's cells. I have no clue why this is happening! So what i do is this (in ´cellForRowAtIndexPath´):

if (index开发者_开发问答Path.section == 0) {
    //do stuff
}
else if (indexPath.section == 1) {
    d = [UIView alloc] init];
    [cell.contentView addSubview:d];
}

and this gets totally messed up when I scroll. The subviews appear in section 0 where they shoudnt, and on didSelectRowAtIdexPath I reload for section 1 and then subviews even appear twice (over each other)... Its a complete MESS! Please, Please help.......


Without seeing any code this seems to be an issue pertaining to reusable cells. What happens is that the cells that have scrolled off the screen are reused for the new content that is to be shown. So i reckon you need to make a distinction in cellForRowAtIndexPath for section 0 and 1 and basically use different set of cells for them.

EDIT: Ok ima give a shot to your problem here

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;

So now you'll have two types of cells for the tableview that'll be reused one without the subview and one with the subview.


You must be using dequeueReusableCellWithIdentifier. The purpose of dequeueReusableCellWithIdentifier is to use less memory. If the screen can fit 4 or 5 table cells, then with reuse you only need to have 4 or 5 table cells allocated in memory even if the table has 1000 entries.

So the subviews in UITableViewCell are also cached. So when the cell is reused, you need to clean out the old view & then put in the new content.

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: @"your-id"];
if (cell)
{
     //reusing old cell. reset all subviews before putting new content.
}
else
{
     //Create a fresh new cell
}


You should use switch instead:

switch ( indexPath.section )
{
    case 0:
    {
        /* do soemthing */
    }
        break;
    case 1:
    {
        d = [UIView alloc] init];
        [cell.contentView addSubview:d];
    }
        break;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消