ios - 重新加载特定的 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
}}
解决方案
您可以使用reloadRows(at:with:)
:
tableView.reloadRows(at: [indexPathOfTheCellToBeReloaded], with: .automatic)
这是 Swift 代码。不确定您需要什么,因为您在标签中同时指定了 Swift 和 Objective-C。
推荐阅读
- c++ - 带有折叠表达式的模板会创建不需要的参数副本
- java - ThreadPoolExecutor 在达到队列容量时创建新线程后首先执行队列中的任务,而不是执行新的传入作业
- docker - docker-compose 错误:FileNotFoundError:[Errno 2] 没有这样的文件或目录
- c++ - QT 中的 UDP 消息接收问题
- c++ - 来自 VS 构建工具和 LLVM 的 Clang 标头
- node.js - NODE JS 项目演示
- server - 如何在一台带 GPU 的笔记本上运行多个服务器?
- php - 为什么相同的 SQL 查询会因用户角色的不同而不同?
- javascript - 材质 UI 中持久抽屉的路由
- vsto - 锁处RibonLabel尺寸