我有一個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
}
因此,首先設計你的數據源。 – 2015-02-17 22:33:24