如何从xib创建自定义uitableViewCell

时间:2010-11-16 15:25:30

标签: iphone uitableview custom-controls

我想创建一个自定义的TableViewCell,我想让UITextField具有编辑功能。 所以我用xib创建了新类。添加TableViewCell元素。在它上面拖动UITextField。在我班上添加了插座并将它们连接在一起。在我的TableView方法cellForRowAtIndexPath中,我创建了自定义单元格,但它们不是我的自定义单元格 - 它们只是常用的单元格。我该如何解决这个问题,为什么会这样?感谢名单!

// EditCell。 ħ

#import <UIKit/UIKit.h>


@interface EditCell : UITableViewCell
{
    IBOutlet UITextField *editRow;
}
@property (nonatomic, retain) IBOutlet UITextField *editRow;
@end

// EditCell.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 个答案:

答案 0 :(得分:14)

不要使用UITableViewCell的初始化程序,但要从笔尖加载单元格:

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

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

答案 1 :(得分:7)

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

第一个解决方案,没有子类:

在ViewController中:

•将单元格ivar定义为IBOutlet

UITableViewCell *tableViewCell;

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

@synthesize ...

在IB中:

•创建新的空NIB文件并在Interface Builder中打开

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

•自定义单元格,不要忘记标记添加的视图

•选择单元格并添加标识符(以后在tableView中使用:cellForRowAtIndexPath:)

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

•使用NIB中的单元格连接文件所有者的单元格出口

在ViewController中:

•在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的新子类<​​/ p>

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

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

- &GT;稍后将从IB

添加奥特莱斯

在IB中:

4.创建新的空XIB文件

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

6.从库中拖动TableView单元格

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

8.设置单元格的标识符属性//小心,与cellForRowAtIndexPath相同:

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

10.添加界面元素正确设置它们(设置类,...)

11.通过Ctrl-Drag创建所需的出口到CustomSubclass.h      - &GT;弱还是强? - &GT;弱的,仅强的顶级对象没有预定义的出口(即像“视图”)

在代码编辑器中:

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

答案 2 :(得分:3)

您需要加载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中,按照您的方式创建单元格,并使用标记检索textField。

相关问题