将图像添加到UItableView

jas*_*son 20 iphone cocoa-touch uitableview uikit

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

小智 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;
}
Run Code Online (Sandbox Code Playgroud)

除非您在UITableViewDelegate中提供tableView:heightForRowAtIndexPath:方法,否则UITableViewCell的默认高度为44点,非视网膜显示屏上为44像素,视网膜显示屏上为88像素.