更改iPhone中tableView中图像的位置?
我想在每个 tableView 的单元格中间显示图像,因为在 tableView cellForRowAtIndexPath 方法中,我可以根据需要自定义每个单元格,
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 框架中设置。 我哪里错了&我做什么在表格视图每个单元格的中间显示图像。
I want to show image in each tableView's cell at middle of it for that in tableView cellForRowAtIndexPath method i can customize each cell like to my need
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;
but the image is not set in the UIImageView frame.
where i'm wrong & what i do for show image at the middle of tableview each cell.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cell.image
在这里不会帮助你。将图像固定到
UIImageView
,然后将UIImageView
对象添加到单元格的contentView
,就像对其他情况所做的那样。并照常设置 UIImageView 的框架。您应该会看到您想要的结果。编辑如下
为了更清楚,以防我含糊不清。你曾说过:
cell.image
won't help you here.Pin the image to a
UIImageView
and then add theUIImageView
object to thecontentView
of the cell just like you do for the other cases. And set the frame for theUIImageView
as usual. You should see your desired result.Edit as following
To be clearer in case I was vague. You have say: