首页 > 解决方案 > 重新加载特定的 tableview 单元格并在本地更新数组模型

问题描述

我正在开发一个应用程序,其中电子邮件列表显示在表格视图中。每封电子邮件都包含来自三个状态的状态之一,例如已付款、待处理和争议。我从 Web 服务响应中获得这些数据。如果电子邮件状态已支付,那么我可以通过单击待处理或争议按钮将状态更改为待处理或争议。如果状态已支付,则用户无法对已支付按钮执行任何操作。

现在假设我通过调用 webservice 将电子邮件状态从待处理更改为已付款,当时我想重新加载该特定单元格并在本地替换该模型的状态。

我怎样才能做到这一点。

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell : HomeTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! HomeTableViewCell
    if arrayData.count > 0 && arrayData.count > indexPath.row {
    var objemail = self.arrayData.object(at: indexPath.row) as? NewHomeModel
    if objemail?.unread == 1 {
        cell.lblEmailSender.textColor = UIColor.black
        cell.lblEmailDescription.textColor = UIColor.black
    }else{
        cell.lblEmailSender.textColor = UIColor.gray
        cell.lblEmailDescription.textColor = UIColor.gray
    }
    if objemail?.fileStatus == 1 {
        cell.imgAttachment.isHidden = false
    }else{
        cell.imgAttachment.isHidden = true
    }
    cell.lblEmailSender.text = objemail?.from
    cell.lblEmailHeading.text = objemail?.subject
    cell.lblEmailDescription.text = objemail?.body
    var pending = objemail?.pending
    if (pending == 0) {
        cell.btnPending.setBackgroundImage(UIImage(named: "pending_disable_icon"), for: UIControl.State.normal)
        cell.btnPending.addTarget(self, action: #selector(HomeViewController.penddingClick(_:)), for:.touchUpInside )
    }
    else
    {
        cell.btnPending.setBackgroundImage(UIImage(named: "pending_active_icon"), for: UIControl.State.normal)
    }
    if objemail?.dispute == 0 {
        cell.btnDispute.setBackgroundImage(UIImage(named: "dispute_disable_icon"), for: UIControl.State.normal)
        cell.btnDispute.addTarget(self, action: #selector(HomeViewController.disputeClick(_:)), for:.touchUpInside )
    }
    else
    {
        cell.btnDispute.setBackgroundImage(UIImage(named: "dispute_active_icon"), for: UIControl.State.normal)
    }
    if(objemail?.paid == 0)
    {
        cell.btnPaid.setBackgroundImage(UIImage(named: "paid_disable_icon"), for: UIControl.State.normal)
        cell.btnPaid.addTarget(self, action: #selector(HomeViewController.paidClick(_:)), for:.touchUpInside )
    }
    else
    {
        cell.btnPaid.setBackgroundImage(UIImage(named: "paid_active_icon"), for: UIControl.State.normal)

    }
    }
    return cell
}
struct NewHomeModel {
var body: String?
var date : String?
var dispute: Int?
var fileStatus: Int?
var from: String?
var msg_id: String?
var paid: Int?
var pending: Int?
var subject: String?
var thread_id: String?
var unread : Int?
var nextToken : String?

init(jsonData: [String: Any]) {
    body = jsonData["body"] as? String ?? ""
    date = jsonData["date"] as? String ?? ""
    dispute = jsonData["dispute"] as? Int ?? 0
    fileStatus = jsonData["fileStatus"] as? Int ?? 0
    from = jsonData["from"] as? String ?? ""
    msg_id = jsonData["msg_id"] as? String ?? ""
    paid = jsonData["paid"] as? Int ?? 0
    pending = jsonData["pending"] as? Int ?? 0
    subject = jsonData["subject"] as? String ?? ""
    thread_id = jsonData["thread_id"] as? String ?? ""
    unread = jsonData["unread"] as? Int ?? 0
}}

标签: iosarraysswiftuitableview

解决方案


您可以使用reloadRows(at:with:)

tableView.reloadRows(at: [indexPathOfTheCellToBeReloaded], with: .automatic)

这是 Swift 代码。不确定您需要什么,因为您在标签中同时指定了 Swift 和 Objective-C。


推荐阅读