ios - 在 Swift 3 中相应部分下的 UiTableView 中显示列表

标签 ios uitableview swift3

问题:下面提到的代码为它应该填充在相关部分标题下的两个部分填充了值(部分即:事件和已完成)

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
}

我认为,我无法理解如何让 header 部分在其中嵌套值/填充列表。

提前致谢。

最佳答案

首先从您的游戏数组创建两个数组:

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 方法。

关于ios - 在 Swift 3 中相应部分下的 UiTableView 中显示列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40968876/

相关文章:

json - 使用 ObjectMapper 和 swift 3 通用化将 JSON 映射到对象的函数

ios - 快速在自定义容器 View 下添加阴影

iphone - 如何从 Xcode 中删除运行脚本阶段

iOS开发,我们真的需要iPhone/iPod/iPad吗?

ios - PushNotification OneSignal 问题

ios - 在 ViewController 中使用带有外部数据源和 UITableView 的自定义 UITableViewCell

swift - 使用控件将 spritenode 移动一圈

swift - 谁能告诉我为什么我的过滤数组是空的?

ios - 如何在 iOS 7 的 UITableViewCell 左侧获取复选标记,就像在“设置”中一样?

ios - scrollToRowAtIndexPath :atScrollPosition causing table view to "jump"