将UIImageView添加到UITableViewCell?

问题描述:

我有真正的麻烦添加UIImageView到单个UITableView单元格。在我的故事板中,我有一个UITableViewController和四个原型动态单元。其中三个与正常的左侧细节单元一样好,并且它们工作正常。然而,其中一个我需要有一个UIImageView英寸将UIImageView添加到UITableViewCell?

所以我已经添加了所述视图的单元格,给单元格的自定义类与属性,所以我可以访问图像视图。

我有以下单元格: 标题单元格。 Image Cell。 URL单元格。 评论Cell。

表视图的内容根据用户是否想添加URL,注释或图像而改变。所以用户总是看到其中一个标题单元格。

这是我的代码,无论什么原因,我只是无法让UIImageView显示在该图像单元上。希望你能帮忙看看有什么不对,谢谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 

    //0 = Image 
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 0) 
    { 
     if (indexPath.row == 0) CellIdentifier = @"titleCell"; 
     if (indexPath.row == 1) CellIdentifier = @"imageCell"; 
     if (indexPath.row == 2) CellIdentifier = @"notesCell"; 
    } 
    //1 = URL 
    else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 1) 
    { 
     if (indexPath.row == 0) CellIdentifier = @"titleCell"; 
     if (indexPath.row == 1) CellIdentifier = @"urlCell"; 
     if (indexPath.row == 2) CellIdentifier = @"notesCell"; 
    } 
    //2 = Notes 
    else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 2) 
    { 
     if (indexPath.row == 0) CellIdentifier = @"titleCell"; 
     if (indexPath.row == 1) CellIdentifier = @"notesCell"; 
    } 

    ScrapbookImageDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[ScrapbookImageDetailCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; 
     cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    [cell setBackgroundColor:[UIColor colorWithRed:250.0f/255.0f green:250.0f/255.0f blue:250.0f/255.0f alpha:1.0f]]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; 

    cell.textLabel.adjustsFontSizeToFitWidth = YES; 

    cell.textLabel.text = [firstSection objectAtIndex:indexPath.row]; 
    cell.detailTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:13.0f]; 
    cell.detailTextLabel.numberOfLines = 0; 
    cell.accessoryView = nil; 

    switch (indexPath.section) 
    { 
     case 0: 
      switch (indexPath.row) 
     { 
      case 0: 
      {     
       cell.detailTextLabel.text = scrapbook.title; 
      } 
       break; 

      case 1: 
      { 
       //0 = Image 
       if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 0) 
       { 
        cell.detailTextLabel.text = nil; 
        cell.iv.image = [UIImage imageNamed:@"image.png"]; 
       } 
       //1 = URL 
       else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 1) 
       { 
        cell.detailTextLabel.text = scrapbook.url; 
       } 
       //2 = Notes 
       else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 2) 
       { 
        cell.detailTextLabel.text = scrapbook.notes; 
       } 
      } 
      break; 

      case 2: 
      { 
       cell.detailTextLabel.text = scrapbook.notes; 
      } 
      break; 

      default: 
       break; 
     } 
     break; 

     default: 
      break; 
    } 

    return cell; 
} 

为什么你改变标识符的值?你只需要做自定义单元格,并在行高度,检查它是否是你想要的行,并返回与图像的高度其他默认大小,在行方法的单元格中的n,再次检查所需的行,并添加图像你想添加其他使图像视图无

+0

我明白你的意思,但我使用不同的单元格标识符的原因是因为我有我的故事板中组成的自定义单元格,所以如果某个标识符用于某个indexPath,然后使用我的故事板中的单元格。基本上它可以让我更容易地使我的自定义单元格可视化。 –