选择单元格时,在UITableView中显示UIButton

时间:2023-02-10 21:08:12

I Want to add a UIButton to my UITableViewCell.My cells expands in height when it is selected.. The button must only be displayed when didSelectRow is called in the expanded area ... I have 4 cells that I want to populate with different questions, forms and buttons. Should I sub-class each cell?

我想在UITableViewCell中添加一个UIButton。我的细胞在被选择的时候会膨胀。只有当在展开区域中调用didSelectRow时才会显示按钮……我有4个单元格,我想填充不同的问题,表单和按钮。我应该对每个单元格进行子类化吗?

This is my code so far:

这是我目前的代码:

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()

}

2 个解决方案

#1


0  

You should subclass each cell that you want. And when didSelectRow is called and the cell is expanded button.hidden = false should be called

您应该对您想要的每个单元格进行子类化。当didSelectRow被调用时,单元格被展开。应该调用hidden = false

#2


2  

Yes, you should make 4 prototype cells with different identifier and class and return the one you want in 'cellForRowAtIndexPath' depending on the indexPath.

是的,您应该创建4个具有不同标识符和类的原型单元格,并根据indexPath在“cellForRowAtIndexPath”中返回您想要的单元格。

#1


0  

You should subclass each cell that you want. And when didSelectRow is called and the cell is expanded button.hidden = false should be called

您应该对您想要的每个单元格进行子类化。当didSelectRow被调用时,单元格被展开。应该调用hidden = false

#2


2  

Yes, you should make 4 prototype cells with different identifier and class and return the one you want in 'cellForRowAtIndexPath' depending on the indexPath.

是的,您应该创建4个具有不同标识符和类的原型单元格,并根据indexPath在“cellForRowAtIndexPath”中返回您想要的单元格。