为啥要处理单击 TableView Swift IOS 值得单元格行之前单击的行? [复制]
Posted
技术标签:
【中文标题】为啥要处理单击 TableView Swift IOS 值得单元格行之前单击的行? [复制]【英文标题】:Why handle clicking on TableView Swift IOS worth cell rows are the previous rows clicked? [duplicate]为什么要处理单击 TableView Swift IOS 值得单元格行之前单击的行? [复制] 【发布时间】:2019-05-30 04:20:58 【问题描述】:我已经是用于显示来自 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
Code Info.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, 但我想展示的价值不符合我的预期。我会将这个值传递给详细视图
谢谢
【问题讨论】:
选择其他单元格时调用didDeselectRowAt,只需将其替换为tableview的didSelect方法 哦,是的,现在开始工作了!谢谢 【参考方案1】:当您单击一行时,该行被选中 - 而前一个选定的行被取消选中。好了,你已经实现了didDeselect
,所以前面选中的行就显示出来了。相反,实现didSelect
。
【讨论】:
【参考方案2】:而不是这个
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)
【讨论】:
以上是关于为啥要处理单击 TableView Swift IOS 值得单元格行之前单击的行? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
如何在单击 tabBar 项目 Swift 上刷新 tableView?
当我在 swift 4 中单击 uitableviewcell 时如何传递数据?
当我使用此代码 firebase Swift 4 时,为啥我的 tableview 会出错