不同的细胞在相同的UITableView

问题描述:

我有一个JSON结果,看起来像这样:不同的细胞在相同的UITableView

[{ 
    "EventDate": "2015-02-19", 
    "PubEvents": [{ 
     "Title": "Ladies Night", 
     "Description": "Every thursday is Ladies Night at the Irish House.\nLadies: 2 cocktails for the price of 1 - 1 pint of Crowmoor Cider - 30 kr", 
     "EventType": "LadiesNight", 
     "Start": "2015-02-19", 
     "End": "2015-02-20", 
     "StartTime": "19:00", 
     "EndTime": "02:00", 
     "Image": "ladies.jpg" 
    }, { 
     "Title": "Pat Kelly", 
     "Description": "Pat Kelly from Ireland has extensive experience in entertaining all nationalities, as he has travelled around the world from New York to Amsterdam. He has a very wide repertoire maintaining a traditional approach, and is well received and appreciated for his ability to get his audience excited every night he hits the stage. A “sure thing” evening you will get with the talented Pat.", 
     "EventType": "Music", 
     "Start": "2015-02-19", 
     "End": "2015-02-20", 
     "StartTime": "21:30", 
     "EndTime": "01:00", 
     "Image": "http://domain.dk/Images/Musicians/kelly" 
    }], 
    "Matches": [{ 
     "EventType": "Sports", 
     "Start": "2015-02-19", 
     "End": "2015-02-19", 
     "StartTime": "18:00", 
     "EndTime": "19:00", 
     "HomeTeam": { 
      "Id": 0, 
      "TeamName": "Hold", 
      "HomeImageUrl": "defaultHome.png", 
      "AwayImageUrl": "defaultAway.png", 
      "Badge": "defaultBadge.png" 
     }, 
     "AwayTeam": { 
      "Id": 0, 
      "TeamName": "AndetHold", 
      "HomeImageUrl": "defaultHome.png", 
      "AwayImageUrl": "defaultAway.png", 
      "Badge": "couldn't get an away team" 
     } 
    }] 
}, { 
    "EventDate": "2015-02-20", 
    "PubEvents": [{ 
     "Title": "Pat Kelly", 
     "Description": "Pat Kelly from Ireland has extensive experience in entertaining all nationalities, as he has travelled around the world from New York to Amsterdam. He has a very wide repertoire maintaining a traditional approach, and is well received and appreciated for his ability to get his audience excited every night he hits the stage. A “sure thing” evening you will get with the talented Pat.", 
     "EventType": "Music", 
     "Start": "2015-02-20", 
     "End": "2015-02-21", 
     "StartTime": "22:30", 
     "EndTime": "02:00", 
     "Image": "http://domain.dk/Images/Musicians/kelly" 
    }], 
    "Matches": [] 
}, 

格式事件的数组:

var eventDate : String? 
var pubEvents : Array<PubEvent>? 
var matches : Array<Match>? 

numberOfSectionsInTableView是eventarray.count,因为每日期是不同的

numberOfRowsInSection:

let event = self.eventarray[section] 
let matchesCount = event.matches?.count ?? 0 
let pubEventsCount = event.pubEvents?.count ?? 0 
return matchesCount + pubEventsCount 

由于每个部分都有x个匹配数和x个pubEvents数,所以我需要每个部分的总数。

这里是我的问题:

我需要插入pubevents(如果有的话)和匹配(如果有的话)在tableview中,但我不能与斯威夫特弄明白。

编辑

菲利普·米尔斯帮助:一个非常快速的实现:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell 
     let event = self.eventarray[indexPath.section] as SortedEvent 
     let matchesCount = event.matches?.count ?? 0 
     let pubEventsCount = event.pubEvents?.count ?? 0 

     if indexPath.row < matchesCount{ 
      cell.textLabel?.text = "\(event.matches![indexPath.row].homeTeam!.teamName!) v \(event.matches![indexPath.row].awayTeam!.teamName!)" 
      cell.detailTextLabel?.text = "\(event.matches![indexPath.row].startTime!) - \(event.matches![indexPath.row].endTime!)" 
     } 
     else{ 
      cell.textLabel?.text = event.pubEvents![indexPath.row - matchesCount].title! 
      cell.detailTextLabel?.text = "\(event.pubEvents![indexPath.row - matchesCount].startTime!) - \(event.pubEvents![indexPath.row - matchesCount].endTime!)" 
     } 
     return cell 

    } 
+0

因此,首先设计你的数据源。 – 2015-02-17 22:33:24

当你给一个索引路径提供一个单元格,行是否小于计数火柴。如果是,请使用该匹配项。如果它不少,则从行中减去匹配计数并将其用作所需酒吧事件的索引。

+0

如何获取cellForRowAtIndexPath中的匹配计数 – catu 2015-02-17 22:54:20

+0

您将得到一个包含部分的索引路径,并且您已经有了代码:'let event = self.eventarray [section]let matchesCount = event.matches?.count ?? 0' – 2015-02-18 01:14:02

+0

太棒了。问题是我认为indexpath是从0到eventcount的线性关系 - 事实上,它从0开始在每个部分帮了很多:)(doh) 谢谢先生 – catu 2015-02-18 07:58:24