简体   繁体   中英

DidSelectRowAtIndextPath give me wrong details

I have a tableview with custom cells, when I click on one of my cells it shows me the next viewcontroller ( which is the details of the view controller ) as it should be, the details that assigned to this cell ( received from JSON and saved locally as dictionary ) is totally wrong and when click back and re enter this cell gives me right things as my expectations

Any explanation please?

My code

Here how I fetch the data

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

        }
    }
}

}

Here is my 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)
    }

}

My 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)
}

It seems you are doing reverse on your myNotifications array after tableView's reloadData called. So try reload your tableView once you have reversed your myNotifications array as like below.

   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()
    }

Also have you noticed that you are doing reverse on your array( generalNotifications.reverse() ) whenever you are selecting a cell, which will reverse your array each time. So First time you will get correct value and next time again array will be reversed and wrong value will be returned. Try using reversed array as like below.

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM