2016-01-23 70 views
0

我在我的TableView中有多個部分,我有點卡住顯示正確部分的名稱。我是新來的Xcode,所以這一個容易爲多數,但不適合我:■Xcode - TableView - Multible部分

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 

    } 
} 
+0

你忘記返回小區時部分!= 0? – kientux

回答

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 
} 
+0

如果答案有用,請將其標記爲已接受。所以其他用戶會覺得很有幫助。謝謝 – technerd