我UITableViewCell
在 Interface Builder 中创建了一个并为它创建了一个子类。在里面我需要在标签中显示抽奖结果。我不知道我会得到多少结果,所以我不能在 IB 中为它创建标签,所以我在里面创建它们cellForRowAtIndexPath
。所以现在发生了什么,当重用单元格时,我不断在子视图上创建子视图。
我考虑awakeFromNib
在 Cell 子类的 Interface Builder \ 中创建标签,并使用它们的标签填充它们,但我不知道会有多少标签。
有什么好办法解决吗?
一旦超出屏幕区域,有没有办法删除单元格的内容?
- (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;
}