【问题标题】:Custom UITableViewCell only drawing one cell自定义 UITableViewCell 只绘制一个单元格
【发布时间】:2011-05-04 13:21:49
【问题描述】:

我想显示自定义单元格,但我的代码一次只显示一个自定义表格单元格.. 我做错了什么?

我在 UIView 内设置了一个 UIViewController 笔尖,其中包含我的 UITableView。 nib 中还有一个 UITableViewCell,它的类是 CustomCell(UITableViewCell 的子类)。 UITableView 和 Cell 都是@sythesized IBOutlet @properties。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier  = @"CellIdentifier";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // CustomCell is the class for standardCell
    if (cell == nil) 
    {
        cell = standardCell; // standardCell is declared in the header and linked with IB
    }
    return cell;
}

【问题讨论】:

    标签: iphone objective-c ios uitableview


    【解决方案1】:

    您可以使用下面的示例代码;

    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSString *CellIdentifier  = @"CellIdentifier";
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // CustomCell is the class for standardCell
        if (cell == nil) 
        {
        NSArray *objectList = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id currentObject in objectList) {
            if([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (CustomCell*)currentObject;
                break;
            }
        }
        }
        return cell;
    }
    

    【讨论】:

      【解决方案2】:

      你应该在每次dequeueReusableCellWithIdentifier返回nil时创建一个新单元格

      通常应该是这样的

      ...
      if (cell == nil)
      {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil] objectAtIndex:0]
      }
      ...
      

      附言而不是objectAtIbndex:,您可以遍历返回的数组并使用isKingOfClass:[MyCell class] 查找单元格

      【讨论】:

        【解决方案3】:

        cell 必须为给定的索引路径设置其内容,即使单元格本身已出列,例如:

        if (cell == nil) {
           /* instantiate cell or load nib */
           cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
        }
        
        /* configure cell for a given index path parameter */
        if (indexPath.row == 123)
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        else 
            cell.accessoryType = nil;
        /* etc. */
        
        return cell;
        

        【讨论】:

          【解决方案4】:

          如果是cell == nil,那么你需要实例化一个新的UITableViewCell

          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
              NSString *CellIdentifier  = @"CellIdentifier";
              CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          
              if (cell == nil) 
              {
                  // Instantiate a new cell here instead of using the "standard cell"
                  CustomCell *cell= [[[CustomCell alloc] init reuseIdentifier:CellIdentifier] autorelease];
          
                  // Do other customization here
          
              }
              return cell;
          }
          

          【讨论】:

            猜你喜欢
            • 2023-01-16
            • 1970-01-01
            • 2016-06-13
            • 1970-01-01
            • 2023-03-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多