Add NSArray items to the UITableViewCell content view


In my app i am adding 3 UILabel to each Table view cell in the following way.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *CellIdentifier = @"ShowMoreCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [self getCellContentView:CellIdentifier];
    return cell;

- (UITableViewCell *)getCellContentView:(NSString *)cellIdentifier
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier] autorelease];
    cell.backgroundColor=[UIColor clearColor];
    for(int j = 0; j < 3; j++)
        [cell insertSubview:[items objectAtIndex:j] atIndex:j];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

Here items is an NSMutableArray and it has got elements of UILabel. I am trying to add three labels in each cell. But it is displaying the 3 labels only in last row.

Am i doing something wrong here ?


You are trying to add the same UILabel objects to different cells. UIViews (which UITableViewCell is a subclass of) can have only one parent view. You should create 3 new labels for every cell (if it doesn't contain them yet).