如何在选择单元格时更改单元格的高度?

时间:2016-03-25 19:11:05

标签: ios swift tableview

我已经四处寻找其他答案,但似乎无法正常工作。我已经实现了这段代码:

var selectedCellIndexPath: NSIndexPath?
let selectedCellHeight: CGFloat = 88.0
let unselectedCellHeight: CGFloat = 51.0

// Override to support editing of individual itemCells

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if selectedCellHeight == indexPath {
        return selectedCellHeight
    }
    return unselectedCellHeight
}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath {
        self.selectedCellIndexPath = nil
    } else {
        self.selectedCellIndexPath = indexPath
    }

    tableView.beginUpdates()
    tableView.endUpdates()

    if selectedCellIndexPath != nil {
        // THis ensure, that the cell is full visile once expended 
        tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .None, animated: true)
    }
}

我从其他StackOverFlow答案中得到了它,但是当我运行我的应用程序时,单元格只显示它被选中(通过着色),然后不执行任何其他操作。任何帮助都会很棒!我是开发人员!

1 个答案:

答案 0 :(得分:2)

if selectedCellHeight == indexPath {

应该是

if selectedCellIndexPath == indexPath {