2016-11-29 61 views
0

在下面的代碼中,我爲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

我想輸出來表示一個表中的以下內容:

+---------------+---------+-------+-------+------------+--------------+ | Bobby Charles | Man Utd | 2 | 3 | Fiorentina| Andrew James | | Ryan Stood | Arsenal | 0 | 1 | Chelsea | Bobby Charles| | Bobby Charles | Chelsea | 0 | 2 | Liverpool | Ryan Stood | +---------------+---------+-------+-------+------------+--------------+

+0

要有collumns,不僅行,用'UICollectionView',而不是'UITableView'。 – shallowThought

+0

非常感謝 - 那麼最好的方法是基本顯示足球比賽結果列表?從數據庫中的結果表中拉出。 – RDowns

+1

您可以使用自定義單元格(UITableViewCell)。您不一定必須使用UICollectionView。 –

回答

1
  1. 採取一個UITableViewCell
  2. 增加6個UILabels到該小區
  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 
    } 
    

注: 維護,許多陣列是不是一個好主意。

使用結構代替,處理data.it更容易和更簡單的

0

確保你恰恰意味着列或行。

你的cellForRowAtindexPath是如此不妥。你在那裏沒有做任何事情。

要回答你的問題,你將不得不作出一個數組追加home_playerhome_goalaway_playeraway_teamaway_goals然後在您的cellForRowAtindexPath相應的配置。儘管也有其他方法可以達到同樣的效果。

注意:我建議你閱讀UITableView的一些教程。

+0

我已更新我的問題,以顯示我所需的輸出。這可以使用'cellForRowAtindexPath'來完成嗎? – RDowns