如何管理单行中的多个标签,但没有。表视图中单元格的数量取决于 iPhone SDK 中的数组。
我想显示这样的输出:
对于使用了两个部分。第一个用于标题,第二个用于显示数据,但在日期列中,日期是动态的,因为它取决于解析数据,意味着没有行取决于该数组。
那么任何人都可以建议我,如何将多个标签放在单行中,以下是我的代码:在 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;
}
输出将根据第二张图片显示]1
I want to display output like this:
For that used two sections. 1st one is for heading, 2nd for displaying the data, but in date column dates are dynamic b'cos it depends on the parsing data means no of row is depends upon that array.
So can anyone suggest me, how can I put multiple labels in single row for that following is my code: in 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;
}
and outPut will appear according to 2nd image]1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好的方法是创建 CustomTableViewCell。
在 nib 文件中,可以轻松绘制所需的输出单元格(超过 1 个标签、添加图像等)。
从这里您可以获取示例。
对于您给出的示例,请创建两个自定义单元格 1 用于标题,1 用于显示所有数据。你的数据是 NSArray 的 NSArray 。
The best way to do this is to create CustomTableViewCell.
Where in the nib file one can easily draw the desired output Cell.(more than 1 label, adding image etc).
From here you can take the sample.
More for your give example, create two custom cells 1 for header and 1 for all data to show. Where as your data is NSArray of NSArray.