我有一个带有自定义单元格的表格视图,当我单击一个单元格时,它会向我显示应有的下一个viewcontroller(这是视图控制器的详细信息),以及分配给该单元格的详细信息(从JSON并在本地另存为Dictionary)是完全错误的,单击返回并重新输入此单元格可以满足我的期望
请问有什么解释吗?
我的代码
这是我如何获取数据
func getMyNotifications() {
Alamofire.request("\(Constant.GetMyNotifications)/-1", method: .get, encoding: JSONEncoding.default , headers: Constant.Header ).responseJSON { response in
if let Json = response.result.value as? [String:Any] {
if let ActionData = Json["ActionData"] as? [[String:Any]] {
self.myNotifications = ActionData
self.generalNotifications = ActionData
//
self.myNotificationsTV.reloadData()
self.counter.text = "\(ActionData.count)"
self.myNotifications.reverse()
self.animationView.isHidden = true
self.animationView.stop()
self.refreshControl.endRefreshing()
}
if self.myBalaghat.count == 0 {
self.myNotificationsTV.isHidden = true
self.counter.text = "no notificatins to show"
} else {
self.myNotificationsTV.isHidden = false
}
}
}
}
这是我的cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if segmented.selectedSegmentIndex == 0 {
return returnCell(balaghat: myNotificationsTV, withData: myNotifications, inCell: indexPath.row)
} else {
return returnCell(balaghat: myNotificationsTV, withData: allNotifications, inCell: indexPath.row)
}
}
我的didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
generalNotifications.reverse()
let prepareNum = generalNotifications[indexPath.row]["Id"] as? NSNumber
currentBalaghId = Int(prepareNum!)
clickedIndex = indexPath.row
if let text = generalNotifications[indexPath.row]["NotifDateG"] as? String {
prepareDateforPassing = text
}
if let text = generalNotifications[indexPath.row]["Description"] as? String {
prepareDesciptionforPassing = text
}
if let text = generalNotifications[indexPath.row]["TypeName"] as? String {
prepareTypeforPassing = text
}
if let text = generalNotifications[indexPath.row]["AddedByName"] as? String {
prepareProviderNameforPassing = text
}
self.performSegue(withIdentifier: "showDetails", sender: self)
// to remove highlighting after finish selecting
tableView.deselectRow(at: indexPath, animated: true)
}
答案 0 :(得分:0)
在调用tableView的reloadData之后,似乎对myNotifications数组进行了反向操作。因此,当您按如下所示反转myNotifications数组后,请尝试重新加载tableView。
if let ActionData = Json["ActionData"] as? [[String:Any]] {
self.myNotifications = ActionData
self.generalNotifications = ActionData
//
self.counter.text = "\(ActionData.count)"
self.myNotifications.reverse()
self.myNotificationsTV.reloadData()
self.animationView.isHidden = true
self.animationView.stop()
self.refreshControl.endRefreshing()
}
您还注意到,每当选择一个单元格时,您都在对数组(generalNotifications.reverse()
)进行反向操作,每次都会反转您的数组。因此,第一次您将获得正确的值,而下次您将再次反转数组并将返回错误的值。尝试使用反向数组,如下所示。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let reversedGeneralNotifications = generalNotifications.reversed()
let prepareNum = reversedGeneralNotifications[indexPath.row]["Id"] as? NSNumber
currentBalaghId = Int(prepareNum!)
clickedIndex = indexPath.row
if let text = reversedGeneralNotifications[indexPath.row]["NotifDateG"] as? String {
prepareDateforPassing = text
}
if let text = reversedGeneralNotifications[indexPath.row]["Description"] as? String {
prepareDesciptionforPassing = text
}
if let text = reversedGeneralNotifications[indexPath.row]["TypeName"] as? String {
prepareTypeforPassing = text
}
if let text = reversedGeneralNotifications[indexPath.row]["AddedByName"] as? String {
prepareProviderNameforPassing = text
}
self.performSegue(withIdentifier: "showDetails", sender: self)
// to remove highlighting after finish selecting
tableView.deselectRow(at: indexPath, animated: true)
}