自定义UITableViewCell不使用.xib(最可能因为init方法中的缺陷)

我将UITableViewCell子类化为自定义它,但我认为我错过了一些东西,因为:1)它不起作用2)有一些我很困惑的事情。 除了自定义.xib文件的外观之外,我还更改了backgroundView,这部分工作正常。 我最不理解/最困惑的部分是init方法,所以我在这里发布了。 如果事实certificate这是正确的,请告诉我,以便我可以发布更多可能是原因的代码。

这是我定制的init方法。 我对“风格”的想法很困惑,我想我只是返回一个带有不同backgroundView的普通UITableViewCell。 我的意思是,那里没有任何东西引用.xib或做任何事情,只是从自己改变.backgroundView:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier wait: (float) wait fadeOut: (float) fadeOut fadeIn: (float) fadeIn playFor: (float) playFor { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { CueLoadingView* lview = [[CueLoadingView alloc] initWithFrame:CGRectMake(0, 0, 320, 53)]; self.backgroundView = lview; [self setWait:wait]; // in turn edits the lview through the backgrounView pointer [self setFadeOut:fadeOut]; [self setFadeIn:fadeIn]; [self setPlayFor:playFor]; } return self; } 

除了.xib和几个setter和getter之外,这是我的代码中唯一真正的部分,它与检索单元格有关。

附加信息:

1)这是我的.xib,它与class级相关联。 在此处输入图像描述

2)这是调用/创建UITableView(委托/视图控制器)的代码:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"CueTableCell"; CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { cell = [[CueTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier wait:5.0 fadeOut:1.0 fadeIn:1.0 playFor:10.0]; [cell updateBarAt:15]; } return cell; } 

在nib文件中创建自定义表格视图单元格的最简单方法(自iOS 5.0起可用)是在表格视图控制器中使用registerNib:forCellReuseIdentifier: . 最大的优点是dequeueReusableCellWithIdentifier:然后在必要时自动从nib文件中实例化一个单元格。 你不再需要if (cell == nil) ... part。

在您添加的表视图控制器的viewDidLoad

 [self.tableView registerNib:[UINib nibWithNibName:@"CueTableCell" bundle:nil] forCellReuseIdentifier:@"CueTableCell"]; 

你可以在cellForRowAtIndexPath完成

 CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CueTableCell"]; // setup cell return cell; 

从nib文件加载的单元格使用initWithCoder进行实例化,如果需要,可以在子类中覆盖它。 要修改UI元素,您应该覆盖awakeFromNib (不要忘记调用“super”)。

您必须从.xib加载单元格:

 if ( cell == nil ) { cell = [[NSBundle mainBundle] loadNibNamed:@"CellXIBName" owner:nil options:nil][0]; } // set the cell's properties 
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"CueTableCell"; CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CueTableCell XibName" owner:self options:nil]; // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). cell = [array objectAtIndex:0]; } return cell; } 
Interesting Posts