来自以下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;
}
仍然没有对齐 请帮助我
答案 0 :(得分:5)
尝试以下代码进行标签对齐:
NSUInteger row = [indexPath row];
int i = 0;
NSUInteger nLabels = [[mainArray objectAtIndex:indexPath.row ] count];
UILabel *label;
float w =self.tableView.frame.size.width/nLabels;
float x = 0;
for (i = 0; i < nLabels; i++) {
label = [[UILabel alloc] initWithFrame:CGRectMake(x, cell.frame.origin.y, w, 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];
label.textAlignment = NSTextAlignmentCenter;
[cell.contentView addSubview:label];
x += w;
}
答案 1 :(得分:0)
删除if(!cell)
,因为单元格不是nil
。它不会执行条件。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] ;
int row = [indexPath row];
int i = 0;
int nLabels = [[rowData objectAtIndex:row] count];
UILabel *label;
for (i = 0; i < nLabels; i++) {
label = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.origin.x + (i * 40), 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"; //temp text
[cell.contentView addSubview:label];
}
return cell;
}