Ios 通过本地更新阵列模型重新加载特定的tableview单元格

Ios 通过本地更新阵列模型重新加载特定的tableview单元格,ios,arrays,swift,uitableview,Ios,Arrays,Swift,Uitableview,我正在开发一个应用程序,其中电子邮件列表显示在tableview中。每封电子邮件都包含三种状态中的一种,如付费、未决和争议。这些数据是我从webservice响应中获得的。如果电子邮件状态为“已支付”,则我可以单击“待决”或“争议”按钮将状态更改为“待决”或“争议”。若状态已经支付,那个么用户不能对“支付”按钮执行任何操作 现在假设我通过调用webservice将电子邮件状态从挂起更改为付费,此时我想用该模型的本地替换状态重新加载该特定单元格 我怎样才能做到这一点 func tableView

我正在开发一个应用程序,其中电子邮件列表显示在tableview中。每封电子邮件都包含三种状态中的一种,如付费、未决和争议。这些数据是我从webservice响应中获得的。如果电子邮件状态为“已支付”,则我可以单击“待决”或“争议”按钮将状态更改为“待决”或“争议”。若状态已经支付,那个么用户不能对“支付”按钮执行任何操作

现在假设我通过调用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
}}
您可以使用:


这是Swift代码。由于在标记中同时指定了Swift和Objective-C,因此不确定需要什么。

您可以在本地重新加载模型数据并调用
tableView.reloadRows(在:indepath,使用:UITableViewRowAnimation)
将使用您在模型对象上更新的新数据重新加载单元格。

假设您的
电子邮件

class Email {
    var message: String
    var state: CellState

    init(message: String, state: CellState) {
        self.message = message
        self.state = state
    }
}
其中,
CellState
是一个
enum
,表示
单元格的
状态
-
已支付、未决、争议
,即

enum CellState {
    case paid, pending, dispute
}
cell.buttonTapHandler = {
    DispatchQueue.main.async {
        tableView.reloadRows(at: [indexPath], with: .none)
    }
}
首先,您不能在
tableView(:cellForRowAt:)
方法中写入
单元格的配置代码。
单元的
配置必须写入
单元
本身,即

class HomeTableViewCell: UITableViewCell {
    var buttonTapHandler: (()->())?
    private var email: Email?

    func configure(with email: Email) {
        self.email = email
        switch email.state {
        case .paid:
            self.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
        case .pending:
            self.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
        case .dispute:
            self.backgroundColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
        }
    }

    @IBAction func onTapPendingButton(_ sender: UIButton) {
        if self.email?.state != .pending {
            self.email?.state = .pending
            self.buttonTapHandler?()
        }
    }

    @IBAction func onTapDisputeButton(_ sender: UIButton) {
        if self.email?.state != .dispute {
            self.email?.state = .dispute
            self.buttonTapHandler?()
        }
    }

    @IBAction func onTapPaidButton(_ sender: UIButton) {
        if self.email?.state != .paid {
            self.email?.state = .paid
            self.buttonTapHandler?()
        }
    }
}
在上述代码中

  • buttonapandler
    -在点击任何
    付费/挂起/争议按钮后,
    单元格的状态发生更改时,处理
    单元格的
    重新加载
    。我们将看看如何进一步使用它

  • configure(with:)
    -根据单元格的
    当前状态处理单元格的
    UI

  • 按钮点击动作
    -更改点击按钮时所依据的
    单元格的
    状态
    。同时调用
    按钮选项卡
    来更新
    UI

  • 您的
    ViewController
    看起来像

    class VC: UIViewController, UITableViewDataSource {
        var emails = [Email]()
        //rest of the code....
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.emails.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeTableViewCell
            let email = self.emails[indexPath.row]
            cell.configure(with: email)
            cell.buttonTapHandler = {
                DispatchQueue.main.async {
                    tableView.reloadRows(at: [indexPath], with: .none)
                }
            }
            return cell
        }
    }
    
    在上面的代码中,最重要的是我们在哪里设置
    单元格的
    按钮面板
    ,即

    enum CellState {
        case paid, pending, dispute
    }
    
    cell.buttonTapHandler = {
        DispatchQueue.main.async {
            tableView.reloadRows(at: [indexPath], with: .none)
        }
    }
    

    每当您点击
    单元格中
    按钮
    时,就会调用
    处理程序
    ,并且该特定的
    单元格将
    重新加载

    是的。但是您必须使用
    enum
    来标识您的
    状态
    ,而不是
    Int
    。这在这里更重要