UITableView:自定义单元格中的更新标签?

时间:2017-12-05 14:09:35

标签: swift uitableview custom-cell

我在一个View Controller中有两个表视图。其中一个表视图使用自定义单元格。我为两个表视图创建了一个自定义类。

在自定义单元格中,我有一些标签和两个按钮。按钮是一个按钮,一个减号按钮。我希望这些按钮添加1,或从名为counterLabel的标签中减去1。

自定义类:

import UIKit  
class MainTableViewCell : UITableViewCell  
{  
    @IBOutlet weak var refMainCell: UILabel!  

}  

class SubTableViewCell : UITableViewCell  
{  
    var counter: Int = 0  
    @IBOutlet weak var label: UILabel!  
    @IBOutlet weak var subtitleLabel: UILabel!  
    @IBAction func minusButton(_ sender: UIButton)  
    {  
        counter -= 1  
    }  
    @IBAction func plussButton(_ sender: UIButton)  
    {  
        counter += 1  

    }  
    @IBOutlet weak var counterLabel: UILabel!  
}

从主视图控制器设置:

import UIKit  
class MenyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {  



    struct Objects {  
        var sectionName: String!  
        var SectionObjects: [String]!  
        var subtitleArray: [String]!  
    }  

    var hovedMenyArray = ["Main1", "Main2", "Main3", "Main4"]  
    var underMenyArray = [Objects]()  
    var subtitleArray = ["", "","",""] /  
    let detailArray = ["Detail1", "Detail2", "Detail3", "Detail4", "Detail5", "Detail6", "Detail7", "Detail8"]  

    @IBOutlet weak var hovedMenyTableView: UITableView!  
    @IBOutlet weak var underMenyTableView: UITableView!  

    override func viewDidLoad() {  
        super.viewDidLoad()  

        underMenyArray = [Objects.init(sectionName: "Section 1", SectionObjects: ["Item 1", "Item 2", "Item 3"], subtitleArray: ["Subtitle 1", "Subtitle 2", ""]),  
                          Objects.init(sectionName: "Section 2", SectionObjects: ["Item 3", "Item 4", "Item 5"], subtitleArray: ["Subtitle 4", "Subtitle 5", "Subtitle 6"]),  
                          Objects.init(sectionName: "Section 3", SectionObjects: ["Item 6", "Item 7", "Item 8"], subtitleArray: ["Subtitle 7", "Subtitle 8", "Subtitle 9"]),  
                          Objects.init(sectionName: "Section 4", SectionObjects: ["Item 6", "Item 7", "Item 8"], subtitleArray: ["Subtitle 10", "Subtitle 11", "Subtitle 12"])]  
       }      

并且:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell  
        {  
           if (tableView == MainTableView)  
            {  
                let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell", for: indexPath) as! MainTableViewCell  
                cell.refMainCell.text = mainArray[indexPath.row]  
                cell.detailTextLabel?.text = subtitleArray[indexPath.row]  
                cell.accessoryType = .disclosureIndicator  
                return cell  
            }else  
            {  
                let cell: UITableViewCell  
                cell = tableView.dequeueReusableCell(withIdentifier: "SubCell", for: indexPath)  
                if let customCell = cell as? SubTableViewCell  
                {  
                    customCell.label.text = subArray[indexPath.section].SectionObjects[indexPath.row]  
                    customCell.SubtitleLabel.text = subArray[indexPath.section].subtitleArray[indexPath.row]  
                    customCell.counterLabel.text = String (customCell.counter)  //Here I'm trying to use the pluss and minus buttons.

                return cell  
            }  
       } 

在此设置中,counterLabel排序有效,但它会更新每个部分中的标签。我认为这是正确的,因为它没有被告知要更新特定的单元格 我该如何获得正确的标签更新?

我还需要向下滚动才能更新标签,但我想还有一个更新功能可供使用?

1 个答案:

答案 0 :(得分:1)

将观察者添加到计数器变量中。每次计数器的值发生变化时,正确的标签都会更新。

var counter: Int = 0 {
    didSet {
        counterLabel.text = String(counter)
    }
}