2016-12-05 24 views
0
相應部分下

問題:下面提到的代碼填充兩者的區段值,其中它應該根據有關節頭填充(第即:主動和Finished)顯示列表中夫特3

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


    let cellIdentifier = "MainTableViewCell" 
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! MainTableViewCell 
    DispatchQueue.main.async{ 

     let theGame = self.game[indexPath.row] 
     if (theGame.status == "Finished") 
     { 
      let finishedGame = theGame 
      cell.userName?.text = finishedGame.myTeamState.team.players[1].username 

     } 
     else{ 
      let otherGame = theGame 
      cell.userName?.text = otherGame.myTeamState.team.players[1].username 

     } 

    } 
    return cell 
} 

我認爲,我無法理解如何讓標題部分在其中嵌套值/填充列表。

在此先感謝。

回答

1

首先從你的遊戲陣列創建兩個數組:

var finishedArray = [] 
var activeArray = [] 

for item in game { 
    if item.status == "Finished" { 
    finishedArray.append(item) 
}else { 
    activeArray.append(item) 
} 
} 

tableView.reloadData 

在此之後,使用numberOfRowsInSection方法:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     if section == 0 { 
      return finishedArray.count 
     }else { 
      return activeArray.count 
     } 
    } 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


    let cellIdentifier = "MainTableViewCell" 
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! MainTableViewCell 

     if (indexPath.section == 0) { 
      let theGame = self.finishedArray[indexPath.row] 
      let finishedGame = theGame 
      cell.userName?.text = finishedGame.myTeamState.team.players[1].username 

     } 
     else{ 
      let theGame = self.activeArray[indexPath.row] 
      let otherGame = theGame 
      cell.userName?.text = otherGame.myTeamState.team.players[1].username 

     } 

    return cell 
} 

您還需要添加numberOfSections和numberOfRowsInSection方法爲好。

+0

我已經加入他們 FUNC numberOfSections(在的tableView:的UITableView) - >內部{ 返回2 } FUNC的tableView(_的tableView:UITableView的,numberOfRowsInSection部的:int) - >內部{ 返回gameList.count } – Prateekro

+0

這是不正確的。您需要在每個部分添加確切的行數。最好將數組分隔成活動和完成的數組。 –

+0

謝謝。你能不能請telI,將數組分成兩部分。我如何需要將部分列表添加到部分。 這將是一個很大的幫助。 – Prateekro