【问题标题】:iOS - Expandable and reordering TableViewiOS - 可扩展和重新排序的 TableView
【发布时间】:2017-12-04 13:34:11
【问题描述】:

我正在用 Swift 3 开发一个应用程序,但是我遇到了一个问题,因为我想制作一个表格:

  • 当我们单击单元格时,展开并查看自定义内容。
  • 当我们选择一个单元格时,我们可以更改表格的显示顺序。

好吧,我附上“传奇类”和“customCell”之一的代码:

class LeftSideViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var selectedIndex = -1
//var menuItems:[String] = ["RGB","PCB", "PCD Zonificado","PCD Arbol","Target Sectors", "Variability Map"]

@IBOutlet weak var tableSideLeft: UITableView!
@IBAction func opacityDelivery(_ sender: UISlider) {

    print(sender.value)

}

override func viewDidLoad() {
    super.viewDidLoad()
    tableSideLeft.isEditing = true

}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return snapShotsLegend.legendEntries[0].deliverables.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! customCell


        cell.firstViewLabel.text = snapShotsLegend.legendEntries[0].deliverables[indexPath.row].type
        cell.secondViewLabel.text = "Option " + snapShotsLegend.legendEntries[0].deliverables[indexPath.row].type
        cell.idDeliveryResponse.text = snapShotsLegend.legendEntries[0].deliverables[indexPath.row].options[0].id
        cell.initialMaxDeliveryResponse.text = String(snapShotsLegend.legendEntries[0].deliverables[indexPath.row].options[0].initial_max_value)
        cell.initialMinDeliveryResponse.text = String(snapShotsLegend.legendEntries[0].deliverables[indexPath.row].options[0].initial_min_value)
        cell.maxRangeDeliveryResponse.text = String(snapShotsLegend.legendEntries[0].deliverables[indexPath.row].options[0].max_range)
        cell.minRangeDeliveryResponse.text = String(snapShotsLegend.legendEntries[0].deliverables[indexPath.row].options[0].min_range)

        cell.backgroundColor = UIColor.init(red: 170/255, green: 170/255, blue: 170/255, alpha: 1)
        return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if(selectedIndex == indexPath.row){
        return 300
    }else{
        return 60
    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if(selectedIndex ==  indexPath.row){
        selectedIndex = -1
    }else {
        selectedIndex = indexPath.row
    }

    self.tableSideLeft.beginUpdates()
    self.tableSideLeft.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
    self.tableSideLeft.endUpdates()
}


//MARK: FUNCTIONS ALLOWS REORDERING OF CELLS
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let item = snapShotsLegend.legendEntries[0].deliverables[sourceIndexPath.row]
    snapShotsLegend.legendEntries[0].deliverables.remove(at: sourceIndexPath.row)
    snapShotsLegend.legendEntries[0].deliverables.insert(item, at: destinationIndexPath.row)
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return UITableViewCellEditingStyle.none
}

}

自定义单元格是:

import UIKit

class customCell: UITableViewCell {

//MARK: OUTLETS VIEW 1
@IBOutlet weak var firstView: UIView!
@IBOutlet weak var firstViewLabel: UILabel!
@IBOutlet weak var swichtActiveLayer: UISwitch!

@IBOutlet weak var secondView: UIView!
@IBOutlet weak var secondViewLabel: UILabel!
@IBOutlet weak var secondHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var idDeliveryResponse: UILabel!
@IBOutlet weak var minRangeDeliveryResponse: UILabel!
@IBOutlet weak var maxRangeDeliveryResponse: UILabel!
@IBOutlet weak var initialMinDeliveryResponse: UILabel!
@IBOutlet weak var initialMaxDeliveryResponse: UILabel!

@IBAction func sliderOpacity(_ sender: UISlider) {

    print(sender.value)
}
//MARK: OUTLETS VIEW 2
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

var showsDetails = false {
    didSet {
        secondHeightConstraint.priority = showsDetails ? 250 : 900
    }
}

}

当我可以重新排序单元格时(取消注释行“tableSideLeft.isEditing = true ") 我明白了:

当我评论“tableSideLeft.isEditing = true”这一行时,你可以看到:

问题是,如果我离开“tableSideLeft.isEditing = true”行,我可以更改表格的顺序。但是,我消失了扩大单元格的动作。但是,如果我将该行注释掉,我是否可以扩展单元格,但不能交换单元格。

我怎样才能让他们两者兼得:)?
谢谢!!

【问题讨论】:

    标签: ios swift uitableview custom-cell


    【解决方案1】:

    昨晚我刚刚在另一个问题上发布了关于展开/折叠表格视图单元格的答案。在 github 上也链接了一个工作示例项目:

    Dynamic Sizing and Collapsable TableViewCells

    至于更改顺序,通常是通过进入 tableview 控制器的“编辑模式”来完成。编辑模式包括对插入、删除和重新排序控件的支持。该功能主要是内置的,只需要一些代码来设置它。

    要进入该模式,请进行以下调用。

    self.tableView.setEditing(editing, animated: animated)
    

    TableViewCell 是您配置编辑选项的地方。在 Interface builder 中,当您选择一个原型单元格时,您可以在检查器中设置以下内容:

    • 在编辑模式下显示的附件
    • 编辑时是否缩进
    • 是否显示重新排序控制

    最后一个是您要确保检查的内容。

    Apple tableView.setEditing docs 在描述中给出了一个不错的概述。

    【讨论】:

    • 是的!这就是解决方案!谢谢
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 2017-06-26
      • 2022-01-07
      相关资源
      最近更新 更多