我遇到了cell.detailTextLabel.text = NULL的问题,就像我之前的其他人一样。我已经读过这些帖子,但无法解决我的问题。我使用的是storyboard,我的Table View使用Dynamic Prototypes作为内容。我有一个具有标识符" myCell"的原型单元格。我用Subtitle作为样式。我以前曾经做过几次这样的事情并且工作得像预期的那样,但这次出了问题。代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
MyClass *myClass = [self sectionNumber:indexPath.section rowNumber:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@", myClass.someProperty];
cell.detailTextLabel.text = @"Some text";
NSLog(@"%@", cell.detailTextLabel.text);
return cell;
}
我错过了什么? NSLog正在打印" NULL"。
汉克
答案 0 :(得分:1)
尝试这种方式:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * CellIdentifier = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
MyClass *myClass = [self sectionNumber:indexPath.section rowNumber:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@", myClass.someProperty];
cell.detailTextLabel.text = @"Some text";
NSLog(@"%@", cell.detailTextLabel.text);
return cell;
}
答案 1 :(得分:0)
嗯,我认为你错过了检查细胞是否为零
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"myCell"];
}
MyClass *myClass = [self sectionNumber:indexPath.section rowNumber:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@", myClass.someProperty];
cell.detailTextLabel.text = @"Some text";
NSLog(@"%@", cell.detailTextLabel.text);
return cell;
尝试使用上面的代码替换函数中的代码。
答案 2 :(得分:0)