[英]How to assign the specific endpoint to the specific selected cell
I have some data coming from the API and I am showing them in tableView cells, Each of those cell's have a "Download" button, I need to assign a fileID from API to the cell that the user has selected to Download.我有一些来自 API 的数据,我在 tableView 单元格中显示它们,每个单元格都有一个“下载”按钮,我需要将 API 中的 fileID 分配给用户选择下载的单元格。
I have already set up the button to a UITableViewCell
class, I just need to have a idea how to make the button get the fileID depending on which cell is clicked since every cell has it's own fileID.我已经将按钮设置为
UITableViewCell
类,我只需要知道如何使按钮根据单击的单元格获取文件 ID,因为每个单元格都有自己的文件 ID。
paymentsViewController
class: 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
类:
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)
}
}
You are thinking about this wrong.你在想这个错误。 It isn't the cell that has the fileID.
它不是具有 fileID 的单元格。 When the user taps on a cell or a button, you should ask the table view (or collection view) for the indexPath it currently occupies, then use the IndexPath to look up the model data for that entry.
当用户点击一个单元格或按钮时,您应该向表格视图(或集合视图)询问它当前占用的 indexPath,然后使用 IndexPath 查找该条目的模型数据。
See my answer here :在这里查看我的答案:
Thant link shows code that lets you figure out the IndexPath for any view in a table view cell. Thant 链接显示的代码可让您找出表格视图单元格中任何视图的 IndexPath。 You could use that code from your button's IBAction.
您可以使用按钮的 IBAction 中的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.