ios - 如何向此 UITableView 添加额外的列并填充已获取的数据?

标签 ios swift uitableview

在下面的代码中,我为 UITableView 获取了一些数据。

目前,该表显示一列数据“homeTeam”。

我想要的输出是在 UITableView 中有多个列。

我已经获取了其他列的数组,可以在下面的代码中看到。

import UIKit

class ShowResultsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


var personalPlayerId: String?
var communityId: String?
var personalPlayer2Id: String?
var postString: String?

var homeTeam = [String?]()
var homePlayer = [String?]()
var homeGoals = [String?]()

var awayTeam = [String?]()
var awayPlayer = [String?]()
var awayGoals = [String?]()

var matchId = [String?]()

var tempResult: Int?

var selectedMatch: String?

var headToHead: Bool?

@IBOutlet weak var leagueTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()



    self.leagueTableView.delegate = self
    self.leagueTableView.dataSource = self


}

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

    return self.homeTeam.count
}

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

    let title = self.homeTeam[indexPath.row]
    let cell = UITableViewCell()

    return cell

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    tempResult = indexPath.row

    if let tempShowResult = (self.homeTeam[tempResult!]){
        self.selectedMatch = tempShowResult
    }

}

override func viewDidAppear(_ animated: Bool) {
    let myUrl = URL(string: "http://www.???.uk/???/getLeagueResults.php")
    var request = URLRequest(url:myUrl!)
    request.httpMethod = "POST"

    if self.headToHead == true {
        self.postString = "player_id=\(self.personalPlayerId!)&player2_id=\(personalPlayer2Id!)&community_id=\(communityId!)";
    }else{
        if self.headToHead == false{
            self.postString = "player_id=\(self.personalPlayerId!)&community_id=\(communityId!)";
        }
    }

    request.httpBody = self.postString?.data(using: String.Encoding.utf8);
    let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
        DispatchQueue.main.async
            {
                do{

                    let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
                    print (json!)


                        // GET DATA FOR TABLE COLUMNS


                            if let arr = json?["allPersonalLeagueStats"] as? [[String:String]] {
                                self.homeTeam = arr.flatMap { $0["player1_team"]!}
                                self.homePlayer = arr.flatMap { $0["player1_name"]!}
                                self.homeGoals = arr.flatMap { $0["player1_goals"]!}

                                self.awayTeam = arr.flatMap { $0["player2_team"]!}
                                self.awayPlayer = arr.flatMap { $0["player2_name"]!}
                                self.awayGoals = arr.flatMap { $0["player2_goals"]!}

                                self.matchId = arr.flatMap { $0["results_id"]!}
                            }


                       self.leagueTableView.reloadData()
                    }


                catch{
                    print(error)
                }
        }
    }
    task.resume()


}


}

如何向该表添加更多列以显示额外的列; home_player, home_goal, away_player, away_team, away_goals?

我希望输出在表格中表示以下内容:

+----------------+--------+--------+--------+- ----------+------------+ |鲍比查尔斯 |曼联 | 2 | 3 |佛罗伦萨|安德鲁·詹姆斯 | |瑞安站着 |阿森纳 | 0 | 1 |切尔西 |鲍比·查尔斯| |鲍比查尔斯 |切尔西 | 0 | 2 |利物浦 |瑞安站着 | +----------------+--------+--------+--------+------ -----+----------------+

最佳答案

  1. 拿一个 UITableViewCell
  2. 向此单元格添加六个 UILabel
  3. 由于每个数组都有不同的数组,因此在 cellForRowAtindexPath 中将数据添加到这些标签。

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:    NSIndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTableViewCell
    
            cell.label1.text = self.homeTeam[indexPath.row]
            cell.label2.text = self.homePlayer[indexPath.row]
            cell.label3.text = self.homeGoal[indexPath.row]
            cell.label4.text = self.awayTeam[indexPath.row]
            cell.label5.text = self.awayPlayer[indexPath.row]
            cell.label6.text = self.awayGoals[indexPath.row]
    
            return cell
    }
    

注意: 维护那么多数组不是一个好主意。

Use Struct instead, to handle the data.it is easier and simpler

关于ios - 如何向此 UITableView 添加额外的列并填充已获取的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40868366/

相关文章:

ios - 如何根据目标名称进行条件编译?

c++ - 在独立的 Xcode playground 中运行 c 或 c++ 代码

swift - NSWindow : change positions of window buttons

ios - 如何在一起滚动的 UITableView 顶部添加一个 View ,但在滚动后坚持到顶部

ios - 根据文本的总宽度调整 UILabels 的大小

iphone - iOS - 有清理 HTML 代码的好方法吗?

c++ - 作为可选函数指针的类参数不被识别为函数

swift - +/- 购物车 View Controller 中的产品数量,每个按钮都位于 TableViewCell 上,它从数组中获取默认数量作为整数

ios - 如何获取当前显示的 UIViewController 不在 AppDelegate 中?

iOS UIPageControl 在点击左右时改变行为