同一个UITableView中的不同单元格

时间:2015-02-17 22:22:04

标签: ios uitableview swift

我有一个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://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个pubEvent数,因此需要计算每个部分的数量。

这是我的问题:

我需要在tableview中插入pubevents(如果有的话)和匹配(如果有的话),但是我无法用Swift来解决它。

修改

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

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

    }

1 个答案:

答案 0 :(得分:1)

当您获得提供单元格的索引路径时,请查看该行是否小于匹配数。如果是,请使用该匹配。如果它不小于,则从该行中减去匹配的数量,并将其用作所需的pub事件的索引。