【发布时间】:2016-06-15 05:49:22
【问题描述】:
来自以下 json
details = (
(
{
subtitle = "One";
title = 1;
},
{
subtitle = "two";
title = "2";
},
{
subtitle = "three";
title = "3";
}
),
(
{
subtitle = "one_two";
title = "1_2";
},
{
subtitle = "two_two";
title = "2_2";
}
)
);
我尝试了以下代码,但没有显示任何 UILables
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] ;
NSUInteger row = [indexPath row];
int i = 0;
NSUInteger nLabels = [[mainArray objectAtIndex:indexPath.row ] count];
UILabel *label;
float x =self.tableView.frame.size.width/nLabels;
for (i = 0; i < nLabels; i++) {
label = [[UILabel alloc] initWithFrame:CGRectMake(10 + (i * x), cell.frame.origin.y, 40, cell.frame.size.height)] ; //replace 40 with desired label width
label.textColor = [UIColor grayColor];
label.backgroundColor = [UIColor clearColor];
// set the label text here
label.text=@"treWT";
[label setNumberOfLines:0];
[label setAdjustsFontSizeToFitWidth:YES];
[cell.contentView addSubview:label];
}
}
return cell;
}
仍然没有对齐 请帮助我
【问题讨论】:
-
nLabels的值是多少? -
temp i 这样的硬编码值 " label.text=@"treWT"; '
-
首先添加调试以查看您的单元格是否为零。如果它不为零,则永远不会调用配置单元代码请将配置单元的代码粘贴到
if (!cell)子句之外。它不会运行里面的代码,因为单元格不是 nil 。 -
为什么人们会忽略弃用警告?有一个出列方法可以保证返回一个单元格。使用它!
标签: ios objective-c json uitableview