我有一个json结果,看起来像这样:
[{
"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://irishhouse.appninja.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://irishhouse.appninja.dk/Images/Musicians/kelly"
}],
"Matches": []
},
格式为:
的事件数组var eventDate : String?
var pubEvents : Array<PubEvent>?
var matches : Array<Match>?
numberOfSectionsInTableView是eventarray.count,因为每个日期都是不同的
我正在努力使用numberOfRowsInSection。它应该是
self.eventarray[section].pubEvents?.count or self.eventarray[section].matches?.count
取决于哪个具有最高计数。
这是我第一次尝试做Swift,我很欣赏正确的方向。我试过这个:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var returncount : Int = 1
if let pubeventscount = self.eventarray[section].pubEvents?.count{
if let sportscount = self.eventarray[section].matches?.count{
if pubeventscount > sportscount{
returncount = pubeventscount
}
else{
returncount = sportscount
}
}
}
return returncount
}
但没有结果
答案 0 :(得分:0)
我不知道“没有结果”是什么意思,但是。
您的功能可以简化为:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let event = self.eventarray[section]
let matchesCount = event.matches?.count ?? 0
let pubEventsCount = event.pubEvents?.count ?? 0
return max(matchesCount, pubEventsCount, 1)
}