简体   繁体   中英

TableView Cell data is being disappeared on click on the cell. Why this is happening?

I have created a tableview. Then calling an url to get the data and accordingly loading the tableview. But whenever i am clicking a cell the data is disappearing. I have tried with making userinteractionenabled as false or the selection style as .None. Still it is not working. Any reason for this?

class CheckInTableViewController: UITableViewController {

var flightInfo : [Flight]!
  var multipleChoiceQuestion : [MultipleChoiceQuestion]!
    var question : [String] = [String]()
       var answer : [[String]] = [[String]]()
         var correctAnswer: [Int] = [Int]()

override func viewDidLoad() {
    super.viewDidLoad()
    loadData()
   }

func loadData(){

    Request.CheckInFlight().execute().validate().responseJSON { (request, _, data, error)
        -> Void in

        if error != nil {
            print("error")
        }
        else{
            self.flightInfo = Response.flightsFromJSON(data)
              self.multipleChoiceQuestion = self.flightInfo[0].checkInUpdate.checkInTest.multipleChoiceQuestion

            for questionNew in self.multipleChoiceQuestion {
                self.question.append(questionNew.question)
                self.answer.append(questionNew.answerArray)
                self.correctAnswer.append(questionNew.correctAnswerId)

            }

            self.tableView.reloadData()

}
}
}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    if flightInfo != nil {
       return flightInfo.count + 1
    }
          return 1
}




override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell()

    switch indexPath.row {

    case 0: _ = tableView.dequeueReusableCellWithIdentifier("headercell", forIndexPath: indexPath) as! CheckInHeaderTableViewCell

    default:
        let cell = tableView.dequeueReusableCellWithIdentifier("datacell", forIndexPath: indexPath) as! CheckInTableViewCell


    cell.flightNumber.text = flightInfo[indexPath.row - 1].fullFlightId

        cell.departureCity.text = flightInfo[indexPath.row - 1].departureInfo!.airport.iataCode
       cell.departureDate.text = dateFormatter(flightInfo[indexPath.row - 1].departureInfo!.scheduledDateTime)
        cell.departureTime.text = flightInfo[indexPath.row - 1].departureInfo!.scheduledDateTime.flightTimeString()
        cell.arrivalCity.text = flightInfo[indexPath.row - 1].arrivalInfo!.airport.iataCode
       cell.arrivalDate.text = dateFormatter(flightInfo[indexPath.row - 1].arrivalInfo!.scheduledDateTime)
        cell.arrivalTime.text = flightInfo[indexPath.row - 1].arrivalInfo!.scheduledDateTime.flightTimeString()

    }
    return cell
}

Just to round this up, I'm turning my comment into an answer.

You are return ing the cell from the first line. That is a fresh cell.

The dequeue'd cell you are modifying and throwing away w/o return in the switch.

The dequeue in case 0 you are throwing away instantly.

This should give you a warning regarding scope ambiguity on the second let cell = , btw.

If the problem persists, please update your question and let me know:)

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