对 viewController 中自定义单元格的单元格输入进行排序

Sort cell input from customized cell in viewController

我有一个 ViewController,其中集成了一个 table 视图和一个按钮 "sort"。 此 tableView 的单元格在另一个名为 "customizedCell" 的 class 中自定义。

加载 ViewController 时,tableView 中的标签(riskTitle:UITextView!在 CellCustomized 中)将填充数组中存储的项目(RiskTitles_Plan = String ViewController)。我下面的代码为此数组硬编码了一些值。

我现在要做的是将标签"riskFactor: UILabel!"中的两个pickerView生成的数字存储在一个数组中(RiskFactor_Plan -> RiskFactor_Int)。当用户单击按钮排序时,必须对我的数组中的值进行排序,并且 table 视图中的行将按新顺序加载(从小到大,反之亦然)。

这是我的代码(删除不必要的代码)。

Swift 3 - ViewController:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var RiskTitles_Plan = [String]()
    var RiskFactor_Plan = [String]()
    var RiskFactor_Plan_Int = [Int]()

    var sortBtn = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()

        RiskTitles_Plan = ["my cat","my dog","my sheep","my cow","my fish"]

        sortBtn.setImage(UIImage(named: "Sort"), for: UIControlState.normal)
        sortBtn.addTarget(self, action: #selector(RiskPlan.sortAction(_:)), for: .touchUpInside)
        self.view.addSubview(sortBtn)
    }

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return RiskTitles_Plan.count
    }

 /////// Here comes the interesting part ///////

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView!.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CellCustomized

        RiskFactor_Plan.append(cell.riskFactor.text!)
        cell.riskTitle?.text = RiskTitles_Plan[indexPath.row]
        cell.backgroundColor = myColorsClass.darkCyan()

        for i in RiskFactor_Plan_Int {
            let stringItem: String = String(i)
            RiskFactor_Plan.append(stringItem)
        }

        cell.riskFactor.text! = RiskFactor_Plan[indexPath.row]

        return cell
    }

    func sortAction(_ sender:UIButton!) {

        for i in RiskFactor_Plan {
            let intItem: Int = Int(i)!
            RiskFactor_Plan_Int.append(intItem)
        }

        RiskFactor_Plan_Int = RiskFactor_Plan_Int.sorted{ [=11=] <  }
        tableView.reloadData()
    }
}

Swift 3 - CellCustomized:

import UIKit

class CellCustomized: UITableViewCell {

    var myColorsClass = myColors()
    var myStylesClass = myStyles()

    @IBOutlet weak var riskTitle: UITextView!
    @IBOutlet weak var riskFactor: UILabel!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier) // the common code is executed in this super call

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()

    } // nib end

}

上面的代码导致错误 fatal error: unexpectedly found nil while unwrapping an Optional value 代码行:let intItem: Int = Int(i)! in ViewController 单击排序按钮时。

我的问题是弄清楚

根据你的问题的上下文,我尝试在一个项目中复制,所以我就是这样做的,希望它能帮助你。

结果:

代码:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
  @IBOutlet weak var tableView: UITableView!
  
  var risks = Array<(title: String, factor: Int)>()

  override func viewDidLoad() {
    super.viewDidLoad()
    
    tableView.delegate = self
    tableView.dataSource = self
    
    risks.append(("my cat", 0))
    risks.append(("my dog", 0))
    risks.append(("my sheep", 0))
    risks.append(("my cow", 0))
    risks.append(("my fish", 0))
  }
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return risks.count
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
    
    let riskTitle = risks[indexPath.row].title
    let riskFactor = risks[indexPath.row].factor
    
    cell.titleLabel.text = riskTitle
    cell.factorLabel.text = String(riskFactor)
    cell.index = indexPath.row
    
    
    cell.onFactorValueChanged = { [unowned self] newFactorValue, atIndex in
      self.risks[atIndex].factor = newFactorValue
    }
    
    return cell
  }
  
  @IBAction func sortRisksBasedOnFactor(_ sender: AnyObject) {
    risks.sort { (riskA, riskB) -> Bool in
      return riskA.factor > riskB.factor
    }
    
    tableView.reloadData()
  }
}

class TableViewCell: UITableViewCell {
  
  @IBOutlet weak var titleLabel: UILabel!
  @IBOutlet weak var factorLabel: UILabel!
  var index = 0
 
  var onFactorValueChanged: (Int, Int) -> Void = { _ in }
 
  @IBAction func generateFactor(_ sender: AnyObject) {
    factorLabel.text = String(random(min: 1, max: 10))
    onFactorValueChanged(Int(factorLabel.text!)!, index)
  }
  
  func random(min: Int, max: Int) -> Int {
    guard min < max else {return min}
    return Int(arc4random_uniform(UInt32(1 + max - min))) + min
  }
  
}