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

时间:2012-06-13 17:09:52

标签: iphone ios xcode uitableview

在TableView中遇到Cell设置问题...我设置了带有图像视图的表视图,以及两个标签,一个标题和一个细节。第一部分看起来很好,但滚动后,它会不断地将所有标签堆叠在一起。

这是我的代码:

- (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;
}

3 个答案:

答案 0 :(得分:1)

我想建议你只需在此方法中为单元格

分配并添加到单元子组件中
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];
}

然后在循环中写出这一行。

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

不要忘记分配标记值。

然后你可以像下面那样访问

希望你能得到我想说的话......

答案 1 :(得分:0)

那是因为你继续在重复使用的细胞上添加标签,当一个细胞被重复使用时它会恢复原状,(你添加了之前的标签),并且你不断添加标签......

解..

1-子类UITableViewCell以获得所需的标签,并设置其文本和属性

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

答案 2 :(得分:0)