2016-04-26 46 views
0

我试着使用复制扩张的tableview细胞中,这种tutorial使用自动布局的可扩展表格视图单元格不起作用?

我会告诉我做了什么,并指出我错过什么吗?!我被困在这里。如果您指出我错过的步骤,这将有所帮助!

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

+0

设置低和高优先级价值观,什么是次问题? –

+0

只显示标题和说明标签的表格但未获得预期输出! –

回答

0

你的代码库,我认为是缺少吼叫方法: -

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

添加此功能没有任何帮助!你可以参考我提到的教程,并帮助我解决问题。 –

0

好像你已经按照相反的顺序

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

订单是正确的,如果我改变它不会扩大。无论如何感谢您的建议。我想到了!! –

+0

您应该添加解决方案作为未来读者的答案。 – UditS

相关问题