在swift中轻点扩展和收缩tableview单元格

Die*_*cci 13 uitableview swift

我试图在点击时扩展tableview单元格.然后,当再次点击它时,我希望它恢复到原始状态.

如果cellA被扩展并且tapB被轻击,我希望cellA收缩并且cellB同时扩展.这样在任何给定时刻只有一个细胞可以处于扩张状态.

我目前的代码:

class MasterViewController: UITableViewController {
   var isCellTapped = false
   var currentRow = -1


   override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        selectedRowIndex = indexPath
        tableView.beginUpdates()
        tableView.endUpdates()
    }


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

    if indexPath.row == selectedRowIndex.row {
        if isCellTapped == false {
            isCellTapped = true
            return 140
        } else if isCellTapped == true {
            isCellTapped = false
            return 70
        }

    }

    return 70
}
Run Code Online (Sandbox Code Playgroud)

当前代码适用于:

  • 你点了一排(它扩大了)
  • 你再次点击它(它收缩)

失败的时候:

  • 你点了一排(它扩大了)
  • 你点击另一行(它收缩,但另一行不扩展)

我怎么解决这个问题?

Vic*_*ler 28

您需要考虑在点击其他行时需要更新所选行,请参阅以下代码:

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

    if indexPath.row == selectedRowIndex {
        return 140
    }
    return 44
}

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

    if selectedRowIndex != indexPath.row {

        // paint the last cell tapped to white again
        self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: self.selectedRowIndex, inSection: 0))?.backgroundColor = UIColor.whiteColor()

        // save the selected index 
        self.selectedRowIndex = indexPath.row

        // paint the selected cell to gray
        self.tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.grayColor()

        // update the height for all the cells
        self.tableView.beginUpdates()
        self.tableView.endUpdates()
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

要处理选中并再次点击的单元格返回其原始状态,您需要检查以下条件:

var thereIsCellTapped = false
var selectedRowIndex = -1

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

    if indexPath.row == selectedRowIndex && thereIsCellTapped {
        return 140
    }

    return 44
}

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

    self.tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.grayColor()

    // avoid paint the cell is the index is outside the bounds
    if self.selectedRowIndex != -1 {
        self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: self.selectedRowIndex, inSection: 0))?.backgroundColor = UIColor.whiteColor()
    }

    if selectedRowIndex != indexPath.row {
        self.thereIsCellTapped = true
        self.selectedRowIndex = indexPath.row
    }
    else {
        // there is no cell selected anymore
        self.thereIsCellTapped = false
        self.selectedRowIndex = -1
    }

    self.tableView.beginUpdates()
    self.tableView.endUpdates()
}
Run Code Online (Sandbox Code Playgroud)

通过对您didSelectRowAtIndexPathheightForRowAtIndexPath函数的上述修改,您可以看到当单元格被点击其背景颜色时,当它的高度增长时它将变为灰色,当点击另一个单元格时,单元格被绘制为白色并且轻敲为灰色并且一次又一次地只允许一次点击一个单元格.

我虽然你可以受益并学习如何在我创建的这个回购中做一个手风琴菜单,我计划很快更新以处理更好的结果,它可以使用你想要的处理.UITableView

您可以在此处发布存储库中的任何疑问.

我希望这对你有帮助.


Mar*_*kov 5

一次仅扩展一个单元格的简单方法:

迅捷3

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {  

var selectedRowIndex = -1  
@IBOutlet weak var tableView: UITableView!

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == selectedRowIndex {
            return 90 //Expanded
        }
        return 40 //Not expanded
    }

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if selectedRowIndex == indexPath.row {
            selectedRowIndex = -1
        } else {
            selectedRowIndex = indexPath.row
        }
        tableView.reloadRows(at: [indexPath], with: .automatic)
    }
}  
Run Code Online (Sandbox Code Playgroud)

此代码将展开单击的单元格并使用动画关闭以前打开的单元格。