Xcode - TableView - 多个部分

时间:2016-01-23 15:32:57

标签: ios xcode uitableview

我的TableView中有多个部分,而且我有点卡在正确的部分显示名称。我是xcode的新手,所以这对大多数人来说都很简单,但不适合我:s

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 2
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    if section == 0 {
    return areas.bars.count
    } else {

    return areas.clubs.count
}
}


 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("barsandclubsIdentifier", forIndexPath: indexPath)

    if  section == 0 { // **This is where I'm stuck I can't put section -> UITableViewCell**
    let bars = areas.bars
    let bar = bars[indexPath.row]

    cell.textLabel?.text = bar.name

    return cell
    } else {
        let clubs = areas.clubs
        let club = clubs[indexPath.row]

        cell.textLabel?.text = club.name

    }
}

1 个答案:

答案 0 :(得分:1)

试试这可能会对您有所帮助:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("barsandclubsIdentifier", forIndexPath: indexPath)

if  indexPath.section == 0 { // **This is where I'm stuck I can't put section -> UITableViewCell**

    let bars = areas.bars
    let bar = bars[indexPath.row]

    cell.textLabel?.text = bar.name


}else {
    let clubs = areas.clubs
    let club = clubs[indexPath.row]

    cell.textLabel?.text = club.name
}

return cell
}