我已经有了用于显示来自API的JSON值的TableView。但是,单击的结果与现有标题不匹配,但是值得上一次单击的标题。更清楚地看到图片
代码InfoViewCell.swift 此代码用于表格视图中的单元格
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
}
}
代码信息。将代码替换为模型
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, 但是我要显示的值不符合我的期望。我会将这个值传递给详细视图
谢谢
答案 0 :(得分:2)
当您单击一行时,将选择该行-并取消选择上一个选定的行。好了,您已经实现了didDeselect
,因此将显示先前选择的行。而是实施didSelect
。
答案 1 :(得分:0)
代替此
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)
}
请使用此
func tableView(_ tableView: UITableView, didSelectRowAt 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)
}