滚动不堆积时如何在tableViewCell中创建数据?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *theCell = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:theCell];
if (! cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:theCell];
}
UILabel *data1 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"nomer"]]WithFrame:CGRectMake(10, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
[cell insertSubview:data1 atIndex:0];
UILabel *data2 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"subjek"]]WithFrame:CGRectMake(50, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
[cell insertSubview:data2 atIndex:1];
UILabel *data3 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"shadowScore"]]WithFrame:CGRectMake(200, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
[cell insertSubview:data3 atIndex:2];
UILabel *data4 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"finalScore"]]WithFrame:CGRectMake(250, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
[cell insertSubview:data4 atIndex:3];
UILabel *data5 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"kkm"]]WithFrame:CGRectMake(300, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
[cell insertSubview:data5 atIndex:4];
UILabel *data6 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"Status"]]WithFrame:CGRectMake(350, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
[cell insertSubview:data6 atIndex:5];
UILabel *data7 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"teachernote"]]WithFrame:CGRectMake(400, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
[cell insertSubview:data7 atIndex:6];
return cell;
}
答案 0 :(得分:6)
问题是每次加载单元格时,您都要添加新的UILabel
。当细胞被回收时,这就成了一个问题,因为它们已经创建了标签并且你正在创建更多的细胞。
您应该对UITableViewCell
进行子类化,在加载时创建所需的UILabel
和布局,然后只需在cellForRowAtIndexPath:
方法中设置信息即可。这使您可以回收单元格并提高性能,同时保持布局并避免您发现的“堆叠问题”。
第二个选项
第二种,不太理想的选择是将UILabel
创建方法移动到if (!cell)
块并使用标记检索它们并将它们设置在该块之外。这样便携性更差,更脆弱,但重复使用标签的效果相同。它看起来像这样:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *theCell = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:theCell];
if (! cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:theCell];
UILabel *data1 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"nomer"]]WithFrame:CGRectMake(10, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
data1.tag = 1;
[cell insertSubview:data1 atIndex:0];
// ... Load Other labels and give unique tags
}
UILabel *data1 = [cell viewWithTag:1];
data1 setText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"nomer"]]];
// ... Load other labels by tag and set text.
return cell;
}