【发布时间】:2018-12-30 07:58:41
【问题描述】:
我有一个由文本和图像组成的 UITableView 应用程序。单元格有时会覆盖自己,如果我滚动问题会变得更糟。见http://web-cars.com/images/temp_img/Simulator-Screen-Shot.png
我在以前的应用程序中遇到了同样的问题,一位朋友向我展示了如何通过子类化 UITableViewCell 来解决它。那是一个纯文本应用程序,我无法通过图像获得相同的解决方案。
这是我的子类 UITableViewCell:
-(void)prepareForReuse {
self.permLabel101.text = @"";
self.permUIImageView201.image = nil;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
UILabel *label101 = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 310, 100)];
label101.tag = 101;
[label101 setLineBreakMode:NSLineBreakByWordWrapping];
[label101 setFont:[UIFont systemFontOfSize:FONT_SIZE]];
label101.textColor = [UIColor blackColor];
label101.backgroundColor = [UIColor clearColor];
label101.numberOfLines = 0;
self.permLabel101 = label101;
[self.contentView addSubview:self.permLabel101];
UIImageView *image201 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
image201.tag = 201;
return self;
}
这是我的 cellForRowAtIndexPath。我用 photo_text 划分文字和图片。
- (MyTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = (NSManagedObject *)[entityArray objectAtIndex:indexPath.row];
NSString *CellIdentifier = @"Cell";
int photo_text = [[object valueForKey:@"photo_text"]intValue];
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.selectionStyle = UITableViewCellSeparatorStyleNone;
[self.tableView setSeparatorColor:[UIColor clearColor]]; // Eliminates border between cells
if (photo_text == 1) { // Image will be here
NSNumber *imgHeight = [[entityArray objectAtIndex:indexPath.row]valueForKey:@"imgHeight"];
float imgHeightFloat = [imgHeight floatValue];
NSLog(@"imgHeightFloat: %f", imgHeightFloat);
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 2, 320, imgHeightFloat)]; // imv is OK
UIImageView *image201View = [[UIImageView alloc]initWithFrame:CGRectMake(0, 2, 320, imgHeightFloat)];
imv.image = [UIImage imageNamed:[object valueForKey:@"photo"]];
image201View.image = [UIImage imageNamed:[object valueForKey:@"photo"]];
NSLog(@"Image cell: %@", cell);
NSString *title = [object valueForKey:@"title"];
self.title = title;
[cell.contentView addSubview:image201View];
return cell;
} else if (photo_text == 2) { // Text will be here
UILabel *label101;
cell.selectionStyle = UITableViewCellSeparatorStyleNone;
[self.tableView setSeparatorColor:[UIColor clearColor]]; // Eleminates border between cells
NSString *text = [object valueForKey:@"text"];
CGSize constraint = CGSizeMake(296.0f, 20000.0f);
CGSize size = [text sizeWithFont: [UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"Text cell: %@", cell);
label101 = (UILabel *)[cell viewWithTag:101];
label101.frame = CGRectMake(5, 5, 320, size.height);
label101.text = [NSString stringWithFormat:@"%@", text];
NSString *title = [object valueForKey:@"title"];
self.title = title;
return cell;
}
// Configure the cell...
// return cell;
}
欢迎提出建议!!
【问题讨论】:
-
在准备单元重用时
self.permUIImageView201.image = nil;将没有任何作用,因为没有参考!每次重用单元格时,``` -
@vignesh:我认为 MyTableViewCell 中的 -(void)prepareForReuse 会删除图像,从而阻止其重用,但我现在看到它没有。我试过 image201View.image = nil;在 cellForRowAtIndexPath 和 MyTableViewCell 但那里没有运气。我同意我在不删除前一个图像的情况下添加图像,但不知道如何删除该图像。有什么建议吗?
-
@safariyellow 使用 nil 分配引用不会删除添加为子视图的视图,您需要做的是在单元格重用时使用带有标签的 removeSubview。
标签: ios objective-c uitableview