我有一个包含文本和图像的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;
}
建议表示赞赏!
答案 0 :(得分:0)
一个朋友能够指出我正确的方向。
我的主要问题是我遇到了CellIdentifier冲突。
在if语句分隔图像和文本单元格之前,我有以下内容:
NSString *CellIdentifier = @"cell";
我需要为图像和文本单元格使用不同的CellIdentifiers:
NSString *CellIdentifier = @"cell";
和 NSString * CellIdentifier = @“ cell2”;
事实证明,我不需要将UILabel和UIImageView放在子类UITableViewCell中。
最后,要摆脱关于cellForRowAtIndexPath的警告:我需要
` return cell;
} else {
return nil;
}`