如何与自定义 UITableViewCell 类交互?

时间:2021-02-14 08:15:09

标签: ios swift uitableview

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

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 类

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

2 个答案:

答案 0 :(得分:0)

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
}

答案 1 :(得分:0)

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

(来自评论中的对话,这是我实现的) enter image description here

自定义 UITableViewCell 类

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 类:

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