如何在iOS中创建多行表格单元格? [英] How do I create a multiline table cell in iOS?

查看:135
本文介绍了如何在iOS中创建多行表格单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让第二个单元格扩展以适应文本而不是缩放文本?在iOS中是否有这样做的内置方式或者我是否需要提出一些自制的解决方案?如果你查看iOS联系人应用程序,就会有一个类似于此框的地址。我找不到如何实现这一点。

How can I get the second cell to expand to fit the text rather than scaling the text? Is there a built in way of doing this in iOS or will I have to come up with some home-cooked solution? If you look in the iOS Contacts application, there's a box like this for Address. I can't find how to implement this though.

对于那些希望将来实现这一目标的人来说,这是我的解决方案的代码:

For anyone looking to achieve this in future, here's the code for my solution:

HEADER文件:

#define FONT_SIZE 22.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 5.0f

实施文件:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0 && indexPath.row == 1) {

        NSString *text = [atmAnnotation address];

        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
        NSLog(@"Size for address is Height:%f Width:%f",size.height,size.width);

        CGFloat height = MAX(size.height, 44.0f);

        return height + (CELL_CONTENT_MARGIN * 2);  

    }
    return 44.0f;
}

以下是结果截图:

推荐答案

不幸的是,您必须自己实现此功能。您需要使用各种方法和回调来计算行和标签的高度。如果您需要一些帮助入门,我可以修改此答案以包含一些示例代码。否则,我确信在SO或Google上有一些相关问题可以帮助您入门。但总结一下:

Unfortunately, you are going to have to implement this feature yourself. There are a variety of methods and callbacks you need to make use of to calculate the height of the rows and labels. If you need some help getting started, I can amend this answer to include some sample code. Otherwise, I'm sure there are some related questions here on SO or Google that can get you started. In summary, however:


  • 确定 UITableViewDelegate 方法中行的高度 -tableView:heightForRowAtIndexPath:。您可能需要使用 NSString UIKit添加方法用于计算字符串与给定字体大小等的高度。从此委托方法返回正确的高度。

  • 确保您的标签(在IB中)或以编程方式配置)设置为使用多行。如果将 numberOfLines 设置为0,标签将根据需要使用尽可能多的行。

  • UITableViewDataSource中 -tableView:cellForRowAtIndexPath:的实现,您需要使用与以前相同的逻辑来确定标签的框架。设置所需的文本,然后更改框架,使标签足够高,几乎不适合所有文本。

  • Determine the height of the row in the UITableViewDelegate method -tableView:heightForRowAtIndexPath:. You'll probably need to use the NSString UIKit Addition methods to calculate how tall your string will be with a given font size, etc. Return the proper height from this delegate method.
  • Make sure your label (either in IB or configured programmatically) is set to use multiple lines. If you set numberOfLines to 0, the label will use as many lines as necessary.
  • In your UITableViewDataSource implementation of -tableView:cellForRowAtIndexPath:, you'll need to use the same logic as before to determine the frame of your labels. Set the text you want, then change the frame so that the label is tall enough to just barely fit all the text.

这篇关于如何在iOS中创建多行表格单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆