如何与自定义 UITableViewCell class 交互?

How to interact with custom UITableViewCell class?

我使用自定义 UITableViewCell class 创建了 table 视图。因此,当点击导航栏中的按钮时,我的应用程序应该将 UIStepper 设置为隐藏。比它应该按 Stepper 的值更新计数标签 我的应用程序的使命是查看您家中的药品数量和清单。因此,当点击该行的步进器时更新每个医疗人员的计数。

import UIKit
class MedicinesListPage: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    
    struct Medicine {
        var name: String
        var count: Int
    }
    var medicines = [
        Medicine(name: "Парацетамол", count: 1),
        Medicine(name: "Ибуфен", count: 2),
        Medicine(name: "Цитрамон", count: 1),
        Medicine(name: "Смекта", count: 1),
        Medicine(name: "Мезин", count: 1),
        Medicine(name: "Терафлю", count: 1),
        Medicine(name: "Спирт", count: 1),
        Medicine(name: "Бинт", count: 1),
        Medicine(name: "Мукалтин", count: 1),
        Medicine(name: "Стрепсилс", count: 1)
    ]
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let rightButton = UIBarButtonItem(title: "Add", style: UIBarButtonItem.Style.plain, target: self, action:(#selector(showEditing)))
        
        navigationItem.rightBarButtonItem = rightButton
        
        tableView.delegate = self
        tableView.dataSource = self
        
    }
    @objc func showEditing(){
        
// should unhide stepper 
    }
    
    
    


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 65
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return medicines.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  tableView.dequeueReusableCell(withIdentifier: "medicalTableViewCell") as? medicalTableViewCell
        cell?.configureCell(name: medicines[indexPath.row].name, count: medicines[indexPath.row].count)
        return cell!
    }
}

UITableViewCell class

import UIKit

final class medicalTableViewCell: UITableViewCell{
    
 
    
    @IBOutlet weak var medicalName: UILabel!
    @IBOutlet weak var medicalCount: UILabel!
    @IBOutlet weak var stepper: UIStepper!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        
    }
    
    
    @IBAction func stepperTapped(_ sender: UIStepper) {
        // should update count of medicals
    }
    
    
    public func configureCell(name: String, count: Int){
        
        medicalName.text = name
        medicalCount.text = String(count)
        
        
    }
    
}
var medicalNames: [String] = ["item 1", "item 2", "item 3"]
var medicalCount: [Int] = [5,2,7]

这是糟糕的设计 - 将其改为结构数组:

struct Medicine { 
   let name: String
   var count: Int
}

let medicines = [
   Medicine(name: "foo", count: 1), Medicine(name: "bar", count: 2)
]

完成后,您可以轻松添加一个 Bool 标志,例如 stepperHidden,它控制步进器是否在您的 configureCell 方法中可见。如果您希望隐藏所有步进器,请将所有标志设置为 true 并调用 tableView.reloadData()

您的 tableView 方法如下所示:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell =  tableView.dequeueReusableCell(withIdentifier: "medicalTableViewCell") as! medicalTableViewCell
    let medicine = medicines[indexPath.row]
    cell?.configureCell(with: medicine)
    return cell
}

相应地,

public func configureCell(_ medicine: Medicine){
    medicalName.text = medicine.name
    medicalCount.text = "\(medicine.count)"
    // stepper.isHidden = medicine.stepperHidden
}

据我了解,您有一个 TableViewController 带有海关 UITableView 单元格。 每个单元格都包含药物名称、编号和 UIStepper(允许更改该药物的编号)。 您的目标是在每次点击 UIStepper 时更新 medicalCountLabel

(从评论里的对话来看,我是这么实现的)

自定义 UITableViewCell Class

import UIKit

class TableViewCell: UITableViewCell {
    //MARK: - Properties
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var countLabel: UILabel!
    @IBOutlet weak var stepper: UIStepper!
    
    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
    }
    
    //MARK: - Actions
    
    @IBAction func stepperTapped(_ sender: UIStepper) {
        countLabel.text = Int(sender.value).description
    } //this is where you update `medicalCountLabel`
    
}

TableViewController Class:

import UIKit

class TableViewController: UITableViewController {
    //MARK: - Properties
    var medicines = [Medicine(name: "Парацетамол", count: 4), Medicine(name: "Ибуфен", count: 7),Medicine(name: "Цитрамон", count: 4),Medicine(name: "Смекта", count: 9), Medicine(name: "Мезин", count: 2)]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - Table view data source

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

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

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TableViewCell else{
            fatalError("The dequeued cell is not an instance of TableViewCell.")
        }

        // Configure the cell
        let medicine = medicines[indexPath.row]
        cell.nameLabel.text = medicine.name
        cell.countLabel.text = String(medicine.count)
        cell.stepper.value = Double(medicine.count)
        return cell
    }
}

Medicine Class(在你的情况下,你只是使用 struct 完成它,这也很好;但我创建了一个单独的 swift 文件):

import UIKit
class Medicine{
    var name: String
    var count: Int
    
    init(name: String, count: Int){
        self.name = name
        self.count = count
    }
}