【问题标题】:How to inherit a XIB file in an iOS project?如何在iOS项目中继承XIB文件?
【发布时间】:2012-09-26 06:14:56
【问题描述】:

我有一个从 UITableViewCell 继承的自定义表格单元格。它工作正常。

现在我必须为其他一些屏幕制作相同类型的单元格,只需稍作修改。我打算继承我的旧单元类以重用其设计和逻辑,但我找不到如何继承 xib 文件的方法。

我应该将所有视图从一个 xib 复制/粘贴到另一个 xib 还是有办法通过少量更改来扩展它?

编辑:我已阅读以下问题,他们声称现在不可能:

如果不可能,我们应该使用什么方法来有效地做到这一点?

【问题讨论】:

    标签: iphone objective-c ios uitableview


    【解决方案1】:

    您应该复制该文件并进行必要的更改。

    谢谢

    【讨论】:

    • 在 OO 上下文中思考时,我试图避免重复;
    【解决方案2】:

    XIB 编辑器没有什么是您不能以编程方式完成的。最简单的方法是(通常)将其全部移动到代码中,然后从所有目标/项目中删除 XIB。然后,您可以根据自己的喜好进行子类化。

    另外,这可以真正简化您的多个目标的资源管理。

    将其移至代码的另一种方法是将可修改部分移至其他实例(由单元保存/引用)。然后控制器可以在呈现之前设置此属性。因此,如果样式化和绘图在某些方面是特殊的或可定制的,那么只需将该逻辑移到不依赖 XIB 反序列化的类层次结构中,然后将这些部分属性设置为您在 nib 中定义的单元格。

    【讨论】:

      【解决方案3】:

      您是否尝试过仅对自定义单元格视图类进行子类化?这样,您可以在所有 UI 组件的基类中拥有 IBOutlets,在新的子类中,您可以根据新行为随意管理它们。就像继承 UIView 或 UIViewController 等一样。

      【讨论】:

        【解决方案4】:

        这件事发生在我身上一次,我所做的是:

        我没有进行子类化,而是创建了一个单独的UIView,其中包含带有 customCell 的表,并在需要时将该视图添加为 subView。我能够使用 customCell 中的 key 更改 customCell 的 UI,以了解哪个 UIViewController 将其添加为子视图。

        我不确定这种方法有多好,但仍然是一种方法:)

        【讨论】:

          【解决方案5】:

          如果您正在为您的问题寻找其他解决方案,那么我建议您以编程方式创建一个自定义单元格。

          我正在一步一步地向你解释,这将帮助你更好。

          因此,为此创建一个继承自 UITableViewCell 的类,并在 tabelCell 中添加您需要的组件。

          你的 .h 文件看起来像这样。

          //--------开始-------->>

          #import <UIKit/UIKit.h>
          
          @interface CustomCell : UITableViewCell{
          
          UIButton *btn1;
          UIButton *btn2;
          UIButton *btn3;
          
          }
          
          @property(nonatomic , retain) UIButton *btn1;
          @property(nonatomic , retain) UIButton *btn2;
          @property(nonatomic , retain) UIButton *btn3;
          
          @end
          

          //-------END-------

          您的 .m 文件将如下所示

          //--------开始-------->>

            #import "CustomCell.h"
          
            @implementation CustomCell
            @synthesize btn1, btn2, btn3;
          
            - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
             {
              self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
              if (self) {
          
              //----frame the components as per your project req.----       
          
              btn1 = [[UIButton alloc] initWithFrame:CGRectMake(1, 1, 85, 70)];
              [self.contentView addSubview:btn1];
          
              btn2 = [[UIButton alloc] initWithFrame:CGRectMake(100, 1, 85, 70)];
              [self.contentView addSubview:btn2];
          
              btn3 = [[UIButton alloc] initWithFrame:CGRectMake(195,1, 85, 70)];
              [self.contentView addSubview:btn3];
             }
              return self;
             }
          
             - (void)setSelected:(BOOL)selected animated:(BOOL)animated
             {
              [super setSelected:selected animated:animated];
          
                // Configure the view for the selected state
             }
          

          //--------END--------

          现在您已准备好在其他类中重用此自定义单元格。也可以根据需要进行修改。

          现在在 cellForRowAtIndexPath >>

          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
          {
          
          static NSString *CellIdentifier = @"Cell";
          
          CustomCell  *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          
          
          if (cell == nil)
          {
              cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
          }
          
            [cell.btN1 setImage:[UIImage imageNamed:@"YOUR IMAGE NAME"] forState:UIControlStateNormal]; // YOU CAN MAKE CHANGES HERE.
          
              ................// remaining logic goes here
          
           return cell;
          
           }
          

          希望这会对您有所帮助。

          1 .Also take a look on link tutorial.

          2. A Closer Look at Table View Cells

          【讨论】:

          • 我认为您没有阅读我的问题;我已经有一个从 UITableViewCell 类继承的自定义单元格;我知道如何为 tableView 制作自定义单元格;我需要多一层继承,不是直接从 UITableViewCell 继承,而是从我自己的自定义单元格类继承,重要的是要重用 xib 文件视图;
          猜你喜欢
          • 2021-09-30
          • 1970-01-01
          • 2013-01-07
          • 2015-07-24
          • 2015-07-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-04
          相关资源
          最近更新 更多