如何在UITableView的单个单元格中插入多行数据?

发布于 2024-11-09 07:20:51 字数 130 浏览 0 评论 0原文

我能知道如何将多行数据插入到 UITableView 的单个单元格中吗? 就像,我希望单个单元格中的输出如下:

KPK

Developer

Infosystems

INDIA

Can I know how can we insert more than one line of data into a single cell of a UITableView,
Just Like, I want the output in a single cell to be like :

KPK

Developer

Infosystems

INDIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

沫雨熙 2024-11-16 07:20:51
            UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 80)];
            myLabel.text = @"KPK";
            myLabel.textAlignment = UITextAlignmentCenter;
            myLabel.textColor = [UIColor yellowColor];
           [cell addSubview:myLabel];

           UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 201, 200, 80)];
            myLabel1.text = @"Developer";
            myLabel1.textAlignment = UITextAlignmentCenter;
            myLabel1.textColor = [UIColor yellowColor];
            [cell addSubview:myLabel1];
            UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 80)];
            myLabel.text = @"KPK";
            myLabel.textAlignment = UITextAlignmentCenter;
            myLabel.textColor = [UIColor yellowColor];
           [cell addSubview:myLabel];

           UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 201, 200, 80)];
            myLabel1.text = @"Developer";
            myLabel1.textAlignment = UITextAlignmentCenter;
            myLabel1.textColor = [UIColor yellowColor];
            [cell addSubview:myLabel1];
七颜 2024-11-16 07:20:51

使用 \n 就像在字符串中使用一样。

numberOfLines 设置为 4 以允许任意数量的行。

label.numberOfLines = 4;

使用 sizeWithFont: 更新标签框架以匹配文本大小。如果您不这样做,您的文本将垂直居中或被截断。

UILabel *label; // set frame to largest size you want
...
CGSize labelSize = [label.text sizeWithFont:label.font
                      constrainedToSize:label.frame.size
                          lineBreakMode:label.lineBreakMode];
label.frame = CGRectMake(
label.origin.x, label.orgin.y, 
label.size.width, labelSize.height);
label.text=[NSString stringWithFormat:@"KPK\nDeveloper\nInfosystems\nIndia"];  

现在您只需在单元格中添加标签即可。

[cell addSubView:label];

Use \n as you are using in your string.

Set numberOfLines to 4 to allow for any number of lines.

label.numberOfLines = 4;

Update the label frame to match the size of the text using sizeWithFont:. If you don't do this your text will be vertically centered or cut off.

UILabel *label; // set frame to largest size you want
...
CGSize labelSize = [label.text sizeWithFont:label.font
                      constrainedToSize:label.frame.size
                          lineBreakMode:label.lineBreakMode];
label.frame = CGRectMake(
label.origin.x, label.orgin.y, 
label.size.width, labelSize.height);
label.text=[NSString stringWithFormat:@"KPK\nDeveloper\nInfosystems\nIndia"];  

Now you just need to add the label in your cell.

[cell addSubView:label];
眉黛浅 2024-11-16 07:20:51

您可以将 \n 放在文本中。不确定这是否有效,否则您可以为多行记录创建自定义单元格。

you can place \n in text.Not sure if that works else you can make custom cell for multiple line records.

寻找我们的幸福 2024-11-16 07:20:51

设置表格行高度并在表格视图中使用标签

set tablerow height and Use label in your tableview

无尽的现实 2024-11-16 07:20:51

创建一个自定义单元格,在其中添加 4 个标签,以显示您所需的所有信息。查看以下教程,了解如何制作自定义单元格。

http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/

Create a custom cell, add 4 labels in it, to display all of your required info. Checkout the following tutorial to see how you can make custom cell.

http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/

吹梦到西洲 2024-11-16 07:20:51

如果您的表格视图单元格使用 uilabel 并将其添加为子视图。您可以指定 uilabel 的行数

   label.numberOfLines = 4;

Use a uilabel and add that one as the subview if your tableview cell.you can specify the number of lines of uilabel

   label.numberOfLines = 4;
野稚 2024-11-16 07:20:51

我认为最好的方法是创建自定义 UITableViewCell 实现,或以编程方式将 UILabel 添加到单元格的 contentView 中。

即:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 30)];
label.text = @"MyText";
label.textColor = [UIColor blackColor];
[cell.contentView addSubview:label];
[label release]; // Never forget this!

添加附加行时,您应该更新第一行定义以正确偏移新行。例如。第二个标签应该具有如下内容:

UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 35, 200, 30)];

完成后,您应该在表的委托中实现 tableView:heightForRowAtIndexPath: 方法,因为您必须为新单元格定义正确的高度。在您的示例中,对于 4 个标签,您应该实现如下所示的操作:

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath)indexPath {
    if (is_one_of_my_modified_cells) {
        return 35*4 + 5;
    }

    return 44; // default height
}

尝试一下并让我知道。

I think that the best way is creating a custom UITableViewCell implementation, or programmatically adding UILabels to the cell's contentView.

I.e.:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 30)];
label.text = @"MyText";
label.textColor = [UIColor blackColor];
[cell.contentView addSubview:label];
[label release]; // Never forget this!

When adding the additional rows you should update the first line definition to correctly offset the new rows. Eg. the second label should have something like this:

UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 35, 200, 30)];

When you are done, you should implement the tableView:heightForRowAtIndexPath: method in the table's delegate, because you have to define the correct height for the new cell. In your example, for 4 labels you should implement something like this:

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath)indexPath {
    if (is_one_of_my_modified_cells) {
        return 35*4 + 5;
    }

    return 44; // default height
}

Try it and let me know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文