2016-07-10 1 views
1

그래서, 나는 parse.comSWIFT : 완료 핸들러

enter image description here

에서 방법 loadData() 다운로드 datas를하고 난 모든 이미지는 테이블보기에 표시 제시한다.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("ReusableCell", forIndexPath: indexPath) as! LeaguesTableViewCell 

    loadData { (success) in 
     if success { 
      cell.leagueImage.image = UIImage(data: self.leaguesImage[indexPath.row]) 
      cell.leagueNameLabel.text = self.leagues[indexPath.row]["name"] as? String 
     } else { 
      cell.leagueNameLabel.text = "Wait" 
     } 
    } 


    return cell 

} 

해당 기능이 작동하지 않습니다. 내가 viewDidLoad()에서 내 기능을 호출하지만 그 역시 정확하지 않다. 테이블보기는 비어있다. 내 배열 사촌

enter image description here

+0

당신이 그것을에 삽입하기 전에 제대로 배열을 "잠금"된다, 또한 ... "작동하지 않았다"설명? – Brandon

+0

그냥 cellForAtIndexPath 메소드에 가지 마라. 테이블 뷰가 비어있다. –

+0

셀이 보이지 않으면 delegate 구현의 'numberOfRowsInSection'과'numberOfSectionsInTableView'가 틀리다 (0을 반환). 그 또는 결코 당신은 tableView 데이터 소스 및 위임을 설정하지 마십시오. – Brandon

답변

1

시도 첫번째 위임 및 데이터 소스를 설정하는 비어 있습니다. 뷰 컨트롤러가 아닌 별도의 데이터 소스가있는 경우 콜백을받지 않으려면 그렇지 않은 상태로 유지하십시오.

+0

class LeaguesTableViewController : UITableViewController, 아무 것도 필요하지 않습니다 –

+0

self.tableView.reloadData()를 사용해 보셨나요? – Arsalan

+0

예, 모든 것을 변경 한 후에이 작업을 수행합니다. (didSet) –

2

jQuery과에 데이터를로드하기위한 기본 절차는 다음과 같습니다

  1. 로드 데이터
  2. 새로 고침 테이블 뷰
  3. 돌아 numberOfSectionsInTableView: 방법 섹션의 수 : 귀하의 경우에는 단 1이됩니다 섹션.
  4. tableView:numberOfRowsInSection:에있는 행 수를 반환하십시오. 귀하의 경우 데이터가로드되면 리그 수를 반환하십시오. 데이터가로드되지 않은 경우 1을 반환하여 테이블보기에 "대기"메시지를 표시 할 행이 하나 이상 있어야합니다.
  5. 데이터에서 셀을 만들고 채 웁니다. leaguesleaguesImage을 사용하십시오.

예 :

private var loaded = false 

override func viewDidLoad() { 
    super.viewDidLoad() 

    loaded = false 

    loadData() { success in 
     NSOperationQueue.mainQueue().addOperationWithBlock() { 
      self.loaded = success 
      self.tableView.reloadData() 
     } 
    } 
} 

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

override func tableView(tableView: UITableView, numberOfRowsInSection: Int) -> Int { 
    if loaded { 
     return leagues.count 
    } 
    else { 
     return 1 
    } 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("ReusableCell", forIndexPath: indexPath) as! LeaguesTableViewCell 

    if loaded { 
     cell.leagueImage.image = UIImage(data: self.leaguesImage[indexPath.row]) 
     cell.leagueNameLabel.text = self.leagues[indexPath.row]["name"] as? String 
    } 
    else { 
     cell.leagueNameLabel.text = "Wait" 
    } 

    return cell 
} 
+1

아무 것도 표시하기 전에 이미지를 검색 할 때까지 기다릴 필요는 없지만 대부분 정확합니다. 그는'Leagues' 페치를 수행해야하는데 (꽤 빠름), 이미지를 검색하지 않습니다. 그리고'cellForRowAtIndexPath'는 느리게 이미지를 가져와야합니다. 이미지는 커질 수 있으며 검색하는 사람의 수를 모르기 때문에 모든 이미지를 검색하는 데 시간이 많이 걸릴 수 있습니다. 특히 첫 번째 이미지 만 필요하기 때문에 더욱 그렇습니다. – Rob

관련 문제