简体   繁体   English

如何将特定端点分配给特定的选定单元格

[英]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.

相关问题 选择单元格时如何将特定值传递给单元格? - How to pass specific value to a cell when that cell is selected? Swift:如何将wilDisplayCell分配给特定的自定义寄存器单元? - Swift: How do I assign wilDisplayCell to specific custom register cell? 如何根据选定的单元格将segue推送到特定的View Controller? - How to push segue to specific View Controller depending on a selected cell? 选择该单元格后,如何将其从图像库上传到特定的collectionview单元格? - How to upload image from photolibrary to specific collectionview cell when that cell is selected? 如何更改特定单元格的值 - How to change the value for a specific cell 如何将特定的ViewController分配给UIView(在UICollectionView中)? - How to assign a specific ViewController to a UIView (in UICollectionView)? 如何为不同的目标分配位置特定的BundleDisplayName - How to assign location specific BundleDisplayNames for different Targets 如何增加特定部分中特定单元格的高度 - How can I increase the height for a specific Cell in a specific Section 如何使用Action存储来自特定单元格的数据? - How to store data from a specific cell with an Action? 如何删除表格视图单元格中的特定行? - How to delete specific rows in a table view cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM