【问题标题】:Design a UIView as the tableview cell with separate xib file使用单独的 xib 文件将 UIView 设计为 tableview 单元格
【发布时间】:2013-10-16 10:23:39
【问题描述】:

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

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

【问题讨论】:

标签: iphone ios ios5 ios4 ios6


【解决方案1】:

首先你必须创建一个文件,你的单元类类的配置应该从 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;
}

【讨论】:

    【解决方案2】:

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

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

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

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

    谢谢。 如有任何疑问,请随时联系。

    【讨论】:

      【解决方案3】:

      您可以找到一个演示 here,并且在共享侧有一个 customcell 文件夹,希望您能够找到所有的东西并根据您的需要进行修复。

      【讨论】:

        【解决方案4】:
        1. 你需要继承UITableViewCell 然后你必须添加一个新的“视图”文件(参考下图)

        2. 删除.xib中的UIView文件并从object library添加Table View Cell

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

        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];
        }
        

        【讨论】:

        • @TheDoctor 我按照 xib @weheartswift.com/swifting-around 创建 customCell。在本教程中,他没有实现func ... heightForRowAtIndexPath。但是如果我不使用这个功能,我的单元格就会重叠。我的代码有什么问题?
        • @EmAdpres:单元格的默认高度为 44,如果您的自定义单元格的高度不同,您需要实现 heightForRowAtIndexPath 或在界面生成器中为表格视图设置单元格高度。
        • @TheDoctor 我在界面生成器中为表格视图设置了单元格高度,但它仍然重叠。我想我除了实现heightForRowAtIndexPath 之外别无选择,因为我的列表不是静态的according to this answer。我是对的还是我还有机会在界面生成器中设置单元格高度?
        • heightForRowAtIndexPath 用于在运行时动态改变单元格的高度。如果您希望 tableView 中的单元格具有不同的高度,则必须实现此方法。
        【解决方案5】:

        您可以创建一个单独的 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 版本。

        【讨论】:

        • 我编辑了它,不知何故最终得到了 2 个帖子。我已经删除了副本。
        【解决方案6】:

        cellForRowAtIndexPath 委托方法中,执行以下操作:

        - (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;
        }
        

        【讨论】:

        • 这是否也会加载自定义单元格?
        猜你喜欢
        • 2018-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-24
        相关资源
        最近更新 更多