在表格单元格中添加图片(iOS)

9
我正在尝试做类似于推特或Facebook的事情。
3个回答

18
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{    
   static NSString* CellIdentifier = @"Cell";

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

   cell.textLabel.text = @"I'm a UITableViewCell!";
   cell.imageView.image = [UIImage imageNamed:@"MyReallyCoolImage.png"];

   return cell;
}

感谢Thomas Moore的贡献。

如何在UITableView中添加图片


1

要将图像添加到表格单元格中,您需要将UIImage添加到副标题UITableViewCell或自定义单元格的图像属性中。我建议阅读developer.apple.com上有关自定义UITableViewCells的文档。


0

Swift 版本:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

  let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

  cell.textLabel.text = "I'm a UITableViewCell!";
  cell.textLabel.textColor = UIColor.whiteColor()
  cell.imageView.image = UIImage(named: "MyReallyCoolImage.png")

  return cell;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接