我想显示如下输出:
对于那用了两个部分。第一个用于标题,第二个用于显示数据,但是在日期列中,日期是动态的,因为它取决于解析数据,这意味着行数不取决于该数组。
所以有人可以建议我,如何将多个标签放在一行中,以下是我的代码:在CellForRowAtIndexPath()中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
for (UIView * view in cell.contentView.subviews)
{
[view removeFromSuperview];
}
if (indexPath.section==0)
{
[cell addColumn:130];
label =[[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 64.0, 45)] autorelease];
//label.tag = LABEL_TAG;
label.font = [UIFont boldSystemFontOfSize:12];
label.text = @"Date";//[NSString stringWithFormat:@"%d", indexPath.row];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor blackColor];
label.backgroundColor=[UIColor brownColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
//[cell addColumn:200];
label1 =[[UILabel alloc] initWithFrame:CGRectMake(66, 0, 64.0, 45)] ;
//label1.tag = VALUE_TAG;
label1.font = [UIFont boldSystemFontOfSize:12];
// add some silly value
//label.text = [NSString stringWithFormat:@"%d", indexPath.row * 4];
label1.text = @"Gold";
label1.textAlignment = UITextAlignmentCenter;
label1.backgroundColor=[UIColor brownColor];
[cell.contentView addSubview:label1];
label2 =[[UILabel alloc] initWithFrame:CGRectMake(132.0, 0, 64, 45)] ;
//[cell addColumn:260];
label2.font = [UIFont boldSystemFontOfSize:12];
label2.text = @"Silver";
label2.textAlignment = UITextAlignmentCenter;
label2.backgroundColor=[UIColor brownColor];
[cell.contentView addSubview:label2];
label3 =[[UILabel alloc] initWithFrame:CGRectMake(198.0, 0, 62, 45)] ;
// [cell addColumn:350];
label3.font = [UIFont boldSystemFontOfSize:12];
label3.text =@"Bronze";
label3.textAlignment = UITextAlignmentCenter;
label3.backgroundColor=[UIColor brownColor];
[cell.contentView addSubview:label3];
label4 =[[UILabel alloc] initWithFrame:CGRectMake(262.0, 0, 61, 45)] ;
// [cell addColumn:350];
label4.font = [UIFont boldSystemFontOfSize:12];
label4.text =@"Avg Conf";
label4.textAlignment = UITextAlignmentCenter;
label4.backgroundColor=[UIColor brownColor];
[cell.contentView addSubview:label4];
}
else
{label5 = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 64.0, 150)];
label5.font = [UIFont boldSystemFontOfSize:12];
label5.numberOfLines=4;
label5.text = [arr objectAtIndex:indexPath.row];
label5.textAlignment = UITextAlignmentLeft;
label5.textColor = [UIColor blackColor];
label5.backgroundColor=[UIColor grayColor];
[cell.contentView addSubview:label5];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
并根据第二张图片显示outPut] 1
最佳答案
最好的方法是创建CustomTableViewCell。
在笔尖文件中,可以轻松地绘制所需的输出单元格(超过1个标签,添加图像等)。
From here you can take the sample.
为您的示例提供更多信息,为标题创建两个自定义单元格,为要显示的所有数据创建1个自定义单元格。其中,您的数据是NSArray的NSArray。