UITableviewcell 布局中的 UIImage

Posted

技术标签:

【中文标题】UITableviewcell 布局中的 UIImage【英文标题】:UIImage in UITableviewcell layout 【发布时间】:2012-04-12 07:31:50 【问题描述】:

我在代码中布局对象时遇到问题。 这是当前和期望状态的图片:

这是代码

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    
if (self) 
    self.title = NSLocalizedString(@"My Favorites", @"My Favorites");
    self.tabBarItem.image = [UIImage imageNamed:@"second"];

    

favoritesTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 364) style:UITableViewStylePlain];
[favoritesTable setHidden:NO];
[favoritesTable setDelegate:self];
[favoritesTable setDataSource:self];

[self.view addSubview:favoritesTable];        

[favoritesTable release];

return self;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
static NSString *cellIdentifier = @"<#MyCell#>";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];


NSInteger objectLocation = indexPath.row;

FoodData* food = (FoodData*)[resultsArray objectAtIndex:objectLocation];    

cell.imageView.image = [UIImage imageNamed:@"paris.jpg"];
cell.imageView.frame = CGRectMake(270, 4, 40, 36);    

cell.imageView.userInteractionEnabled = YES;
cell.imageView.tag = indexPath.row;

UILabel* lblText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 260, 20)];
UILabel* lblType = [[UILabel alloc] initWithFrame:CGRectMake(0, 21, 260, 20)];

[lblText setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];
[lblType setFont:[UIFont fontWithName:@"Helvetica" size:9.0]];

lblType.textColor = [UIColor blueColor  ];

[lblText setText:[food foodName]];
[lblType setText:[food foodType]];

[cell addSubview:lblText];
[cell addSubview:lblType];

[lblText release];
[lblType release];

return cell;

【问题讨论】:

【参考方案1】:

ios5 上,使用 IB 上的原型单元将是您最简单的出路。

如果您不在 ios 5 上,我强烈建议您使用自定义 tableviewcell。

您的问题的简短解决方案可以在下面的链接中找到。

UITableViewCell with image on the right?

【讨论】:

我用 UIImageView 做了,但我无法捕捉到它的 ontouch 事件。你可以在这里看到我的最后一个问题:***.com/questions/10029049/… hmmm... 似乎您只询问了部分要求。如果您可以更新您的问题,包括您打算实现的目标,那就太好了。但我认为向 uiImage 添加事件不会有问题。当我拿到Mac时,我会检查一下。

以上是关于UITableviewcell 布局中的 UIImage的主要内容,如果未能解决你的问题,请参考以下文章

UITableviewcell 布局中的 UIImage

带有 UITextView 的 UITableViewCell 中的自动布局

缩进 UITableViewCell 中的所有自动布局约束

uitableviewcell中的swift uitableview,自动布局始终高度为0

UITableViewCell 中的 UITextField 使用自动布局

UITableViewCell中的自动布局约束不匹配