如何从xib创建自定义tableViewCell

yoz*_*hik 8 iphone custom-controls uitableview

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

// EditCell.H

#import <UIKit/UIKit.h>


@interface EditCell : UITableViewCell
{
    IBOutlet UITextField *editRow;
}
@property (nonatomic, retain) IBOutlet UITextField *editRow;
@end
Run Code Online (Sandbox Code Playgroud)

//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
Run Code Online (Sandbox Code Playgroud)

//在我的代码中

- (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;
}
Run Code Online (Sandbox Code Playgroud)

Bjö*_*lek 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;
}
Run Code Online (Sandbox Code Playgroud)

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

  • 仍然崩溃((( (2认同)

drc*_*rct 7

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

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

在ViewController中:

•将单元格ivar定义为IBOutlet

UITableViewCell *tableViewCell;

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

@synthesize ...
Run Code Online (Sandbox Code Playgroud)

在IB中:

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

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

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

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

•将File's Owner设置为将加载此单元格的控制器类

•将文件所有者的单元插座与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
Run Code Online (Sandbox Code Playgroud)

搞定

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

第二个解决方案,带子类:

在代码编辑器中:

1. 创建UITableViewCell的新子类

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

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

3. 添加设置值的方法(如"setupCellWith:")

- (id)setupCellWith:(NSDictionary *)someSetupDict {

  // more magic here
}
Run Code Online (Sandbox Code Playgroud)

- >稍后将从IB添加奥特莱斯

在IB中:

4. 创建新的空XIB文件

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

6. 从库中拖动TableView单元格

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

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

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

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

11.通过Ctrl-Drag创建所需的出口到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;
}
Run Code Online (Sandbox Code Playgroud)