Ios 除非用户触摸,否则tableview不会显示数据
我试图在tableview上显示一些数据,但如果我不触摸tableview,它将不显示数据。它仅在我触摸数据后显示数据 我做了一些研究,但找不到与我的问题类似的东西。下面是我的代码。我怎样才能解决这个问题Ios 除非用户触摸,否则tableview不会显示数据,ios,uitableview,swift,Ios,Uitableview,Swift,我试图在tableview上显示一些数据,但如果我不触摸tableview,它将不显示数据。它仅在我触摸数据后显示数据 我做了一些研究,但找不到与我的问题类似的东西。下面是我的代码。我怎样才能解决这个问题 import UIKit class ViewController: UIViewController { @IBOutlet var myTableView: UITableView! var myData:NSMutableArray = NSMutableArray
import UIKit
class ViewController: UIViewController {
@IBOutlet var myTableView: UITableView!
var myData:NSMutableArray = NSMutableArray()
func getManufacturer2(){
var url : String = "http://00.00.000.000/myWebService"
var request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary
if (jsonResult != nil) {
var userMomentList:NSMutableArray = NSMutableArray()
self.myData = jsonResult.objectForKey("cevap") as NSMutableArray
self.myTableView.reloadData()
} else {
// couldn't load JSON, look at error
println("jsonResult is nil")
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
getManufacturer2()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(self.myData.count > 0) {
return myData.count
}
return 0;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "myTableCell")
var data = self.myData.objectAtIndex(indexPath.row) as NSDictionary
cell.textLabel?.text = data.objectForKey("item_title") as NSString
cell.detailTextLabel?.text = data.objectForKey("item_value") as NSString
return cell
}
}
导入UIKit
类ViewController:UIViewController{
@IBVAR myTableView:UITableView!
var myData:NSMutableArray=NSMutableArray()
func getManufacturer2(){
变量url:字符串=”http://00.00.000.000/myWebService"
var请求:NSMutableURLRequest=NSMutableURLRequest()
request.URL=NSURL(字符串:URL)
request.HTTPMethod=“GET”
NSURLConnection.sendAsynchronousRequest(请求,队列:NSOperationQueue(),completionHandler:{(响应:NSURLResponse!,数据:NSData!,错误:NSError!)->中无效
var错误:AutoreleasingUnsafeMutablePointer=nil
将jsonResult:NSDictionary!=NSJSONSerialization.JSONObjectWithData(数据,选项:NSJSONReadingOptions.MutableContainers,错误:error)设为?NSDictionary
if(jsonResult!=nil){
var userMomentList:NSMutableArray=NSMutableArray()
self.myData=jsonResult.objectForKey(“cevap”)作为NSMutableArray
self.myTableView.reloadData()
}否则{
//无法加载JSON,请查看错误
println(“jsonResult为零”)
}
})
}
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
getManufacturer2()
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
如果(self.myData.count>0){
返回myData.count
}
返回0;
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
let cell:UITableViewCell=UITableViewCell(样式:UITableViewCellStyle.Value1,重用标识符:“myTableCell”)
var data=self.myData.objectAtIndex(indexPath.row)作为NSDictionary
cell.textlab?.text=data.objectForKey(“项目标题”)作为NSString
cell.detailTextLabel?.text=data.objectForKey(“项值”)作为NSString
返回单元
}
}
您将以numberOfRowsInSection的形式返回零。您将以numberOfRowsInSection的形式返回零。用户界面上的更新必须始终在主线程中完成,因此请尝试包装您的tableView重新加载:
dispatch_async(dispatch_get_main_queue(), {
self.myTableView.reloadData()
})
用户界面上的更新必须始终在主线程中完成,因此请尝试包装tableView重新加载:
dispatch_async(dispatch_get_main_queue(), {
self.myTableView.reloadData()
})
我将其更改为:func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{return self.myData.count}但它仍然是一样的..我将其更改为:func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{return self.myData.count}但它仍然是一样的。。