我想在每个tableView的单元格中间显示图像,因为在tableView中我可以根据需要自定义每个单元格
UIImage *backgroundImage = [UIImage imageWithContentsOfFile:
[[NSBundle mainBundle]
pathForResource:@"phone"
ofType:@"png"]];
infoView = [[UIView alloc]initWithFrame:cell.frame];
lblCell1 = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, (cell.frame.size.width)/3, cell.frame.size.height)]autorelease];
lblCell1.backgroundColor = [UIColor grayColor];
[infoView addSubview:lblCell1];
imgCell1 = [[[UIImageView alloc]initWithFrame:CGRectMake((cell.frame.size.width)/3, cell.frame.size.width, 30, (cell.frame.size.height)/2)]autorelease];
[infoView addSubview:imgCell1];
lblCell2 = [[[UILabel alloc]initWithFrame:CGRectMake(((cell.frame.size.width)/3)+30, 0, cell.frame.size.width,(cell.frame.size.height)/2 )]autorelease];
lblCell2.backgroundColor = [UIColor lightGrayColor];
[infoView addSubview:lblCell2];
lblCell3 = [[[UILabel alloc]initWithFrame:CGRectMake((cell.frame.size.width)/3, (cell.frame.size.height)/2, cell.frame.size.width, (cell.frame.size.height)/2)]autorelease];
lblCell3.backgroundColor = [UIColor brownColor];
[infoView addSubview:lblCell3];
[cell.contentView addSubview:infoView];
//[cell.contentView addSubview:imgCell1];
//[cell.contentView addSubview:lblCell2];
//[cell.contentView addSubview:lblCell3];
Player *doc = [[Player alloc]init];
NSMutableArray* reversedArray = [[NSMutableArray alloc] initWithArray:[[[[NSArray alloc] initWithArray: _data] reverseObjectEnumerator] allObjects]];
doc = [reversedArray objectAtIndex:indexPath.row];
lblCell1.text = doc.name;
lblCell2.text = doc.email;
lblCell3.text = doc.phone;
imgCell1.image = backgroundImage;
[imgCell1 sizeToFit];
// cell.imgCell1.image = [UIImage imageNamed:@"phone.png"];
return cell;
但是图像未在UIImageView框架中设置。 哪里我错了&我在桌面中间显示每个单元格的图像。
答案 0 :(得分:1)
cell.image
在这里不会帮到你。
将图像固定到UIImageView
,然后将UIImageView
对象添加到单元格的contentView
,就像对其他情况一样。并像往常一样设置UIImageView
的框架。你应该看到你想要的结果。
编辑如下
如果我含糊不清,要更加清楚。你说:
UIImage *image = blah blah blah;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = image;
imageView.frame = CGRectMake(blah,blah,blah,blah); // position it to the middle
[[cell contentView] addSubview:imageView];