2016-11-15 40 views
0

我有一個tableview,我有一個部分和兩個單元格。第一個單元格用於顯示分區名稱,第二個單元格用於顯示相應的分區名稱。TableView - 多個單元格處理

var Division = [「Dhaka」,」Khulna」] 
var Districts = [[「Gazipur」,」Gopalganj」,」Faridpur」],[「Bagerhat」,」Jessor」,」Chuadanga」]] 

我需要像這樣在表格視圖中呈現。

Dhaka 
Gazipur 
Gopalganj 
Faridpur 

Khulna 
Bagerhat 
Jessor 
Chuadanga 

起初我接近像傳統的方式,但我無法前進時,有必要在cellForRowAtIndexPathnumberOfRowsInSection協議:(

請提出的方法。在此先感謝定義:)

+0

,而不是1段和2電池使用2節以 'N' 細胞。 'n'是每個部分中的單元格數量。 – Adeel

回答

0

如果我明白你的問題正確

這裏分工將是你的節頭

override func numberOfSectionsInTableView(tableView: UITableView) -> Int 
{ 
    return Division.count 
} 

和地區將是你行

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
return Districts.count 
} 

顯示別忘了添加方法,返回視圖的頭和的cellForRowAtIndexPath。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
} 

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{ 
} 
+0

謝謝..但我只需要一個部分...難以前進:( – KhanShaheb

+0

主標題可以是你的tableview標題和司將是你的部分標題。如果你給我看任何圖像或任何圖形(你想做什麼),那麼我可以更好地幫助你 –

0

對於部分的數量,您可以使用:

func numberOfSections(in tableView: UITableView) -> Int 
{ 
    return Districts.count 
} 

然後,指定在每個部分的行數,你可以使用:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
    return Districts[section].count 
} 

終於可以設置你的手機是這樣的:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let cell = self.TableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell 
    let cellText = Districts[indexPath.section][indexPath.row] 
    cell.Label.text = cellText 
    return cell 
} 

對於的TableView頭,你可以這樣寫:

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? 
{ 
    if section < Division.count 
    { 
     print(Division[section]) 
     return Division[section] 
    } 
    else 
    { 
     return nil 
    } 
} 

和你TableViewCell類是:

import UIKit 

class TableViewCell: UITableViewCell 
{ 
@IBOutlet weak var Label: UILabel! 
override func awakeFromNib() 
{ 
    super.awakeFromNib() 
} 

override func setSelected(_ selected: Bool, animated: Bool) 
{ 
    super.setSelected(selected, animated: animated) 

} 

} 
+0

感謝你的努力,但我只想用兩個單元格來完成它,第一個單元格用於分區名稱,第二個單元格用於它們各自的城市。 – KhanShaheb