简体   繁体   中英

Unknown amount of UILabels inside of a UITableViewCell

I've created a UITableViewCell in the Interface Builder and created a subclass for it. Inside it I need to display raffle results in labels. I don't know how many results I'm going to get so I can't create labels for it in the IB so I'm creating them inside cellForRowAtIndexPath . So what is happening right now that I keep creating subviews over subviews when the cell is reused.

I thought about creating the labels in the Interface Builder \\ awakeFromNib of the Cell subclass and them fill them using their tag but I don't know how many labels are going to be.

What's a good way to solve it?

Is there a way to remove to content of the cell once it's out of the screen region?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //Where we configure the cell in each row
    id currentRaffle = [_winnings objectAtIndex:indexPath.row];
    RaffleResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"raffleResCell"];
    if (cell == nil) {
        cell = [[RaffleResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"raffleResCell"];
    }
    cell.prizeTitle.text = [currentRaffle valueForKey:@"title"];
    NSArray *winningNumbers = [[currentRaffle valueForKey:@"winningNumbers"] componentsSeparatedByString:@","];
    cell.numbOfRowsPerCell = 1+ [winningNumbers count]/4;
    int row =0;
    int col=0;
    for (int i=0;i<[winningNumbers count];i++)
    {
        UILabel *temp =[[UILabel alloc]initWithFrame:CGRectMake(70*row, 30*col, 70, 20)];
        temp.textAlignment = UITextAlignmentCenter;
        temp.font=[temp.font fontWithSize:14];
        temp.text = [NSString stringWithFormat:@"%@  ",[winningNumbers objectAtIndex:i]];
        [cell.winningNumbersView addSubview:temp];
        if(row<3)
        [cell.winningNumbersView addSubview:line];
        row++;
        if(row >3)
        {
            row=0;
            col++;
        }
    }

    return cell;
}

If you are adding subviews to the cell instead of using the default label, you need to remove the subviews that already have the cell with something like this:

while (cell.subviews.count != 0)
{
    [[cell.subviews objectAtIndex:0] removeFromSuperview];
}
// And then, add the new subviews

Hope it helps.

You are adding the UILabels every time, regardless of reuse. You need to add the UILabels at cell creation only.

This requires a slightly more elegant solution though if the number of labels each time will vary.

Perhaps add a single UIView container in IB , that will hold all of your dynamically created UILabels , and remove all the UILabels each time.

eg

for (UILabel *label in cell.labelContainer.subviews) {
    [label removeFromSuperview];
}

有一个在这种计算策略的是每次的单元被装载的标签将以上及以上的单元,wheenever一遍又一遍被添加一个问题cellForrowAtIndexpath:是called.To避免创建和加入UILabel 必须在完成if(cell == nil)部分

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM