2015-01-22 2 views
0

위임/데이터 소스에 값 (자체)을 할당했지만 reloadData가 작동하지 않습니다.
그래서 중단 점을 설정하고 dataSource에 데이터가 없습니다.
어떻게 reloadData를 작동합니까? 여기에 내 예제 코드가있다. Swift로 구문 분석 한 후 reloadData가 작동하지 않습니다.

func getData(query: String) { 
    // ...(data processing part) 
    var parser: NSXMLParser! = NSXMLParser(data: data) 
    parser.delegate = self 
    parser.parse() // parsing works well. 
    self.testTableView.hidden = false // breakpoint 1 
    self.testTableView.dataSource = self 
    self.testTableView.delegate = self 
    self.testTableView.reloadData() 
    // breakpoint 2 
    insertTableWithConstraint() // set constraints 
    // init. dataSource 
    self.testTableView.dataSource = nil 
} 

나는 "~. testTableView" "self.testTableView. ~"모두 시도했지만 그것은 작동하지 않았다.
여기 내 중단

testTableView = (jQuery과) 00000001360dee00
  일부 = 가변 세트이다 (jQuery과) 0x00000001360dee00
    ...
    _dataSource = (sunny.TestViewController *) 0x135d34950
      [0] (sunny.TestViewController)
    ...
testDatas = (NSMutableArray) "4 값"

내 CellForRowAtIndex 코드를 추가합니다.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell 
    cell.textLabel.text = testDatas.objectAtIndex(indexPath.row).valueForKey("title") as NSString 
    cell.detailTextLabel?.text = testData.objectAtIndex(indexPath.row).valueForKey("roadAddress") as NSString 
    NSLog("cell.textLabel.text : \(cell.textLabel.text)") // It was not shown in exec. 
    NSLog("cell.detailTextLabel.text : \(cell.detailTextLabel?.text)") // It was not shown in exec. 
    return cell as UITableViewCell 
} 

numberOfRowsInSection 코드를 추가하는 것을 잊어 버렸습니다. 그래서 나는 그것을 덧붙인다.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return testDatas.count 
} 
+0

당신이 self.testTableView.dataSource = nil''설정하는 이유 : 그런 다음이 시도? 그러면 테이블이 지워집니다. – Paulw11

+0

cellForRowAtIndexpath 및 numberOfRow 메소드를 표시하십시오. – Rashad

+0

tableView를 제공하십시오 : cellForRowAtIndexPath 코드 – maxpovver

답변

0

1) self.testTableView.dataSource = nil으로 전화하지 마십시오. 앱이 없어도 충돌이 발생하면 데이터가 비어 있지 않은지 먼저 확인하지 않고 데이터에 액세스하려는 것일 수 있습니다.

2) 귀하의 NSXMLParser이 (가) UITableView 개의 행을 채우는 데 사용되는 것으로 가정합니다. NSXMLParser은 위임 메커니즘과 함께 작동하므로 지금 당장 수행하는 것과 달리 파싱을 완료 한 후에 만 ​​testTableView.reloadData()으로 전화해야합니다. 그것을 호출하기에 좋은 곳은 parserDidEndDocument 델리게이트 함수입니다.

3) func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 함수에서 testDatas 변수 (XML 구문 분석 중에 만들어진 것으로 가정)에서 액세스 할 수있는 값에 indexPath이 해당하는지 확인합니다. 이는 앱이 지금 충돌하는 이유 일 수 있습니다.

4) 또한 대리자 기능 (아마도 testDatas.count을 사용)에서 셀 수를 적절하게 설정하는 데주의해야합니다.

+0

감사합니다. reloadData를 배치했지만 작동하지 않습니다. cellForRowIndexPath에 중단 점을 설정했습니다. 아무 반응이 없습니다. –

+0

또한 "self.testTableView.dataSource = nil"이 (가) 삭제되지 않습니다. –

+0

'parserDidEndDocument'에 중단 점을 설정하고 호출되고 있는지 확인 하시겠습니까? – Romain

0

우선 테이블 뷰의 dataSource & delegate부터 nil까지를 설정하지 않으려면 self으로 설정하십시오.

당신이 말한대로 numberOfRowsInSection은 데이터 소스 개체의 정확한 카운트를 반환합니다. 이 도움이

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { 
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 
    cell.textLabel.text = testDatas.objectAtIndex(indexPath.row).valueForKey("title") as NSString 
    return cell 
} 

희망 ... :)

관련 문제