views:

30

answers:

3

I have a UITableView that is being populated from an NSMutable array that has 79 entries in it. When I run my app and scroll down the table there seems to be multiple entries in one cell. It seems to happen about a screen height and a half down the table.

For example:

One object in the array is @"Dog" and another is @"Cat". In one cell you can see both Cat and Dog written in the cellLabel text.

Code for init'ing the cells

// Customize the appearance of table view cells.
- (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];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    // Configure the cell...

    UILabel *cellLabel = [[[UILabel alloc] initWithFrame:cell.frame] autorelease];
    cellLabel.text = [symptomsArray objectAtIndex:indexPath.row];
    cellLabel.textColor = [UIColor blackColor];
    cellLabel.backgroundColor = [UIColor clearColor];
    cellLabel.opaque = NO;
    [cell.contentView addSubview:cellLabel];
    cell.contentView.backgroundColor = [UIColor colorWithRed:(214.0/255) green:(215.0/255.0) blue:(217.0/255) alpha:1.0];

    return cell;
}
+1  A: 

That is because you are adding a label everytime If you really need that new UiLabel then add it inside the if sentence you have and change it's text out of the if

And to do this efficiently you will have to subclass UITableViewCell maybe

Hope this helps

nacho4d
+1  A: 

Subviews are positioned relative to their superviews' bounds. So, for example, if you want cellLabel to be in the upper-left corner of the content view, you need to set its origin to (0, 0). You're using the cell's frame, which is relative to the upper-left corner of the table view.

When you scroll down, UITableView is reusing old cells, and you're adding additional labels to those cells. These cells have frames with origins offset by a screen or two from the top of the table view, so you're positioning the new labels outside the cell's actual bounds. Here's how to fix it.

static int CellLabelTag = 12345;

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.accessoryType = UITableViewCellAccessoryTypeNone;

    UILabel *cellLabel = [[[UILabel alloc] initWithFrame:cell.contentView.bounds] autorelease];
    cellLabel.tag = 585493;
    [cell.contentView addSubview:CellLabelTag];
}

UILabel *cellLabel = (UILabel *)[cell viewWithTag:CellLabelTag];
cellLabel.text = // your text here
// blah blah blah configuration
return cell;
Alex
A: 

Keep it Simple.

If you only have a few entries, why not allocate an array of cells before load and load the cell object from the array directly inside:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

 return [arrayOfCells objectAtIndex:indexPath.row]; //already allocated one single time at the beginning

    }

No more dequeueReusableCellWithIdentifier..

Keep it simple!

lupu1001
Thanks. This ended up being the best way for me.
Pick