在选择 Cell 时在 UITableView 中显示 UIButton

Display UIButton in UITableView , when Cell is selected

我想添加一个 UIButton 到我的 UITableViewCell。我的单元格在被选中时会扩展高度。按钮必须仅在 didSelectRow 被调用时显示扩展区域 ... 我有 4 个单元格,我想用不同的问题、表格和按钮填充它们。我应该对每个单元格进行分class吗?

到目前为止,这是我的代码:

let SelectedCellHeight: CGFloat = 500.0
let UnselectedCellHeight: CGFloat = 44.0
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 4
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let logItemCell = tableView.dequeueReusableCellWithIdentifier("LogCell", forIndexPath: indexPath) as! UITableViewCell




}

// Used to expand cell when selected
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
    if let selectedCellIndexPath = selectedCellIndexPath {
        if selectedCellIndexPath == indexPath {
            return SelectedCellHeight
        }
    }
    return UnselectedCellHeight
}


func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

    if let selectedCellIndexPath = selectedCellIndexPath {
        if selectedCellIndexPath == indexPath {
            self.selectedCellIndexPath = nil
        } else {
            self.selectedCellIndexPath = indexPath
        }
    } else {
        selectedCellIndexPath = indexPath
    }
    tableView.beginUpdates()
    tableView.endUpdates()

}

是的,您应该根据 indexPath 在 'cellForRowAtIndexPath' 中制作 4 个具有不同标识符和 class 和 return 的原型单元格。

您应该子类化您想要的每个单元格。当 didSelectRow 被调用并且单元格被展开时 button.hidden = false 应该被调用