I am trying to use the new (since iOS8) feature for self-sizing UITableViewCells. Unfortunately it does not work right when using a Right Detail Cell.
我正在尝试使用新的(自从iOS8以来)特性来自定义尺寸的uitableviewcell。不幸的是,在使用正确的细节单元格时,它不能正常工作。
My Setup
我的设置
I am using a UITableViewController. Therefore a UITableViewCell is already configured. I set the UITableViewCell style to "Right Detail". Then I set the numberOfLines property of the detailTextLabel to 0 in order to grow when I provide a bunch of text.
我使用的是UITableViewController。因此,已经配置了UITableViewCell。我将UITableViewCell样式设置为“正确的细节”。然后,我将detailTextLabel的numberOfLines属性设置为0,以便在提供大量文本时实现增长。
Problem
问题
结果是这样的:
When I am changing the UITableViewCell to some other style like "Left Detail" it works:
当我将UITableViewCell更改为其他样式时,比如“左细节”,它可以工作:
It also works if I use a custom UITableViewCell subclass which I am using now but I am still wondering why the Right-Detail style does not work.
如果我使用自定义的UITableViewCell子类,它也可以工作,我现在正在使用它,但是我仍然想知道为什么Right-Detail样式不能工作。
Question
问题
Why is the self sizing UITableViewCell not working when using a Right-Detail cell style ?
为什么自定尺寸的UITableViewCell在使用右细节单元格样式时不能工作?
Also does anybody have a fix for this ?
还有谁能解决这个问题吗?
1 个解决方案
#1
1
I ran into the same problem.
我遇到了同样的问题。
Eventually I gave up on the "Right Detail" in favor of my own custom one:
最终我放弃了“正确的细节”,而选择了我自己的习惯:
-
I created my own simple class that inherited from the UITableViewCell:
我创建了自己的简单类,继承自UITableViewCell:
class TransactionCell: UITableViewCell { }
-
I set my prototype cell use that custom class by setting the "Style" field to "Custom" on the "Table View Cell" Menu and by adding "TransactionCell" to the "Class" field of the "Custom Class" menu. These menus are available when you select the prototype cell in the storyboard.
我通过将“样式”字段设置为“表视图单元格”菜单上的“自定义”字段,并将“事务单元格”添加到“自定义类”菜单的“类”字段,来设置原型单元格使用该自定义类。当您在故事板中选择原型单元格时,可以使用这些菜单。
-
I added two labels to my prototype cell and connected them to my custom class by right click dragging from my labels to my class (Oddly, I had to clean my build before it would let me do this):
我在原型单元格中添加了两个标签,并通过右键单击将它们从标签拖拽到类中,将它们连接到我的自定义类(奇怪的是,在它允许我这样做之前,我必须先清理构建):
class TransactionCell: UITableViewCell{ @IBOutlet weak var detailsLabel: UILabel! @IBOutlet weak var amountLabel: UILabel! }
-
I added new constraints to my labels, taking advantage of swift's autolayout features (You will need to set these to match your own requirements; see a tutorial on autolayout if you are stuck)
我在标签上添加了新的约束,利用了swift的自动布局特性(您需要设置这些约束以满足您自己的需求;如果您被困住了,请参阅自动布局教程)
- ...and set the "Lines" and "Line Breaks" fields in the respective "Label" menus so that the spacing between labels would be even and so that my details label could flex to multiple lines.
- …在相应的“Label”菜单中设置“Lines”和“Line break”字段,这样标签之间的间隔就会是均匀的,这样我的details标签就可以伸缩到多行。
It worked for me, allowing me to have the flexibility of different amounts of multiple lines in a UITableView in swift per cell, while formatting the word wrapping so that it looked nice and even, like I would have expected the "Right Detail" to do automatically.
它为我工作,允许我在一个UITableView中,在swift的每个单元中有不同数量的多行的灵活性,同时格式化单词包装,使它看起来很好,甚至,就像我希望“正确的细节”自动完成一样。
Please mark as the answer if this was what you were looking for :)
如果这是你想要的答案,请标记为:
#1
1
I ran into the same problem.
我遇到了同样的问题。
Eventually I gave up on the "Right Detail" in favor of my own custom one:
最终我放弃了“正确的细节”,而选择了我自己的习惯:
-
I created my own simple class that inherited from the UITableViewCell:
我创建了自己的简单类,继承自UITableViewCell:
class TransactionCell: UITableViewCell { }
-
I set my prototype cell use that custom class by setting the "Style" field to "Custom" on the "Table View Cell" Menu and by adding "TransactionCell" to the "Class" field of the "Custom Class" menu. These menus are available when you select the prototype cell in the storyboard.
我通过将“样式”字段设置为“表视图单元格”菜单上的“自定义”字段,并将“事务单元格”添加到“自定义类”菜单的“类”字段,来设置原型单元格使用该自定义类。当您在故事板中选择原型单元格时,可以使用这些菜单。
-
I added two labels to my prototype cell and connected them to my custom class by right click dragging from my labels to my class (Oddly, I had to clean my build before it would let me do this):
我在原型单元格中添加了两个标签,并通过右键单击将它们从标签拖拽到类中,将它们连接到我的自定义类(奇怪的是,在它允许我这样做之前,我必须先清理构建):
class TransactionCell: UITableViewCell{ @IBOutlet weak var detailsLabel: UILabel! @IBOutlet weak var amountLabel: UILabel! }
-
I added new constraints to my labels, taking advantage of swift's autolayout features (You will need to set these to match your own requirements; see a tutorial on autolayout if you are stuck)
我在标签上添加了新的约束,利用了swift的自动布局特性(您需要设置这些约束以满足您自己的需求;如果您被困住了,请参阅自动布局教程)
- ...and set the "Lines" and "Line Breaks" fields in the respective "Label" menus so that the spacing between labels would be even and so that my details label could flex to multiple lines.
- …在相应的“Label”菜单中设置“Lines”和“Line break”字段,这样标签之间的间隔就会是均匀的,这样我的details标签就可以伸缩到多行。
It worked for me, allowing me to have the flexibility of different amounts of multiple lines in a UITableView in swift per cell, while formatting the word wrapping so that it looked nice and even, like I would have expected the "Right Detail" to do automatically.
它为我工作,允许我在一个UITableView中,在swift的每个单元中有不同数量的多行的灵活性,同时格式化单词包装,使它看起来很好,甚至,就像我希望“正确的细节”自动完成一样。
Please mark as the answer if this was what you were looking for :)
如果这是你想要的答案,请标记为: