使用自动布局的可扩展表格视图单元格不起作用?

时间:2016-04-26 07:55:28

标签: ios uitableview github nslayoutconstraint expandable-table

我尝试使用此tutorial

复制可扩展的tableview单元格

我将展示我的所作所为,并指出我在这里失踪了什么?!!我被困在这里。如果你指出我错过的步骤会很有帮助。

TableviewController

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
  }
}

故事板

enter image description here

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

enter image description here

2 个答案:

答案 0 :(得分:0)

根据你的代码,我认为你错过了以下方法: -

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

答案 1 :(得分:0)

似乎您已按相反的顺序设置了低优先级值和高优先级值

var showsDetails = false {
    didSet {
        descriptionConstraint.constant = showsDetails ? highLayoutPriority: lowLayoutPriority 
    }
}
相关问题