【发布时间】:2010-02-11 09:01:53
【问题描述】:
我有一个 UITableView,当然我在其中使用了一些 UITableViewCells。现在有些单元格有一个我想在文本前面显示的图标/图像,而另一些则没有。我没有在 Interface Builder 中创建 UITableViewCells,只是使用默认的东西:
// 自定义表格视图单元格的外观。 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Nearby";
cell.imageView.image = [UIImage imageNamed:@"marker.png"];
break;
case 1:
cell.textLabel.text = @"Bookmarked";
cell.imageView.image = [UIImage imageNamed:@"bookmark.png"];
break;
case 2:
cell.textLabel.text = @"Other";
break;
default:
break;
}
return cell;
}
这似乎工作得很好,但我对 cell.imageView 的宽度有一点问题。我使用的图标似乎有不同的宽度。有些是 21 像素,有些是 25 像素,在某些演员表中我根本没有图标。
这会导致 textLabel 的文本出现在不同的位置(取决于 imageView 的宽度)。
现在我的问题是,我可以添加一些代码/语句来确保我的单元格的 imageView 始终为 30 像素宽吗?即使该 imageView 没有图像?
这可以通过编程方式完成,还是我必须在 InterfaceBuilder 中创建一个自定义单元格,甚至创建我自己的 UITableViewCell 后代。
欢迎提供有关此主题的任何信息。
问候,
史蒂芬
【问题讨论】:
标签: iphone uitableview uiimageview width