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

如何从XIB创建自定义TableViewCell

  •  8
  • yozhik  · 技术社区  · 15 年前

    我想创建一个自定义的TableViewCell,我想在其上使用具有编辑功能的uitextfield。 所以我用XIB创建了新的类。添加TableViewCell元素。在其上拖动uitextfield。在我的课上增加了插座,并将它们连接在一起。在我的TableView方法cellForRowatindexPath中,我创建了我的自定义单元格,但它们不是我的自定义单元格——它们只是普通的单元格。我怎样才能解决这个问题,为什么会这样?谢谢!

    //编辑单元格。H

    #import <UIKit/UIKit.h>
    
    
    @interface EditCell : UITableViewCell
    {
        IBOutlet UITextField *editRow;
    }
    @property (nonatomic, retain) IBOutlet UITextField *editRow;
    @end
    

    //编辑单元格.m

    #import "EditCell.h"
    
    
    @implementation EditCell
    @synthesize editRow;
    
    #pragma mark -
    #pragma mark View lifecycle
    
    - (void)viewDidUnload 
    {
        // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
        // For example: self.myOutlet = nil;
        self.editRow = nil; 
    }
    @end
    

    //在我的代码中

    - (UITableViewCell *)tableView:(UITableView *)tableView 
             cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        static NSString *CellIdentifier = @"EditCell";
    
        EditCell *cell = (EditCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[[EditCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                    reuseIdentifier:CellIdentifier] autorelease];
        }
    cell.editRow.text = @"some text to test";
    return cell;
    }
    
    3 回复  |  直到 15 年前
        1
  •  14
  •   Björn Marschollek    15 年前

    不要使用UITableViewCell的初始值设定项,而是从NIB加载单元:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        static NSString *CellIdentifier = @"EditCell";
    
        EditCell *cell = (EditCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"YourNibNameHere" owner:self options:nil];
            cell = (EditCell *)[nib objectAtIndex:0];
        }
        cell.editRow.text = @"some text to test";
        return cell;
    }
    

    当然,您需要指定正确的笔尖名称。

        2
  •  7
  •   drct    13 年前

    您可以从NIB文件加载自定义的UITableViewCell,而无需首先创建UITableViewCell的子类,但通过子类,您可以配置有关该单元的更多信息。

    第一个解决方案,无子类:

    在视图控制器中:

    _穖将单元格ivar定义为iboutlet

    UITableViewCell *tableViewCell;
    
    @property (nonatomic, assign) IBOutlet UITableViewCell *tableViewCell;
    
    @synthesize ...
    

    在IB:

    创建新的空NIB文件并在接口生成器中打开

    将表格视图单元格从库拖动到文档窗口,然后双击将其打开

    定制单元格,别忘了标记添加的视图

    _窆选择单元格并添加标识符(供以后在TableView中使用:cellForRowatindexPath:)

    将文件的所有者设置为将加载此单元的控制器类

    将文件所有者的单元插座与NIB中的单元连接

    在视图控制器中:

    在TableView:CellForRowatindexPath中:

    static NSString * cellIdentifier = @"SameIdentifierAsInNIB";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"NibFileNameWithoutSuffix" owner:self options:nil];
        cell = tableViewCell;
        // Configure the cell
    
        self.tableViewCell = nil;
    }
    // Configure the cell
    

    准备就绪

    /**************************************************/

    第二种解决方案,子类:

    在代码编辑器中:

    1. 创建UITableViewCell的新子类

    2. 添加initwitcoder方法,添加自定义

    - (id)initWithCoder:(NSCoder *)aDecoder {
        self = [super initWithCoder:aDecoder];
        if (self) {
          // init magic here
          self.contentView.backgroundColor = [UIColor lightGrayColor];
        }
        return self;
    }
    

    3. 添加设置值的方法(如“SetupCellWith:”)

    - (id)setupCellWith:(NSDictionary *)someSetupDict {
    
      // more magic here
    }
    

    -->稍后将从ib添加插座

    在IB:

    4. 创建新的空XIB文件

    5. 更改文件的所有者=uiviewController

    6. 从库中拖动TableView单元格

    7. 将其类更改为自定义子类(请参见1)。

    8. 设置单元格的Identifier属性//此处小心,与CellForRowatindexPath中相同:

    9. 将文件所有者的视图出口连接到TableView单元格

    10. 添加接口元素,正确设置它们(set class,)

    11. 通过ctrl-drag-to-customsubclass.h创建所需的出口 --&弱还是强?-->弱,强,仅顶层对象,没有预定义的出口(例如“视图”)。

    在代码编辑器中:

    12. 自定义“TableView:CellForRowatindexPath:”

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifier = @"CustomIdentifier";
    
        CustomCellSubclass *cell = (CustomCellSubclass *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if (cell == nil) {
          //cell = [[CustomCellSubclass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
          UIViewController *tempController = [[UIViewController alloc] initWithNibName:@"CustomCellSubclassXIBName" bundle:nil];
          cell = (CustomCellSubclass *)tempController.view;
          //[tempController release]; // not needed with ARC
        }
        // Configure the cell...
          [cell setupCellWith:…];
    
        // do other setup magic here
    
        return cell;
    }
    
        3
  •  3
  •   ebany    15 年前

    您需要加载XIB并检索自定义单元:

    NSArray *uiObjects = [[NSBundle mainBundle] loadNibNamed:@"yourNib" 
                                                       owner:self 
                                                     options:nil];
    for (id uiObject in uiObjects) {
         if ([uiObject isKindOfClass:[EditCell class]]) {
              cell = (EditCell *) uiObject;
         }
    }
    

    还要确保您确实将XIB中的TableViewCell类更改为EditCell。 您还需要将TableView行高度更改为正确的大小。

    另一种方法是在EditCell类中以编程方式构建单元格,我相信这让您比InterfaceBuilder更自由、更精确:

    在editcell.m中:

    - (id)initWithStyle:(UITableViewCellStyle)style 
        reuseIdentifier:(NSString *)reuseIdentifier {
    
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            CGRect textFieldRect = CGRectMake(5, 5, 300, 30);
            UITextField *textField = [[UITextField alloc] initWithFrame:textFieldRect];
            textField.tag = kTextFieldTag;
            [self.contentView addSubview:textField];
            [textField release];
        }
        return self;
    }
    

    然后在TableViewController中,按照您的方式创建单元格,并使用标记检索文本字段。

    推荐文章