TableView 中的单元格设置有问题...我设置了带有 ImageView 的表格 View 、两个标签、一个标题和一个详细信息。第一部分看起来不错,但滚动后,它不断地将所有标签堆叠在一起。
这是我的代码:
- (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];
不要忘记分配标签值。
然后你就可以像下面这样访问了
希望你能明白我想说的......
关于iphone - 在 TableView 中重用单元格会导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11019908/