开发者

Change the position of image in tableView in iphone?

开发者 https://www.devze.com 2023-01-26 22:03 出处:网络
I want to show image in each tableView\'s cell at middle of it for that in tableView cellForRowAtIndexPath method i can customize e开发者_运维百科ach cell like to my need

I want to show image in each tableView's cell at middle of it for that in tableView cellForRowAtIndexPath method i can customize e开发者_运维百科ach 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.


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

精彩评论

暂无评论...
验证码 换一张
取 消