iOS: How to access custom labels on the contentView of an UITableViewCell correctly?

前端 未结 1 1497
梦谈多话
梦谈多话 2021-01-07 07:34

I have a UITableView with each cell being broken into several sections with a different label in each section. The table gets populated by an NSArray of NSDictionaries whic

相关标签:
1条回答
  • 2021-01-07 08:33

    Probably you are assigning the value within the if(cell==nil) block? Only the initialization should happen within that block. Move the rest out of it. (Post your complete cellForRow code, if you need more help)

    //edit: now, after you posted your code i see your problem:

    you are storing all your labels and views in member variables.. but of course that only happens, when the if(cell != nil) block is executed. After that, you always access the same single cell (that was assigned last). So you are updating at least one cell ;)

    To fix your problem, work e.g. with tags to get your corresponding views back from the cell. I will show it for your backgroundView, but you have to do it for all of your views (INSTEAD of the member variables. Remove them.)

    static NSString *cellIdentifier = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
    
    if (cell == nil) {
      //// Background View
      UIView* cellBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 622, 43)];
      cellBackgroundView.tag = 1;
      [cellBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Images/Library/phase_table_cell.png"]]];
    }
    
    // get the backgroundview from the current cell
    UIView* backgroundView = [cell.contentView viewWithTag: 1];
    

    and so on..

    0 讨论(0)
提交回复
热议问题