表格单元格不显示分配给属性的字符串是否太长
问题描述:
我正在构建一个应用程序,让用户创建一个包含标题和文本的“故事”。表格单元格不显示分配给属性的字符串是否太长
我正在实现一个显示所有创建的故事的tableView。到目前为止,一切正常但这里是我的问题:
当用户输入一个标题或文本的长度是什么tableViewCell将能够显示,该单元根本不显示。 尽管如此,其他人名字较短。
我正在使用单元格样式“副标题”。
如何限制显示在单元格中的文本数量以及导致此错误的原因?因为即使我找到了修复方法,但屏幕上的文字仍然可能会出现问题。
这里是我的UITableViewController
类代码:
class StoryTableViewController: UITableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return savedStories.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
cell.textLabel?.text = savedStories[indexPath.row].title
cell.detailTextLabel?.text = savedStories[indexPath.row].text
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
这里是UI的从界面生成器的截图:
答
对于您需要使用可变高度TableViewCell
override func viewDidLoad()
{
super.viewDidLoad()
self.tableView.estimatedRowHeight = 200 // give maximum height you required
self.tableView.rowHeight = UITableViewAutomaticDimension
}
然后添加您的视图控制器中的此代表方法
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
答
您需要创建自定义UITableViewCell。您可以使用可用的动态可调整大小的单元格来自动调整单元格的文本长度。
IB步骤: 在单元格上生成一个UILabel。不要给它任何高度限制。刚刚从四面八方脚起来,做如下:
label.numberOfLines = 0
在viewDidLoad中:
self.tableView.estimatedRowHeight = 88.0 //Any estimated Height
self.tableView.rowHeight = UITableViewAutomaticDimension
不要写heightForRow:
方法,但如果你想使用它,因为现有的几个有细胞的,你可以为该特定单元格高度返回UITableViewAutomaticDimension。
试试这个out
答
您必须实现这两个代表,不要忘记的tableView委托和数据源绑定VC,并设置你的故事板标签说明财产numberOfLines = 0
。
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60; // height of default cell
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension; // It takes automatic height of cell
}
现在在故事板上做以下
您的视图层次应该是这样的
检查只ViewLabelContatainer
添加视图,并把所有的标签进入它。
标签集装箱约束上
标签名称约束
标签说明约束
输出
因此,如果用户输入1000个字的故事,你想显示在那个特定的细胞每一个字? –
**理想情况下,只有前x个字符。** 但我的tableView目前不显示任何内容,当文本太长。 – Marmelador
添加UI的屏幕截图。 –