繁体   English   中英

重用表格视图中的单元格导致问题

[英]Reusing Cells in Table View Causing Problems

TableView中的单元格设置有问题...我用图像视图,两个标签,一个标题和一个细节设置了表格视图。 第一部分看起来不错,但是滚动之后,它会不断将所有标签堆叠在一起。

这是我的代码:

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

static NSString *CellIdentifier = @"Cell";

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

}



RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
   NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];
NSLog(@"%@", articleDateString);
//  cell.textLabel.text = entry.articleTitle; 
// cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, entry.blogTitle];

UIFont *cellFont = [UIFont fontWithName:@"ArialRoundedMTBold" size:17];    
//cell.textLabel.font = cellFont;
UIFont *cellFont2 = [UIFont fontWithName:@"ArialRoundedMTBold" size:12];    
//cell.detailTextLabel.font = cellFont2;
UIImage *img = [UIImage imageNamed:@"anicon.png"];
UIImageView *alternate = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,70,70)];


alternate.image = img;
UILabel *alternatelabel = [[UILabel alloc] initWithFrame:CGRectMake(75,0,210,70)];
alternatelabel.backgroundColor = [UIColor clearColor];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(75, 20, 225, 70)];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, entry.blogTitle];
detailLabel.font = cellFont2;
alternatelabel.font = cellFont;

alternatelabel.text = entry.articleTitle;

[cell.contentView addSubview:alternate];
[cell.contentView addSubview:alternatelabel];
[cell.contentView addSubview:detailLabel];
[detailLabel release];
[alternatelabel release];
[alternate release];
NSLog(@"imageurl%@", img);

return cell;
}

我想建议你只分配并添加到单元格此方法内的单元格子组件中

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(75, 20, 225, 70)];
detailLabel.tag = 88;
detailLabel.backgroundColor = [UIColor clearColor];


[cell.contentView addSubview:detailLabel];
}

然后将此行写到if循环的外面。

UILabel *detailLabel = (UILabel *)[cell viewWithtag:88];
detailLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, entry.blogTitle];

不要忘记分配标签值。

然后就可以像波纹管一样进入

希望你能明白我的意思。

那是因为您一直在重复使用的单元格上添加标签,当单元格被重复使用时,它会恢复原样(添加了之前的标签),并且您继续添加标签...

解决方案

1-子类化UITableViewCell以拥有所需的标签,只需设置其文本和属性

2-从单元格上删除标签(如果有的话),然后重新添加单元格,或者用标签标记它们,以便稍后找到它们并设置其文本(我可能会选择选项1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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