ios - 为什么点击 TableView Swift IOS 值得单元格行是点击前几行?

标签 ios swift tableview

<分区>

我已经有了用于显示来自 API 的 JSON 值的 TableView。但是点击的结果与现有标题不匹配,但值得上次点击的标题。看图更清楚

Picture

代码 InfoViewCell.swift 此代码用于 tableview 中的单元格

import UIKit

class InfoViewCell: UITableViewCell {
    @IBOutlet weak var imgInfo: UIImageView!
    @IBOutlet weak var lblInfo: UILabel!
    @IBOutlet weak var lblBerita: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

代码信息.swift 模型代码

class Info {

    var id_informasi: Int?
    var tgl_informasi: String?
    var judul: String?
    var berita: String?
    var foto: String?

    init(id_informasi:Int?,judul: String?,berita: String?,foto: String?) {
        self.id_informasi = id_informasi
        self.judul = judul
        self.berita = berita
        self.foto = foto
    }
}

代码InfoViewController.swift

import UIKit
import Alamofire
import AlamofireImage

class InformasiViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var tableInfo: UITableView!
    var activityIndicator:UIActivityIndicatorView = UIActivityIndicatorView()


    var infoes = [Info]()

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return infoes.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellInfo", for: indexPath) as! InfoViewCell

        //getting the hero for the specified position
        let inpo: Info
        inpo = infoes[indexPath.row]

        //displaying values
        cell.lblInfo.text = inpo.judul
        cell.lblBerita.text = inpo.berita

        //displaying image
        Alamofire.request(inpo.foto!).responseImage { response in
            debugPrint(response)

            if let image = response.result.value {
                cell.imgInfo.image = image
            }
        }

        return cell
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

        let info: Info
        info  = infoes[indexPath.row]

        //building an alert
        let alertController = UIAlertController(title: info.judul, message: "", preferredStyle: .alert)

        //the confirm action taking the inputs
        let confirmAction = UIAlertAction(title: "Enter", style: .default) { (_) in

        }

        //the cancel action doing nothing
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }

        //adding action
        alertController.addAction(confirmAction)
        alertController.addAction(cancelAction)

        //presenting dialog
        present(alertController, animated: true, completion: nil)
    }



    override func viewDidLoad() {
        super.viewDidLoad()


        let defaultValues = UserDefaults.standard
        let token = defaultValues.string(forKey: "token")


        //the Web API URL
        let URL_GET_DATA = "https://api.landslidepad.com/api/admin_desa/informasi_penting?token=" + token!

        activityIndicator.center = self.view.center
        activityIndicator.hidesWhenStopped = true
        activityIndicator.style = UIActivityIndicatorView.Style.gray
        view.addSubview(activityIndicator)
        activityIndicator.startAnimating()
        //fetching data from web api
        Alamofire.request(URL_GET_DATA, method: .get).responseJSON
            {

                response in
                //printing response
                print(response)

                self.activityIndicator.stopAnimating()
                //getting the json value from the server
                if let result = response.result.value {

                    let jsonData = result as! NSDictionary

                    //if there is no error
                    if((jsonData.value(forKey: "message") as! String == "Sukses!")){

                        //getting the user from response
                        let user = jsonData.value(forKey: "values") as! NSArray

                        for i in 0..<user.count{

                            //adding hero values to the hero list
                            self.infoes.append(Info(
                                id_informasi: (user[i] as AnyObject).value(forKey: "id_informasi") as? Int,
                                judul: (user[i] as AnyObject).value(forKey: "judul") as? String,
                                berita: (user[i] as AnyObject).value(forKey: "berita") as? String,
                                foto: (user[i] as AnyObject).value(forKey: "foto") as? String
                            ))
                        }
                        //displaying data in tableview
                        self.tableInfo.reloadData()

                    }else{
                        let alert = UIAlertController(title: "Ada yang salah?", message: "Silahkan Ulangi Kembali!.", preferredStyle: .alert)

                        alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: nil))
                        alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))

                        self.present(alert, animated: true)
                    }
                }
        }



        self.tableInfo.reloadData()
        // Do any additional setup after loading the view, typically from a nib.

        // Do any additional setup after loading the view.
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

我已经尝试创建 func tableview didDeselectRowAt indexPath, 但是我想要展示的值(value)并不符合我的预期。我会将这个值传递给详细 View

谢谢

最佳答案

当您单击一行时,该行将被选中,而先前选中的行将被取消选中。好吧,您已经实现了didDeselect,因此显示了之前选择的行。相反,实现 didSelect

关于ios - 为什么点击 TableView Swift IOS 值得单元格行是点击前几行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56371573/

相关文章:

ios - Uitableviewcell iOS内的单选按钮

ios - 我可以为警报 Controller 添加长按事件吗

ios - 如何圆 UIBezierPath 线边缘?

ios - Apple Watch 上的 SpriteKit - 如何呈现场景?

xcode - 使用 OS X 平台的 Swift Playground 中没有这样的模块 'Cocoa'

ios - 根据 slider 更改 UIView 中的内容

ios - 滚动时如何在静态单元格表​​ View 中创建弹性图像

JavaFX TableView 与 SceneBuilder

ios - 在我的Xcode项目中找不到<libxml2/tree.h>

ios - 在 iOS 上从不同的应用程序读取文档