更改iPhone中tableView中图像的位置?

发布于 2024-10-03 19:00:56 字数 1947 浏览 2 评论 0原文

我想在每个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话难免假 2024-10-10 19:00:56

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];

cell.image won't help you here.

Pin the image to a UIImageView and then add the UIImageView object to the contentView of the cell just like you do for the other cases. And set the frame for the UIImageView as usual. You should see your desired result.

Edit as following

To be clearer in case I was vague. You have say:

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文