2012-11-09 40 views
1

我遇到了一些我的代码问题。我非常积极的有一个非常简单的解决方案,我似乎无法看到它!基本上我使用单个tableview类来显示不同的数据(内容,项目,移动)。我没有3个不同的表格视图控制器,而是有一个跟踪哪个单元被点击并显示相关数据。所有作品都非常好!我正在使用一个单独的nib文件的自定义单元类,所以我需要正确实现一个if语句(或一个switch语句)来检查什么单元被挖掘(self.title),然后创建基于此的自定义单元。将多个自定义单元格动态加载到单个表格视图中

很明显,我遇到的问题是范围,cell.imageView不知道它在引用什么,因为它在if语句中超出了范围。

我一直在使用

id cell; 

我得到同样的错误尝试。什么是我完成这个任务的一种方式,以便每个“标题”使用不同的自定义单元格?

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

      static NSString *CellIdentifier = @"Cell"; 


      if (title == @"Content" || title == @"Favorite Content") 
      { 
       ContentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

       if (cell == nil) { 
        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"ContentCell" owner:self options:nil]; 

        for (id currentObject in objects) { 
         if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
          cell = (ContentCell *)currentObject; 

          break; 
         } 
        } 
       } 

      } 
      else if (title == @"Items" || title == @"Favorite Items") 
      { 
       ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

       if (cell == nil) { 
        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"ItemCell" owner:self options:nil]; 

        for (id currentObject in objects) { 
         if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
          cell = (ItemCell *)currentObject; 

          break; 
         } 
        } 
       } 

      } 
      else if (title == @"Moves" || title == @"Favorite Moves") 
      { 
       MoveCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

       if (cell == nil) { 
        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"MoveCell" owner:self options:nil]; 

        for (id currentObject in objects) { 
         if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
          cell = (MoveCell *)currentObject; 

          break; 
         } 
        } 
       } 

      } 

      cell.imageView.image = [UIImage imageNamed:[[self.theData objectAtIndex:indexPath.row] objectForKey:@"image"]]; 
      cell.contentName.text = [[self.theData objectAtIndex:indexPath.row] objectForKey:@"name"]; 

      return cell; 

    } 

任何帮助将是伟大的!我敢肯定,我的盯着屏幕的时间已经让我错过了一些简单的事情。谢谢!

+0

你必须改变CellIdentifier值。每次你设置相同的值,那就是。 – priyanka

回答

4

每次使用新的自定义单元格时,必须更改NSString *CellIdentifier;值。你必须把相同的值,其中输入

enter image description here

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

NSString *CellIdentifier; 
UITableViewCell *cell; 

if (title == @"Content" || title == @"Favorite Content") 
{ 
    CellIdentifier = @"Cell"; 
    ContentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"ContentCell" owner:self options:nil]; 

     for (id currentObject in objects) { 
      if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
       cell = (ContentCell *)currentObject; 

       break; 
      } 
     } 
    } 

} 
else if (title == @"Items" || title == @"Favorite Items") 
{ 
    CellIdentifier = @""; 
    ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"ItemCell" owner:self options:nil]; 

     for (id currentObject in objects) { 
      if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
       cell = (ItemCell *)currentObject; 

       break; 
      } 
     } 
    } 

} 
else if (title == @"Moves" || title == @"Favorite Moves") 
{ 
    CellIdentifier = @""; 
    MoveCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"MoveCell" owner:self options:nil]; 

     for (id currentObject in objects) { 
      if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
       cell = (MoveCell *)currentObject; 

       break; 
      } 
     } 
    } 

} 

cell.imageView.image = [UIImage imageNamed:[[self.theData objectAtIndex:indexPath.row] objectForKey:@"image"]]; 
cell.contentName.text = [[self.theData objectAtIndex:indexPath.row] objectForKey:@"name"]; 

return cell; 

}

+0

谢谢你的非常详细的回应!这非常有帮助;然而,我仍然有相同的错误:如果我尝试在这些if语句之外的任何地方使用我的单元格,请使用未声明的标识符“单元格”。 –

+0

@KyleBegeman请检查我编辑我的代码。 – priyanka

相关问题