2017-05-21 41 views
1

我有一個視圖控制器中的2個原型單元的tableview。我希望每個單元顯示來自不同陣列的數據。如何在Swift中將具有不同類型原型單元格的第二部分添加到tableview中?

以下是我的代碼。我可以得到桌面視圖來顯示工作或學校,但不是兩者。我不明白當每個單元格包含不同的數據源時,如何使表格顯示單元格1(作業),然後單元格2(學校)。

screenshot

Import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 


let jobs = ["McDonalds", "Hardees", "Taco Bell"] 
let schools = ["Univ of CO", "Univ of TX", "Univ of CA"] 


override func viewDidLoad() { 
    super.viewDidLoad() 

} 


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return jobs.count 

} 


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "JobsCell", for: indexPath) as! Jobs 

     let job = jobs[indexPath.row] 
     cell.jobLbl.text = job 

     return cell 


    } 

} 

答:我加入numberOfSections功能(感謝羅伯特)解決了這個問題。下面,如果任何人有這個問題,更新後的代碼:

進口的UIKit

類的ViewController:UIViewController的,的UITableViewDelegate,UITableViewDataSource {

let jobs = ["McDonalds", "Hardees", "Taco Bell"] 
let schools = ["Univ of CO", "Univ of TX", "Univ of CA", "Univ of Camdenton"] 


override func viewDidLoad() { 
    super.viewDidLoad() 
} 


func numberOfSections(in tableView: UITableView) -> Int { 
    return 2 
} 


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if (section == 0) { 
     return jobs.count 
    } else { 
     return schools.count 
    } 

} 


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if indexPath.section == 0 { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "JobsCell", for: indexPath) as! Jobs 
     let job = jobs[indexPath.row] 
     cell.jobLbl.text = job 
     return cell 

    } else { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "SchoolsCell", for: indexPath) as! Schools 
     let school = schools[indexPath.row] 
     cell.schoolLbl.text = school 
     return cell 

    } 

} 

}

+0

我編輯您的標題,使之更加具體到你的問題。如果您不同意,請隨時將其回滾。 – Robert

回答

1

聽起來好像要顯示一個部分與工作單元,然後第二部分與學校的單元格。如果是這種情況,則需要將部分數設置爲2,然後根據部分編號重新編寫委託函數以作出適當響應。

因此numberOfRowsInSection將不得不檢查節號,然後返回該特定節的行數。並且cellForRowAt將需要檢查該部分,然後設置並返回給定行的作業或學校單元格。

下面是什麼樣子在你的榜樣:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    let jobs = ["McDonalds", "Hardees", "Taco Bell"] 
    let schools = ["Univ of CO", "Univ of TX", "Univ of CA"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 2 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     switch(section) { 
     case 0: return jobs.count 
     default: return schools.count 
     } 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     switch(indexPath.section) { 
     case 0: 
      let cell = tableView.dequeueReusableCell(withIdentifier: "JobsCell", for: indexPath) as! Jobs 
      let job = jobs[indexPath.row] 
      cell.jobLbl.text = job 
      return cell 
     default: 
      let cell = tableView.dequeueReusableCell(withIdentifier: "SchoolsCell", for: indexPath) as! Schools 
      let school = schools[indexPath.row] 
      cell.schoolLbl.text = school 
      return cell 
     } 
    } 

} 

下面是在模擬器輸出:

Simulator output image

+0

感謝羅伯特讓我走上正軌。我修改了我的代碼並添加了numberOfSections,它起作用了!我將在我原來的帖子中分享我的更新代碼。 – Zach

相關問題