Saurabh_Jhingan
Saurabh_Jhingan

Reputation: 320

UITableView cellForRowAtIndexPath function not updating after reload function

I am trying to reload data in my tableview after getting the data from a delegate method. But the issue is that all the data is not coming through to the tableView function cellForRowAtIndexPath. I have set the variable self.restNames to hold the values from the delegate method which comes around 4 values but not all of them show up in the function cellForRowAtIndexPath. When I change the tabs and go back to the tableView, some data does come through but not all of it.Will appreciate any help on this. Apologies if I have missed something, I am new to Swift and haven't raised much questions in StackOverflow.

Regards, Saurabh

Below is my the code for the tableViewController

class RestTableViewController: UITableViewController,getDistanceTime {

var RestTable = [Restaurant]()
let label = UILabel()
let menuUrl = "menu url"
let listProjectUrl = "url"
override func viewDidLoad() {
    super.viewDidLoad()

    let headerView = self.tableView
    headerView.tableHeaderView?.frame = CGRectMake(0, 30, self.view.frame.width, 40)
    headerView.tableHeaderView?.backgroundColor = UIColor.redColor()


    self.tableView.tableHeaderView = self.tableView.tableHeaderView
    self.tableView.tableHeaderView?.frame = CGRectMake(0, 30, self.view.frame.width, 40)

    let token = keychain[""]

    let menuData = Alamofire.request(Method.GET, self.menuUrl, headers: ["Authorization":"JWT \(token!)"])
    menuData.responseJSON{ response in
        let data = JSON(response.result.value!)
        var localmenu = [menu]()
        for (_,item) in data{
            let menuOne = menu(place: item["place"].string!, types: item["types"].string!, name: item["name"].string!, price: item["price"].string!)
            localmenu.append(menuOne)
        }
        self.menus = localmenu
    }

}


//  Delegate method
func loadWithDisTime(distance: String,name: String)
{
    dispatch_async(dispatch_get_main_queue())
        { () -> Void in
            self.userDistanceFromLocation = distance
            //This will hold 4 values
            self.restNames = name

            self.tableView.reloadData()
    }

}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)
    self.getDistance = getDistanceTimeVC()
    self.getDistance.delegate = self

    let restCount = self.RestTable.count
    if restCount == 0
    {
        let alertView = UIAlertController(title: "There are no restaurants near your location", message: "Press Okay to go back", preferredStyle: UIAlertControllerStyle.Alert)
        let alertAction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.Cancel, handler: nil)
        alertView.addAction(alertAction)
        self.presentViewController(alertView, animated: true, completion: nil)

    }
    else
    {
        for item in 0...restCount - 1
        {
            self.restLat = self.RestTable[item].lat
            self.restLng = self.RestTable[item].lng
            self.getDistance.getDistance(self.userLat, userLng: self.userLng, restLat: self.restLat, restLng: self.restLng,restName: self.RestTable[item].name)
        }
    }
}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.RestTable.count

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! RestTableViewCell
   //Not all values coming through here 
    print(self.restNames)
    if self.RestTable[indexPath.row].name == self.restNames
    {
        cell.textLabel?.text = "\(indexPath.row + 1) - \(self.RestTable[indexPath.row].name) - \(self.userDistanceFromLocation)"
        cell.backgroundColor = UIColor.whiteColor()
        cell.textLabel?.textColor = UIColor.blackColor()
        cell.textLabel?.font = UIFont(name: "Avenir-Heavy", size: 15)
    }


    return cell
}

Upvotes: 0

Views: 1193

Answers (1)

Anton
Anton

Reputation: 3120

In this block leave only reload table view

dispatch_async(dispatch_get_main_queue())
        { () -> Void in

            self.tableView.reloadData()
    }

Upvotes: 1

Related Questions