【问题标题】:Expandable table view cell using auto layout is not working?使用自动布局的可扩展表格视图单元不起作用?
【发布时间】:2016-08-19 21:56:18
【问题描述】:

我尝试使用 tutorial 复制可扩展的 tableview 单元格

我会展示我所做的并指出我在这里缺少什么?!我被困在这里。如果您指出我错过的步骤会有所帮助!

表格视图控制器

class ExpandableCell: UITableViewController {

let titles = ["one" , "two"]    

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 125
//  tableView.tableFooterView = UIView(frame: CGRectZero)
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return titles.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableCell

    cell.title.text = titles[indexPath.row]
    cell.indexPath = indexPath


    switch expandedIndexPath {
    case .Some(let expandedIndexPath) where expandedIndexPath == indexPath:
        cell.showsDetails = true
    default:
        cell.showsDetails = false
    }

    return cell

}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    tableView.deselectRowAtIndexPath(indexPath, animated: false)

    switch expandedIndexPath {
    case .Some(_) where expandedIndexPath == indexPath:
        expandedIndexPath = nil
    case .Some(let expandedIndex) where expandedIndex != indexPath:
        expandedIndexPath = nil
        self.tableView(tableView, didSelectRowAtIndexPath: indexPath)
    default:
        expandedIndexPath = indexPath
    }

}

var expandedIndexPath: NSIndexPath? {
    didSet {
        switch expandedIndexPath {
        case .Some(let index):
            tableView.reloadRowsAtIndexPaths([index], withRowAnimation: UITableViewRowAnimation.Automatic)
        case .None:
            tableView.reloadRowsAtIndexPaths([oldValue!], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }
  }
}

TableViewCell

class TableCell: UITableViewCell {

@IBOutlet weak var title: UILabel!
@IBOutlet weak var descriptionConstraint: NSLayoutConstraint!
@IBOutlet weak var descriptionNew: UILabel!

//    let detailViewDefaultHeight: CGFloat = 44
let lowLayoutPriority: Float = 250
let highLayoutPriority: Float = 999


var showsDetails = false {
    didSet {
        descriptionConstraint.priority = showsDetails ? lowLayoutPriority : highLayoutPriority
    }
}

var indexPath = NSIndexPath()

override func awakeFromNib() {
    super.awakeFromNib()
    descriptionConstraint.constant = 0
  }
}

故事板

这是我得到的输出,不是我想要得到的,

【问题讨论】:

  • 还有什么问题???
  • 只显示一个带有标题和描述标签的表格,但没有得到预期的输出!!

标签: ios uitableview github nslayoutconstraint expandable-table


【解决方案1】:

根据您的代码,我认为您是否缺少以下方法:-

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        return UITableViewAutomaticDimension
    }

【讨论】:

  • 添加这个功能没有任何帮助!!您可以参考我提到的教程并帮助我解决问题。
【解决方案2】:

好像你已经按相反的顺序设置了低优先级和高优先级值

var showsDetails = false {
    didSet {
        descriptionConstraint.constant = showsDetails ? highLayoutPriority: lowLayoutPriority 
    }
}

【讨论】:

  • 顺序是正确的,如果我改变它不会展开。无论如何感谢您的建议。我想通了!!
猜你喜欢
  • 2018-07-16
  • 2016-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
相关资源
最近更新 更多