简体   繁体   English

iOS-可扩展并重新排序TableView

[英]iOS - Expandable and reordering TableView

I'm developing an application in Swift 3, but I have a problem, because I want to make a table that: 我正在Swift 3中开发应用程序,但是有一个问题,因为我想制作一个表,该表:

  • When we click on a cell expand and see a custom content. 当我们单击一个单元格时,展开并看到自定义内容。
  • When we select a cell, we can change the order in which the table will be shown. 当选择一个单元格时,我们可以更改表格的显示顺序。

Well, I enclose the code of the "class of the legend" and the one of the "customCell": 好吧,我附上“图例类”和“ 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
}

}

And the custom cell is: 自定义单元格是:

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
    }
}

}

When I can reorder cells (uncoment line "tableSideLeft.isEditing = true ") I see that: 当我可以对单元格重新排序(不合要求的行“ tableSideLeft.isEditing = true”)时,我看到:

在此处输入图片说明

And when I comment the line "tableSideLeft.isEditing = true" you can see that: 当我评论“ tableSideLeft.isEditing = true”行时,您会看到:

在此处输入图片说明

The problem is that if I leave the line "tableSideLeft.isEditing = true" I can change the order of the table. 问题是,如果我保留“ tableSideLeft.isEditing = true”行,则可以更改表的顺序。 But, I disappear the action of expanding the cell. 但是,我消失了扩大细胞的动作。 But if I leave that line commented, if I may expand the cell, but not exchange the cells. 但是,如果我对这一行发表评论,是否可以扩展单元格,但不能交换单元格。

How can I make them do both :) ? 我怎样才能让他们都做:)?
Thanks!! 谢谢!!

I just posted an answer to expanding/collapsing table view cells last night on another question. 昨晚我刚刚发布了一个扩展/折叠表格视图单元格的答案,涉及另一个问题。 Theres a working sample project linked to on github as well: 在github上也有一个链接到的工作示例项目:

Dynamic Sizing and Collapsable TableViewCells 动态调整大小和可折叠TableViewCells

As for changing the order, that is usually done through entering "Edit mode" for a tableview controller. 至于更改顺序,通常是通过为tableview控制器输入“编辑模式”来完成的。 Editing mode includes support for insertion, deletion, and re-ordering controls. 编辑模式包括对插入,删除和重新排序控件的支持。 The functionality is mostly build-in, just needs a little code to set it up. 该功能主要是内置的,只需要一些代码即可进行设置。

To enter that mode, make the following call. 要进入该模式,请拨打以下电话。

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

The TableViewCell is where you configure the editing options. 您可以在TableViewCell上配置编辑选项。 In Interface builder, when you select a prototype cell, you can set the following in the inspector: 在“界面构建器”中,选择原型单元时,可以在检查器中设置以下内容:

  • an accessory to display in editing mode 在编辑模式下显示的附件
  • whether to indent when editing 编辑时是否缩进
  • whether to show re-order control 是否显示重新订购控制

That last one is what you'll want to make sure is checked. 最后一个是您要确保选中的内容。

The Apple tableView.setEditing docs give a decent overview in the description. Apple tableView.setEditing文档在说明中给出了不错的概述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM