如何为UITableView触发下载的单元格绑定对应接口的fileID
问题描述
我从API获取了相关数据并展示在tableView单元格中,每个单元格都配有"Download"下载按钮,我需要将接口返回的fileID绑定到用户选中触发下载操作的对应单元格上。
我已经将按钮关联配置到了自定义UITableViewCell类中,由于每个单元格都对应独立的fileID,现需要实现点击任意单元格的下载按钮时,可准确获取该单元格对应fileID的逻辑。
现有代码
paymentsViewController 类现有代码
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data?.count ?? 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "paymentsCell", for: indexPath) as? paymentsModel else {return UITableViewCell()} let currentInvoice = payments.Result![changeCustomerKey.DefaultsKeys.keyTwo].properties?[0].payments let currentInvoices = currentInvoice?[indexPath.row] cell.priceLabelPayments?.text = "€\(currentInvoices?.amount ?? 0.00)" cell.whenDateLabel?.text = "\(currentInvoices?.insertionDate?.convertToDisplayForm() ?? "")" cell.fromSubLabel.text = "\(currentInvoices?.bank ?? "")" cell.downloadButtonPayments.layer.cornerRadius = CGFloat(5) cell.localizePaymentsModel() return cell } }
paymentsModel 类现有代码
class paymentsModel: UITableViewCell { @IBOutlet weak var cellContentViewPayments: UIView! @IBOutlet weak var downloadButtonPayments: UIButton! @IBOutlet weak var priceLabelPayments: UILabel! @IBOutlet weak var fromSubLabel: UILabel! @IBOutlet weak var whenDateLabel: UILabel! func localizePaymentsModel() { let defaults = UserDefaults.standard let lang = defaults.string(forKey: changeLanguageKey.DefaultsKeys.keyOne) downloadButtonPayments.setTitle(NSLocalizedString("Download", tableName: nil, bundle: changeLanguage.createBundlePath(lang: lang ?? "sq" ), value: "", comment: ""), for: UIControl.State.normal) } }
实现方案
用闭包回调的方式实现最稳妥,不会因为cell复用出现数据错位问题,不要用给button绑定tag、cell存fileID属性的方案,这两种在cell复用时很容易出现ID匹配错误的问题。修改步骤如下:
- 修改自定义cell类
paymentsModel,添加下载按钮点击的回调闭包,在cell的初始化方法里给按钮绑定点击事件 - 在
cellForRowAt方法中给cell的回调闭包赋值,闭包内直接捕获当前行对应的模型数据,就能拿到准确的fileID - 点击按钮时触发回调,在控制器层执行下载逻辑即可
修改后的paymentsModel代码
class paymentsModel: UITableViewCell { @IBOutlet weak var cellContentViewPayments: UIView! @IBOutlet weak var downloadButtonPayments: UIButton! @IBOutlet weak var priceLabelPayments: UILabel! @IBOutlet weak var fromSubLabel: UILabel! @IBOutlet weak var whenDateLabel: UILabel! // 下载按钮点击回调 var downloadTapAction: (() -> Void)? override func awakeFromNib() { super.awakeFromNib() // 绑定按钮点击事件 downloadButtonPayments.addTarget(self, action: #selector(downloadButtonTapped), for: .touchUpInside) } @objc private func downloadButtonTapped() { downloadTapAction?() } func localizePaymentsModel() { let defaults = UserDefaults.standard let lang = defaults.string(forKey: changeLanguageKey.DefaultsKeys.keyOne) downloadButtonPayments.setTitle(NSLocalizedString("Download", tableName: nil, bundle: changeLanguage.createBundlePath(lang: lang ?? "sq" ), value: "", comment: ""), for: .normal) } }
修改paymentsViewController中的cellForRowAt方法
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "paymentsCell", for: indexPath) as? paymentsModel else {return UITableViewCell()} let currentInvoice = payments.Result![changeCustomerKey.DefaultsKeys.keyTwo].properties?[0].payments guard let currentInvoices = currentInvoice?[indexPath.row] else { return cell } cell.priceLabelPayments?.text = "€\(currentInvoices.amount ?? 0.00)" cell.whenDateLabel?.text = "\(currentInvoices.insertionDate?.convertToDisplayForm() ?? "")" cell.fromSubLabel.text = "\(currentInvoices.bank ?? "")" cell.downloadButtonPayments.layer.cornerRadius = 5 cell.localizePaymentsModel() // 绑定点击回调,闭包直接捕获当前行的模型数据 cell.downloadTapAction = { [weak self] in guard let self = self else { return } // 替换为你模型中fileID对应的实际字段名 let targetFileID = currentInvoices.fileID // 此处执行下载逻辑,targetFileID就是当前点击单元格对应的正确ID print("待下载文件ID:\(targetFileID)") } return cell }
内容的提问来源于stack exchange,提问作者Lis1
相关产品推荐
相关产品推荐

