我无法从自定义单元格的xib文件中获取内容,单元格为空,单元格.xib文件仅包含UILabel,然后Master类中的代码(在Master-Detail项目中)如下:< / p>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
//[self configureCell:cell atIndexPath:indexPath];
return cell;
}
其中self configureCell用于Core Data,为了测试我的UILabel,我不想从Core Data获取值,所以我评论了这一行。 故事板中的“自定义类”设置为“MyCell”,标识符是相同的......
我也在我的班级MyCell中试过这个:
- (void)awakeFromNib {
// Initialization code
self.ibLabel.text = @"allo";
}
但我的单元格仍然是空的... .xib中每个单元格的高度也与代码中的高度相同...
编辑#1:
这适用于注册单元格自定义类:
UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"Cell"];
答案 0 :(得分:1)
确保在viewDidLoad中注册nib:
self.tableView.registerNib(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "Cell")
答案 1 :(得分:0)
为什么不尝试使用单元格forRow:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.ibLabel.text = @"allo";
return cell;
如果这不起作用,你必须确保&#34; Cell&#34;也是故事板中的标识符,在故事板中,该类是MyCell。
首先,一个愚蠢的问题:你有没有实现这个方法并且没有返回0?
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5; // To test
}
第二: 在storyboaard中更改自定义单元格的de backgroundColor,以确保是这个单元格绘制。
答案 2 :(得分:0)
您必须初始化单元格。它可能是第一个单元格而你之前还没有... ...
如果现有单元格可用,则此方法会出现,或者根据先前注册的类或nib文件创建新单元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell;
cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if (cell == nil)
{
cell = [[MyCell alloc] init];
cell.ibLabel.text = @"allo";
}
return cell;
}