【发布时间】:2011-06-27 07:23:15
【问题描述】:
是否可以将图像添加到表格视图中?到左边?如果是这样,它应该是什么尺寸?
【问题讨论】:
标签: iphone cocoa-touch uikit uitableview
是否可以将图像添加到表格视图中?到左边?如果是这样,它应该是什么尺寸?
【问题讨论】:
标签: iphone cocoa-touch uikit uitableview
自定义 UITableViewCell 不需要简单地将图像添加到单元格的左侧。只需在 tableView:cellForRowAtIndexPath: 委托方法中配置 UITableView 单元格的 imageView 属性,如下所示:
- (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];
cell.textLabel.text = @"I'm a UITableViewCell!";
cell.imageView.image = [UIImage imageNamed:@"MyReallyCoolImage.png"];
return cell;
}
除非您在 UITableViewDelegate 中提供 tableView:heightForRowAtIndexPath: 方法,否则 UITableViewCell 的默认高度为 44 磅,在非视网膜显示器上为 44 像素,在视网膜显示器上为 88 像素。
【讨论】:
是的,这是可能的。您可以从cocoawithlove 和here 获得帮助。这些教程将让您了解如何将图像提供给UITableView。最后,正如之前在 SO 上询问的那样,UITableViewCell Set Selected Image。
【讨论】:
是的,您可以在单元格中的任意位置添加它们。最后,它们应该是对您的应用有意义的大(或小)。
【讨论】:
Swift 4托马斯答案的解决方案:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let CellIdentifier = "Cell"
var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: CellIdentifier)
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: CellIdentifier)
}
cell?.textLabel?.text = "I'm a UITableViewCell!"
cell?.imageView?.image = UIImage(named: "MyReallyCoolImage.png")
return cell ?? UITableViewCell()
}
【讨论】: