如何在没有自定义单元格的情况下将文本换行到 UITableViewCell 中
这是在 iPhone 0S 2.0 上。 2.1 的答案也很好,尽管我不知道有关表格的任何差异。
感觉应该可以在不创建自定义单元格的情况下让文本换行,因为默认情况下 UITableViewCell
包含 UILabel
。 我知道如果我创建一个自定义单元格,我可以让它工作,但这不是我想要实现的目标 - 我想了解为什么我当前的方法不起作用。
我发现标签是根据需要创建的(因为单元格支持文本和图像访问,因此在必要时它不会创建数据视图),所以如果我这样做:
cell.text = @""; // create the label
UILabel* label = (UILabel*)[[cell.contentView subviews] objectAtIndex:0];
那么我会得到一个有效的标签,但设置 numberOfLines
(和 lineBreakMode)不起作用 - 我仍然得到单行文本。 UILabel
中有足够的高度用于显示文本 - 我只是在 heightForRowAtIndexPath
中返回一个较大的高度值。
This is on iPhone 0S 2.0. Answers for 2.1 are fine too, though I am unaware of any differences regarding tables.
It feels like it should be possible to get text to wrap without creating a custom cell, since a UITableViewCell
contains a UILabel
by default. I know I can make it work if I create a custom cell, but that's not what I'm trying to achieve - I want to understand why my current approach doesn't work.
I've figured out that the label is created on demand (since the cell supports text and image access, so it doesn't create the data view until necessary), so if I do something like this:
cell.text = @""; // create the label
UILabel* label = (UILabel*)[[cell.contentView subviews] objectAtIndex:0];
then I get a valid label, but setting numberOfLines
on that (and lineBreakMode) doesn't work - I still get single line text. There is plenty of height in the UILabel
for the text to display - I'm just returning a large value for the height in heightForRowAtIndexPath
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这是一种更简单的方法,它对我有用:
在您的
cellForRowAtIndexPath:
函数中。 第一次创建单元格时:您会注意到我将标签的行数设置为 0。这让它可以使用所需的行数。
下一部分是指定
UITableViewCell
的大小,因此请在heightForRowAtIndexPath
函数中执行此操作:我将 20 添加到返回的单元格高度,因为我喜欢周围有一点缓冲区我的文字。
Here is a simpler way, and it works for me:
Inside your
cellForRowAtIndexPath:
function. The first time you create your cell:You'll notice that I set the number of lines for the label to 0. This lets it use as many lines as it needs.
The next part is to specify how large your
UITableViewCell
will be, so do that in yourheightForRowAtIndexPath
function:I added 20 to my returned cell height because I like a little buffer around my text.
如果我们只在
UITableView
单元格中添加文本,则只需要两个委托即可使用(无需添加额外的UILabels
)1)
cellForRowAtIndexPath
2)
heightForRowAtIndexPath
这个解决方案对我有用:-
这里字符串
mutArr
是一个可变数组,我从中获取数据。编辑:- 这是我获取的数组。
If we are to add only text in
UITableView
cell, we need only two delegates to work with (no need to add extraUILabels
)1)
cellForRowAtIndexPath
2)
heightForRowAtIndexPath
This solution worked for me:-
Here the string
mutArr
is a mutable array from which i am getting my data.EDIT :- Here is the array which I took.
更新了 Tim Rupe 对 iOS7 的回答:
Updated Tim Rupe's answer for iOS7:
一个简短的评论/回答,记录我遇到同样问题时的经历。 尽管使用了代码示例,表视图单元格高度正在调整,但单元格内的标签仍然无法正确调整 - 解决方案是我从自定义 NIB 文件加载单元格,这发生在之后单元格高度已调整。
我在 NIB 文件中设置了不换行文本,并且只有 1 行用于标签; NIB 文件设置覆盖了我在代码中调整的设置。
我学到的教训是确保始终牢记对象在每个时间点的状态 - 它们可能还没有被创建! ……有人在线下。
A brief comment / answer to record my experience when I had the same problem. Despite using the code examples, the table view cell height was adjusting, but the label inside the cell was still not adjusting correctly - solution was that I was loading my cell from a custom NIB file, which happens after the cell height in adjusted.
And I had my settings inside the NIB file to not wrap text, and only have 1 line for the label; the NIB file settings were overriding the settings I adjusted inside the code.
The lesson I took was to make sure to always bear in mind what the state of the objects are at each point in time - they might not have been created yet! ... hth someone down the line.
现在,表格视图可以具有自动调整大小的单元格。 设置表视图如下
tableView.estimatedRowHeight = 85.0 //使用适当的估计
tableView.rowHeight = UITableViewAutomaticDimension
苹果参考
Now the tableviews can have self-sizing cells. Set the table view up as follows
tableView.estimatedRowHeight = 85.0 //use an appropriate estimate
tableView.rowHeight = UITableViewAutomaticDimension
Apple Reference
我使用以下解决方案。
数据在成员中单独提供:
可以在 cellForRowAtIndexPath 中轻松完成处理。
定义单元格/定义字体并将这些值分配给结果“单元格”。
请注意,
numberoflines
设置为“0”,这意味着取需要的内容。在
heightForRowAtIndexPath
中,我计算了换行文本的高度。边框尺寸应与单元格的宽度相关。 对于 iPad,该值为 1024。
适用于 iPhone 和 iPod 320。
I use the following solutions.
The data is provided separately in a member:
The handling can be easily done in
cellForRowAtIndexPath
.Define the cell / define the font and assign these values to the result "cell".
Note that the
numberoflines
is set to "0", which means take what is needed.In
heightForRowAtIndexPath
, I calculate the heights of the wrapped text.The boding size shall be related to the width of your cell. For iPad this shall be 1024.
For iPhone en iPod 320.
我发现这非常简单和直接:
例如:
这可能是也可能不是最好/标准的方法,但它在我的情况下有效。
I found this to be quite simple and straightForward :
for e.g. :
This may or may not be the best / standard approach to do so, but it worked in my case.
尝试我的代码 swift 。 这段代码也适用于普通的 UILabels。
现在像这样简单地调用
Try my code in swift . This code will work for normal UILabels also.
Now call simply like this
我认为这是一个更好、更短的解决方案。 只需格式化单元格的
UILabel
(textLabel
) 即可通过指定sizeToFit
自动计算高度,一切都应该没问题。I think this is a better and shorter solution. Just format the
UILabel
(textLabel
) of the cell to auto calculate for the height by specifyingsizeToFit
and everything should be fine.我认为您无法操作基本
UITableViewCell's
私有UILabel
来执行此操作。 您可以自己向单元格添加一个新的UILabel
,并使用numberOfLines
和sizeToFit
来适当调整其大小。 就像是:I don't think you can manipulate a base
UITableViewCell's
privateUILabel
to do this. You could add a newUILabel
to the cell yourself and usenumberOfLines
withsizeToFit
to size it appropriately. Something like: