JonasG
JonasG

Reputation: 9324

UITableView subview appears in wrong section when scrolling?

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 (indexPath.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.......

Upvotes: 0

Views: 1856

Answers (3)

Arsalan Habib
Arsalan Habib

Reputation: 1395

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.

Upvotes: 5

WrightsCS
WrightsCS

Reputation: 50707

You should use switch instead:

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

Upvotes: -1

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73608

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
}

Upvotes: 1

Related Questions