细胞性质返回无



我创建了一个名为 GateCell的自定义uiaTityViewCell,其中我放置了一个标签和一个文本字段。
GateCell.h

@property (weak, nonatomic) IBOutlet UILabel *gateLabel;
@property (weak, nonatomic) IBOutlet UITextField *gateTextField;

GateTableViewController

- (void)viewDidLoad {
    [self.tableView registerClass:[GateCell class] forCellReuseIdentifier:@"cellIdentifier"];
}

最终在CellForrowatIndExpath方法中,我像这样使用了

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GateCell *cell = (GateCell *)[tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    cell.gateLabel.text = @"Gate";
    cell.gateTextField.text = @"Open Gate"
    return cell;
}

当我打印单元的描述时,我将获得以下内容。

<`GateCell`: 0x7b6bd790; baseClass = `UITableViewCell`; frame = (0 0; 320 44); layer = <CALayer: 0x7b6c2e60>> <br>

打印细胞描述 -> _ gatelabel:

nil

打印细胞描述 -> _ gateTextfield:

nil

为什么创建单元格时标签和Textfield返回零???

我以前在执行registerClass:forCellReuseIdentifier:并在tableView:cellForRowAtIndexPath:中进行dequeueReusableCellWithIdentifier:时遇到了麻烦。

我必须替换dequeueReusableCellWithIdentifier:和直接启动单元格,因为它已经制作了registerClass:forCellReuseIdentifier:

tableView:cellForRowAtIndexPath:中尝试

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //Create your custom cell GateCell the way it has to be done providing the 'reuseIdentifier'
    //With a standard UITableViewCell it should be :
    //UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
    GateCell *cell = [[GateCell alloc] init];
    cell.gateLabel.text = @"Gate";
    cell.gateTextField.text = @"Open Gate"
    return cell;
}

第一件事确保您从接口构建器连接了IBOutlet。如果没有,请连接IBOutlet。用于获取单元格的使用以下

UITableViewCell *cell = [tableView      dequeueReusableCellWithIdentifier:@"cellReuseIdentifier"];

通过调用

[self.tableView registerClass:[GateCell class] forCellReuseIdentifier:@"cellIdentifier"];

表视图将直接创建一个单元格,而不是从笔尖文件中创建一个单元格,因此您的IBOutlets不会设置(无处可从中设置它们)。

要么您应该注册笔尖而不是类,要么应该作为initWithStyle:reuseIdentifier:的一部分创建子视图。

您的其他评论说您正在使用故事板。在这种情况下,您应该将故事板中添加到GateCell中添加的单元格的类别设置,并将单元格标识符设置为cellIdentifier。然后,在代码中,您应该删除对[self.tableView registerClass:[GateCell class] forCellReuseIdentifier:@"cellIdentifier"];的呼叫

最新更新