views:

15

answers:

1

This is the tableView, as you can see, which cell, have two part, the left one, is a leftUIView, and right one, is the rightUIView. The red and green color can display, but the rightLabel I create can't show successfully. What's wrong with my code? Thank you.

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

        static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
    if (cell == nil) { 
        cell = [[[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleSubtitle 
                 reuseIdentifier:SectionsTableIdentifier] autorelease];
    }

    cell.textLabel.textColor = [UIColor whiteColor];
    UIView *rightUIView = [[UIView alloc] initWithFrame:CGRectMake(160, 0, 160, cell.frame.size.height)];
    [rightUIView setBackgroundColor:[UIColor greenColor]];
    UIView *leftUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, cell.frame.size.height)];
    [leftUIView setBackgroundColor:[UIColor redColor]];

    UILabel *rightLabel = [[UILabel alloc] init];
    [rightLabel setText:@"dummy"];
    [rightUIView addSubview:rightLabel];

    [cell addSubView:leftUIView];
    [cell addSubView:rightUIView];

}
+1  A: 

The first problem with your code is that you create cell's subviews each time cell is requested - if you scroll your table to and fro your cell will have a bunch of those subviews - certainly that's not good. You must create cell subviews only once - when you create the cell and after that only set appropriate values to them:

const int kRightViewTag = 100;
const int kRightLabelTag = 200;
...
if (cell == nil) { 
    // Create everything here
    cell = [[[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:SectionsTableIdentifier] autorelease];
    cell.textLabel.textColor = [UIColor whiteColor];

    UIView *rightUIView = [[UIView alloc] initWithFrame:CGRectMake(160, 0, 160, cell.frame.size.height)];
    rightUIView.tag = kRightViewTag;
    [rightUIView setBackgroundColor:[UIColor greenColor]];
    [rightUIView release]; // Do not forget to release object you allocate!!

    UIView *leftUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, cell.frame.size.height)];
    [leftUIView setBackgroundColor:[UIColor redColor]];
    [leftUIView release]; // Do not forget to release object you allocate!!

    UILabel *rightLabel = [[UILabel alloc] initWithFrame:leftUIView.bounds];   
    rightLabel.tag  = kRightLabelTag;
    [rightUIView addSubview:rightLabel];
    [rightLabel release];
}
// Setup values here
UILabel* rightLabel = (UILabel*)[[cell viewWithTag:kRightViewTag] viewWithTag:kRightLabelTag];
rightLabel.text = @"dummy";

The second thing is you do not set label's frame anywhere (I've fixed that above) so it is likely has zero-sized frame and that's why you cannot see it.

The 3rd problem is that you allocate views but don't release them - so they just leak. Do not forget that if you create some objects using alloc, new or copy then you're responsible for releasing them.

Vladimir