将图像添加到UItableView

时间:2011-02-15 01:03:18

标签: iphone cocoa-touch uikit uitableview

是否可以将图像添加到表格视图中?在左边?如果是这样,它应该是多大的尺寸?

4 个答案:

答案 0 :(得分:56)

自定义UITableViewCell不需要简单地将图像添加到单元格的左侧。只需在tableView:cellForRowAtIndexPath:delegate方法中配置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像素。

答案 1 :(得分:3)

是的,有可能。您可以从cocoawithlovehere获取帮助。这些教程将为您提供如何将图像提供给UITableView的建议。最后,正如之前所提到的那样,UITableViewCell Set Selected Image

答案 2 :(得分:0)

是的,您可以在单元格中的任何位置添加它们。最后,它们应该是对您的应用程序有意义的大(或小)。

答案 3 :(得分:0)

迅速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()
}