Я ищу форум, но не нашел решения, соответствующего моей проблеме, поэтому я решил открыть тему.
У меня есть Collectionview в Tableviewcell. Используя Alamofire и SwiftJSON, я загружаю данные и сохраняю их в своем модельном проекте. У меня есть 2 файла модели, один для просмотра таблицы и один для просмотра коллекции
Модельный проект для Tableview:
class Matchs {
var matchTime : String
var matchMbs : Int
var matchLeague : String
var matchCode : Int
var matchHomeTeam : String
var matchAwayTeam : String
init(time : String, mbs : Int, league : String, code : Int, homeTeam : String, awayTeam : String ) {
matchTime = time
matchMbs = mbs
matchLeague = league
matchCode = code
matchHomeTeam = homeTeam
matchAwayTeam = awayTeam
}
}
Модельный проект для Collectionview:
class MatchsOdds {
var homeWin : Double
var awayWin : Double
var draw : Double
var under : Double
var over : Double
var oddResult = ["1","0","2","Under","Over"]
init(homeWin: Double, awayWin : Double, draw : Double, under : Double, over : Double) {
self.homeWin = homeWin
self.awayWin = awayWin
self.draw = draw
self.under = under
self.over = over
}
}
А вот код Viewcontroller:
class MainViewController: UIViewController {
@IBOutlet weak var tableview: UITableView!
let MATCH_URL = "somelink"
var match : [Matchs] = []
var matchOdd : [MatchsOdds] = []
@IBOutlet weak var totalMatchLabel: UILabel!
@IBOutlet weak var totalOddsLabel: UILabel!
fileprivate func getMatchData(url:String) {
Alamofire.request(url).responseJSON {
response in
if response.result.isSuccess {
print("I got the match data")
let matchJSON : JSON = JSON(response.result.value!)
self.getMatch(json: matchJSON)
}else {
print("Match Unavailable")
}
self.tableview.reloadData()
}
}
fileprivate func getMatch(json: JSON) {
if let matchs = json["data"]["b"]["f"]["list"].array {
matchs.forEach { (arg0) in
let events = arg0
let tournamentName = events["tournamentName"].stringValue
let homeTeam = events["homeTeam"].stringValue
let mbs = events["mbs"].intValue
let matchCode = events["matchCode"].intValue
let matchTime : String = events["eventHour"].stringValue
let awayTeam = events["awayTeam"].stringValue
let matchTimeFirst5 = matchTime.prefix(5)
let allMatch = Matchs(time: String(matchTimeFirst5), mbs: mbs, league: tournamentName, code: matchCode, homeTeam: homeTeam, awayTeam: awayTeam)
let homeWin = events["oddList"][0]["v"].doubleValue
let draw = events["oddList"][1]["v"].doubleValue
let awaywin = events["oddList"][2]["v"].doubleValue
let under = events["oddList"][3]["v"].doubleValue
let over = events["oddList"][4]["v"].doubleValue
let fiveOdds = MatchsOdds(homeWin: homeWin, awayWin: awaywin, draw: draw, under: under, over: over)
match.append(allMatch)
matchOdd.append(fiveOdds)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
getMatchData(url: MATCH_URL)
}
override func viewDidAppear(_ animated: Bool) {
}
}
extension MainViewController: UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return match.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let matcCell = match[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "oddsCell") as! OddsTableViewCell
tableView.separatorStyle = .none
cell.matchToMatch(matcs: matcCell)
tableview.allowsSelection = false
cell.collectionview.tag = indexPath.row
cell.collectionview.reloadData()
cell.matchOddCV = matchOdd
return cell
}
}
Нет проблем отсюда. Я могу записывать данные на ярлыки Tableviewcell. Но когда я пытаюсь данные matchOdds к метке ячейки Collectionview. Я получил только первые данные, которые повторяются так: http://i63.tinypic.com/29aqzxu.png
Вот код для Tableviewcell:
class OddsTableViewCell: UITableViewCell {
@IBOutlet weak var reloadButton: RoundedButton!
@IBAction func reloadBottonPressed() {
print("reloadDataPressed")
}
var matchOddCV : [MatchsOdds]!
@IBOutlet weak var collectionview: UICollectionView!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var mbsLabel: UILabel! {
didSet{
mbsLabel.layer.cornerRadius = 5
mbsLabel.layer.masksToBounds = true
}
}
@IBOutlet weak var leageLabel: UILabel!
@IBOutlet weak var matchCodeLabel: UILabel!
@IBOutlet weak var homeVsAwayLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
collectionview.dataSource = self
collectionview.delegate = self
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func matchToMatch (matcs : Matchs) {
timeLabel.text = matcs.matchTime
mbsLabel.text = String(matcs.matchMbs)
leageLabel.text = matcs.matchLeague
matchCodeLabel.text = String(matcs.matchCode)
homeVsAwayLabel.text = "\(matcs.matchHomeTeam) - \(matcs.matchAwayTeam)"
}
}
extension OddsTableViewCell: UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width/5, height: collectionView.bounds.height)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "oddsCollectionViewCell", for: indexPath) as! OddsCollectionViewCell
let matchCell = matchOddCV[indexPath.row]
if indexPath.row == 0 {
cell.matchResultLabel.text = matchCell.oddResult[0]
cell.matchOddsLabel.text = String(matchCell.homeWin)
}else if indexPath.row == 1{
cell.matchResultLabel.text = matchCell.oddResult[1]
cell.matchOddsLabel.text = String(matchCell.draw)
}else if indexPath.row == 2 {
cell.matchResultLabel.text = matchCell.oddResult[2]
cell.matchOddsLabel.text = String(matchCell.awayWin)
}else if indexPath.row == 3 {
cell.matchResultLabel.text = matchCell.oddResult[3]
cell.matchOddsLabel.text = String(matchCell.under)
}else if indexPath.row == 4 {
cell.matchResultLabel.text = matchCell.oddResult[4]
cell.matchOddsLabel.text = String(matchCell.over)
}
cell.matchOddsLabel.layer.borderWidth = 0.5
cell.matchOddsLabel.layer.borderColor = UIColor.lightGray.cgColor
cell.matchResultLabel.layer.borderWidth = 0.5
cell.matchResultLabel.layer.borderColor = UIColor.lightGray.cgColor
return cell
}
}
Collectionviewcell код:
class OddsCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var matchResultLabel: UILabel!
@IBOutlet weak var matchOddsLabel: UILabel!
}