代码之家  ›  专栏  ›  技术社区  ›  Arunkumar

将UIView设计为具有单独xib文件的表视图单元格

  •  4
  • Arunkumar  · 技术社区  · 11 年前

    我需要设计一个UIView作为带有单独xib文件的表视图单元格。 我还尝试创建一个单独的xib,并将其视图设计为与表视图单元格类型相匹配。 但它失败了,有什么编程指南可以创建自定义的、可重复使用的表视图单元格创建吗?

    创建自定义表视图单元格后,我们如何将其添加到表视图中?

    6 回复  |  直到 11 年前
        1
  •  8
  •   Ankur Arya    11 年前
    1. 您需要子类 UITableViewCell 然后你必须添加一个新的“视图”文件(参见下图) enter image description here

    2. 删除 UIView 文件在 .xib 并添加 Table View Cell 从…起 object library .

    3.为单元格设置自定义类

    enter image description here

    CustomCell *cell = (CustomCell*)[tableView    dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];
    }
    
        2
  •  1
  •   tarmes    11 年前

    您可以创建一个单独的NIB来存储您的表格单元格。在viewDidLoad中,为每种单元格类型注册您的Nib文件:

    #define kMyCellIdentifier @"kMyCellIdentifier"
    
    ....
    
    - (void)viewDidLoad
    {
      UINib *tableCellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
      [self.tableView tableCellNib forCellReuseIdentifier:kMyCellIdentifier];
    }
    

    然后,您可以在需要时创建该单元格。由于您已经注册了Nib,如果还没有要排队的单元格,iOS将为您处理创建单元格的操作:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMyCellIdentifier];
    
      // Configure cell...
    
      return cell;
    }
    

    你也可以直接在IB中的Storyboards中创建原型单元格,但如果你有一个通用的应用程序,那就很麻烦了,因为你需要维护同一单元格的iPad和iPhone版本。

        3
  •  1
  •   D-eptdeveloper    11 年前

    你可以找到一个演示 here 在side shared中有一个customcell文件夹,希望你能找到所有的东西,并根据你的需要进行修复。

        4
  •  1
  •   Shreyansh Shah    11 年前

    在委托方法的cellforrowindexpath方法中,添加如下:-

    UIView *myView = [UIView alloc]init]; //or initwithframe
    myview.frame = cell.contentView.frame;
    

    如果要确定,则设置背景颜色

    myview.backgroundColor = [UIColor yellowColor];  //Or any other color.
    [cell addsubview :myview];
    

    非常感谢。 如果您有任何疑问,请随时联系。

        5
  •  0
  •   Mutawe    11 年前

    cellForRowAtIndexPath delegate方法,请执行以下操作:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"customCellIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customCellIdentifier"];
    
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
    
        UIView *view =[[UIView alloc]init];
        view.frame = cell.contentView.frame;
    
        view.backgroundColor = [UIColor redColor];//To be sure that the custom view in the cell
        [cell addSubview:view];
    
        return cell;
    }
    
        6
  •  0
  •   Jekil Patel    11 年前

    首先,你必须创建一个文件,其中你的单元格类的配置必须从UITableViewCell类中写入。创建一个只有表视图单元格的XIB文件。然后将你的自定义单元格类文件导入到视图或视图控制器中,并实现以下方法。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];
                YOUR_CUSTOM_CELL_CLASS * cell = (YOUR_CUSTOM_CELL_CLASS *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                return [self createCustomCell:cell cellForRowAtIndexPath:indexPath];
    }
    
    
    -(YOUR_CUSTOM_CELL_CLASS *)createCustomCell:(YOUR_CUSTOM_CELL_CLASS *)cell cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    if (cell == nil)
        {
            cell = (YOUR_CUSTOM_CELL_CLASS *)[[[NSBundle mainBundle] loadNibNamed:@"YOUR_CUSTOM_CELL_CLASS_XIB_FILE_NAME" owner:self options:nil] objectAtIndex:0];
        }
        return cell;
    }