UITableView cellForRowAtIndexPath函数在重载功能后不更新

时间:2016-03-14 11:02:01

标签: swift reloaddata uitableview

我在从委托方法获取数据后尝试在tableview中重新加载数据。但问题是所有数据都没有通过tableView函数cellForRowAtIndexPath。我已经设置变量self.restNames来保存委托方法中的值,这些值来自4个值,但并非所有值都出现在函数cellForRowAtIndexPath中。当我更改选项卡并返回到tableView时,一些数据确实通过但不是全部。请注意任何帮助。抱歉,如果我错过了什么,我是Swift的新手,并且在StackOverflow中没有提出太多问题。

此致 SAURABH

下面是我的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
}

1 个答案:

答案 0 :(得分:1)

在此块中仅保留重新加载表视图

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

            self.tableView.reloadData()
    }