Donal Rafferty
Donal Rafferty

Reputation: 19826

iphone - UITableView scroll problem

I have created a TableView in my application with 5 sections in it.

Sections 1 - 4 only contain one row each for the minute and section 5 contains 5 rows.

Everything works fine until I scroll the TableView off the screen. In my first section and row (cell) I have the accessoryView set to a UILabel with some text in it.

Every other cell has the disclosure button as the accessoryType.

When I scroll the tableView the text I have in the first cell somehow appears in the the last cell!?

I have set up my data by adding NSStrings to array's and then adding them as dictionaries to an NSMutableArray.

And here is my cell set up:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

   // UISwitch *aUISwitch = [[[UISwitch alloc]initWithFrame:CGRectZero]autorelease];

    // Configure the cell. 

    NSDictionary *dictionary = [listOfSettings objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"Settings"];
    NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

    if([cellValue isEqualToString:@"Status"]){
        UILabel *viewLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
        [viewLabel setText:@"Connected"];
        cell.accessoryView = viewLabel;
        [viewLabel release];

    }
    else{
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    }
    return cell;
}

I know cells get deleted/removed when they go off screen so I presume this has something to do with it? What is the recommended practice for dealing with cells that go off screen and reappear?

Upvotes: 2

Views: 2024

Answers (1)

Jason Cragun
Jason Cragun

Reputation: 3233

just at quick glance... in the else statement, not only do you need to set the cell.accessoryType, but also set the cell.accessoryView=nil;

the accesoryView is still there as the cell was recycled.

Upvotes: 1

Related Questions