Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView在我滚动之前不显示新数据_Ios_Swift_Uitableview - Fatal编程技术网

Ios UITableView在我滚动之前不显示新数据

Ios UITableView在我滚动之前不显示新数据,ios,swift,uitableview,Ios,Swift,Uitableview,首先,我想告诉你,我已经尝试过搜索,我发现了太多的问题都是同一个问题,但是在我的案例中,提出的解决方案不起作用 我想在UIViewController的视图中插入一个tableView当单击特定按钮时,UITableView的数据将来自服务器 我有UITableView而不是UITableViewController 我的问题是,除非我滚动,否则数据不会被更新 我已经发现这个问题的解决方案是调用setNeedLayout,另一个人建议使用setNeedsDisplay。两者都没有解决我的问题 这

首先,我想告诉你,我已经尝试过搜索,我发现了太多的问题都是同一个问题,但是在我的案例中,提出的解决方案不起作用

我想在UIViewController的视图中插入一个tableView当单击特定按钮时,UITableView的数据将来自服务器

我有UITableView而不是UITableViewController

我的问题是,除非我滚动,否则数据不会被更新

我已经发现这个问题的解决方案是调用setNeedLayout,另一个人建议使用setNeedsDisplay。两者都没有解决我的问题

这个问题也提出了同样的问题,答案是调用重载数据,我从一开始就是这么做的

这是我的UITableView的委托和数据适配器

class CusinePreferencesTableView: NSObject, UITableViewDelegate, UITableViewDataSource {

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentefiers.oneCusinePreferencesCell.rawValue, forIndexPath: indexPath) as! OneCusinePreferencesTableViewCell
        let row = indexPath.row
        print("row = \(row)")
        let oneCusineDataLeft = Preferences2ViewController.cusines![2*row]
        cell.leftButton.titleLabel?.text = oneCusineDataLeft
        if (2*row+1) < Preferences2ViewController.cusines!.count{
            let oneCusineDataRight = Preferences2ViewController.cusines![2*row+1]
            cell.rightButton.titleLabel?.text = oneCusineDataRight
        }else {
            //I should hide the right button
            cell.rightButton.titleLabel?.text = "wow"
        }
        cell.setNeedsDisplay()
        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let cusines = Preferences2ViewController.cusines {
            if cusines.count % 2 == 0 {
                return cusines.count/2
            }else {
                return (cusines.count+1)/2
            }
        }else {
            return 0
        }
    }

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

}
最后是loadCusines函数

func loadCusiens(){
        let url = NSURL(string: ConstantData.getWebserviceFullAddress()+"preferences/cusines")
        let request = NSMutableURLRequest(URL: url!)
        request.HTTPMethod = "POST"
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error ) in

            if let data = data {
                do{
                    // here I handle the response
                    Preferences2ViewController.cusines = results
                    dispatch_async(dispatch_get_main_queue(), {
                        self.cusineTableView.reloadData()
                    })
                } catch{

                }

            }
        })
    task.resume()
}
尽管早上我问了一个类似的问题,但这是一个不同的问题,因为早上我在tableViewCell中有一个不好的代码

setConstraintsForTableView

另外,尝试删除网络调用中的
dispatch\u async

请忘记setConstraints可移植视图,该函数在设置构造时没有任何异常。显然,我无法删除dispath\u async,@sarah当然可以删除调度。尝试it@ray当然,无法删除dispatch\u async。NSURLSession dataTask的完成处理程序在后台线程上运行,而UIKit调用必须在主线程上运行。@jcaron source to docs?我在后台线程中进行UIKit调用已经有一段时间没有任何问题了now@ray或者你没有注意到结果,或者你没有注意到结果,很明显当你注意到时,更新通常会延迟。例如,请参阅topicHave上的许多SO问题您是否检查了您的函数是否确实被调用,同样,对于完成处理程序,以及更新和重新加载数据的部分?您的代码显然缺少将
数据
转换为
结果的部分
@jcaron是的,当然,它正在被调用,数据正在从服务器获取,从
数据
结果
的转换在哪里?@jcaron好吧,我没有发表这篇文章,因为这只不过是删除来自服务器的json,它(至少我认为)与问题无关。我想保持问题的简单性您是否检查了
结果
是否包含正确的数据?您是否检查了在触发重新加载时是否实际调用了
numberOfSectionsInTableView
tableView:numberofrowsinssection:
,以及它们返回的值?注意:
setNeedsDisplay
肯定是不需要的,而且行计数函数可以简化很多。
setConstraintsForTableView