【问题标题】:Make 'Read More' button in tableview cell在表格视图单元格中制作“阅读更多”按钮
【发布时间】:2016-07-30 11:20:47
【问题描述】:

我有一个简单的表格视图,其中包含动态单元格。

每个单元格有 3 个元素(question as UILabel - Answer as UITextView - readmore as UIButton

我只需要在每个单元格中显示来自答案的 2 行,当我单击阅读更多按钮时,单元格的高度应根据答案文本视图字符动态变化。

这是我的调用表单元格方法:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            tableCell = myTable.dequeueReusableCellWithIdentifier("fqaCell") as! fqaTableViewCell
             myTable.estimatedRowHeight = 100
            myTable.rowHeight = 100





            return tableCell!
        }

【问题讨论】:

    标签: ios iphone swift tableview


    【解决方案1】:

    首先,您不应该从cellForRowAtIndexPath 方法调用rowHeight,因为这将为每个单元格调用并应用于表格视图中的所有单元格。

    改用tableView:heightForRowAtIndexPath:tableView:estimatedHeightForRowAtIndexPath: 方法配置单个单元格。

    其次,您应该为到目前为止所做的尝试付出一些努力,但我想尝试这些方法会给您一个提示,告诉您如何完成您的要求。

    【讨论】:

      【解决方案2】:

      要实现这一点,首先在 viewDidLoad 方法中将 rowHeight 初始化为 UITableViewAutomticDimension 并将estimatedRowHeight 初始化为 100(根据您的代码)。这将有助于根据其内容调整单元格的大小。现在,如果要显示标签的整个文本,则将标签的 numberOfLine 设置为 0,否则 2.要实现此目的,请维护一个字典或数组来存储布尔状态所有单元格的阅读更多按钮。单击按钮时切换其值,然后重新加载 tableview 或重新加载相应的单元格。希望这会帮助你。 :)

      【讨论】:

        【解决方案3】:

        首先为 textview 设置高度约束,例如 => 80

        并且还限制答案字符串的计数,同时将其设置为 textView.. 以便在阅读更多内容时我们可以获得实际的全长答案.. 将高度约束拖到表格视图单元格.. 并点击阅读更多按钮这样做

        //code
        @IBAction func readMoreBtn(_ sender: UIButton) {
        
        
                let indexPath = IndexPath(row: sender.tag, section: 0)
                let cell = tableView.cellForRow(at: indexPath) as! AnswersCell
                if(cell.answerTextView.text.count < answerList[sender.tag].answer.count){
        
        
                    cell.answerHeightConstraints.isActive = false
        
                    tableView.beginUpdates()
                    cell.answerTextView.addConstraint(NSLayoutConstraint(item:cell.answerTextView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.greaterThanOrEqual, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 80))
        
        
               cell.answerTextView.text = alertsFeedList[sender.tag].answer
        
                    cell.readMoreBtnOfCell.isHidden = true
                    tableView.endUpdates()
        
        
                }
        

        您必须将按钮拖到单元格作为出口,并在表格视图中作为操作。在cellForRowAt 这样做cell.readMoreBtnOfCell.tag = indexPath.row

        【讨论】:

        • tableView.beginUpdates() 和 tableView.endUpdates() 成功了,谢谢
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-19
        • 1970-01-01
        • 1970-01-01
        • 2014-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多