2016-06-22 1 views
1

전무 도착하지만 dispatch_async에서 실행할 때 당신은 주 스레드에서 self.data을 수정해야 tableViews 기능 numberOfRowsInSection배열 GCD

dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { 
    self.data = self.dataOfJson("http://192.168.1.100/practice/studentCourseSelection.php?ID=\(NSUserDefaults.standardUserDefaults().objectForKey("currentUser")!)") 
    self.RefreshTableView() 
} 

func RefreshTableView() 
{ 
    dispatch_async(dispatch_get_main_queue()){ 
     self.tableView.reloadData() 
    } 
} 
func dataOfJson(url:String) -> NSArray 
{ 
    var jsonArray : NSMutableArray = [] 

    let data = NSData(contentsOfURL: NSURL(string : url)!) 
    jsonArray = try! NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSMutableArray 



    return jsonArray 
} 

enter image description here

+0

'return self.data.count'를'return self.data.count ?? 0' –

+0

코드를 보여주세요! 'self.dataOfJson'은 무엇입니까? 당신의 코드는'numberOfRowsInSection'과 다른 두 가지 큰 질문을 어떻게 구현합니까? – matt

+0

self.data가 nil 일 때 count를 nil로 호출하면 EXC_BAD_INSTRUCTION RED ERROR가 발생합니다. 당신은 체크하지 않아도된다. – Cruz

답변

2

에서 오류를 가져옵니다.

dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { 
    let data = self.dataOfJson("http://192.168.1.100/practice/studentCourseSelection.php?ID=\(NSUserDefaults.standardUserDefaults().objectForKey("currentUser")!)") 
    self.RefreshTableView(data)  
} 

func RefreshTableView(data: ??) { 
    dispatch_async(dispatch_get_main_queue()){ 
     self.data = data 
     self.tableView.reloadData() 
    } 
} 
+0

아직 작동하지 않습니다 :/ –

+0

고마워요 .. 수정되었습니다 ..하지만 고맙습니다. –