【问题标题】:Table View Cell not showing if string assigned to a property is too long如果分配给属性的字符串太长,表格视图单元格不显示
【发布时间】:2017-04-20 04:51:00
【问题描述】:

我正在构建一个应用程序,让用户创建一个由标题和文本组成的“故事”。

我正在实现一个显示所有已创建故事的 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 屏幕截图:

【问题讨论】:

  • 那么,如果用户输入 1000 个单词的故事并且您想在该特定单元格中显示每个单词?
  • 理想情况下只有前 x 个字符。 但是当文本太长时,我的 tableView 目前不显示任何内容。
  • 添加ui截图。
  • 用输出检查我的答案。

标签: ios swift uitableview tableviewcell uitableviewautomaticdimension


【解决方案1】:

您需要创建自定义 UITableViewCell。您可以使用可用的动态调整大小的单元格自动调整单元格以适应文本长度。

IB 步骤: 在单元格上制作一个 UILabel。不要给它任何高度限制。只需从四面八方将其固定并执行以下操作:

label.numberOfLines = 0

在 viewDidLoad 中:

self.tableView.estimatedRowHeight = 88.0 //Any estimated Height
self.tableView.rowHeight = UITableViewAutomaticDimension

不要编写heightForRow: 方法,但如果你想使用它,因为那里存在多个单元格,你可以返回那个特定单元格高度的 UITableViewAutomaticDimension。

试试这个out

【讨论】:

    【解决方案2】:

    你必须实现这两个委托,别忘了用 VC 绑定 tableView 委托和数据源,并从 storyboard 中设置标签描述属性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

    添加一个视图并将所有标签放入其中。

    标签容器约束

    标签标题限制

    标签描述约束

    输出

    【讨论】:

      【解决方案3】:

      为此,您需要使用可变高度的 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
      }
      

      【讨论】:

        猜你喜欢
        • 2021-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多