【问题标题】:custom uitableviewCell outlets return nil自定义 uitableviewCell 出口返回 nil
【发布时间】:2011-08-22 16:06:21
【问题描述】:

我正在构建一个使用自定义 tableView 单元格的应用程序,但我不确定是否正确: 例如,我不太确定 cellOwner...

基本问题是,我在 IB 中定义和连接的所有出口在运行时都为零。 这是我所做的:

  • 构建 UITableViewCell (ProductListTableViewCell) 的自定义子类
  • 制作了一个笔尖,包含一个 UITableViewCell 作为根元素
  • 将单元格的 className 和 owner Type 都设置为 ProductListTableViewCell
  • 设置重用标识符
  • 然后将几个标签等放在单元格上,并通过拖放生成出口
  • 仔细检查所有插座是否正确连接。

然而,出口都是零。 我做错了什么?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"ProductListTableViewCell";

ProductListTableViewCell *cell =(ProductListTableViewCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    ProductListTableViewCell *cellOwner = [[ProductListTableViewCell alloc]init];

    UINib* nib = [UINib nibWithNibName:@"ProductListTableViewCell" bundle:nil];

    cell =(ProductListTableViewCell*) [[nib instantiateWithOwner:cellOwner options:nil]objectAtIndex:0];

}
// all outlets are nil here

【问题讨论】:

  • 您是否为表格视图 xib 设置了相同的标识符 ProductListTableViewCell ?
  • 是 - 通过 IB 设置相同的标识符

标签: iphone uitableview


【解决方案1】:

试试这个

static NSString *CellIdentifier = @"ProductListTableViewCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"ProductListTableViewCell" owner:self options:nil];
    cell = ProductListTableViewCell;
    self.ProductListTableViewCell=nil;

}

【讨论】:

  • 好的。我试图通过 NSBundle 加载它 - 但失败了,因为我将 nil 作为所有者传递...
  • 将所有者设置为自定义表格单元格的另一个实例与通过 UINib 进行设置具有相同的效果 - 所有 Outlets 均为零。
  • 好的 - 我想不通,所以我选择了cell = [[[NSBundle mainBundle] loadNibNamed:@"TableCellBackgroundView" owner:self options:nil] lastObject];,因为我无法获得对网点的引用,所以我通过[((UIButton*)[cell viewWithTag:100]) setImage:[UIImage imageNamed:@"Default-Landscape~ipad"] forState:UIControlStateNormal] ;配置了它们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
  • 2015-05-31
  • 1970-01-01
相关资源
最近更新 更多