如何在swift中设置tableview手风琴?

时间:2016-03-07 06:47:22

标签: ios swift accordion

我是IOS的新手,并试图设计一个手风琴菜单。我被困了......首先,我让我的UI有一些静态内容(表视图和视图中的3行标签..)。同时尝试设置手风琴。 .didnt显示任何回复..

这里是我的viewcontroller.swift

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    let previndexPath = selectedIndexPath
    if indexPath == selectedIndexPath
    {
        selectedIndexPath = nil
    }
   else
    {
        selectedIndexPath = indexPath
    }

    var indexPaths:Array <NSIndexPath> = []
    if let previous = previndexPath
    {
        indexPaths += [previous]
    }
    if let current = selectedIndexPath
    {
        indexPaths += [current]
    }
    if indexPaths.count > 0
    {
        tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation:UITableViewRowAnimation.Automatic)
    }

}


func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    (cell as! TableViewCell).watchFrameChanges()
    (cell as! TableViewCell2).watchFrameChanges()
}

 func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
 {
    (cell as!TableViewCell).ignoreFrameChanges()
    (cell as!TableViewCell2).ignoreFrameChanges()
}
override func viewWillDisappear(animated: Bool)
{
    super.viewWillDisappear(animated)
    for cell in tblvw.visibleCells as! [TableViewCell] {
        cell.ignoreFrameChanges()
    }
   for cell in tblvw.visibleCells as! [TableViewCell2] {
            cell.ignoreFrameChanges()
    }
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath == selectedIndexPath {
        return TableViewCell.expandedHeight

    } else {
       return TableViewCell.defaultHeight

 }


    if indexPath == selectedIndexPath {
        return TableViewCell2.expandedHeight
   } else {
       return TableViewCell2.defaultHeight
    }
   return 0.0

}

1 个答案:

答案 0 :(得分:0)

我有同样的问题。问题是何时实现多个单元格。解决方案是:

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)


    for cell in tableView.visibleCells {

        if(cell.reuseIdentifier == "YOUR_IDENTIFIER_FROM_CELL")
        {
            let ncelda = cell as! TableViewCell
            ncelda.ignoreFrameChanges()
        }

        else
        {
            let mcelda = cell as! TableViewCell2
            mcelda.ignoreFrameChanges()
        }
    }
}