2016-07-07 31 views
0

我有一個UITableView,其中有三個部分(0,1,2)。部分0可以有多個單元格,而部分1和2只有一個唯一的單元格。我無法弄清楚什麼,是如何有第1,第2和第0換言之每個單元后有相應的細胞出現,我希望我的UITableView有以下結構:需要部分出現在使用Swift的UITableView中的每一行之後

Section 0 
row 0 
Section 1 
row 0 
Section 2 
row 0 
Section 0 
row 1 
Section 1 
row 0 
Section 2 
row 0 
Section 0 
row 2 
Section 1 
row 0 
Section 2 
row 0 
... 
and so on. 

目前,不幸的是,我有以下結構來代替:

Section 0 
row 0 
row 1 
row 2 
... 
Section 1 
row 0 
Section 2 
row 0 

,因爲我不知道什麼是相關我還沒有提供任何代碼。

回答

1

如果您瞭解哪些行和部分是非常容易的。

UITableViewDataSource方法,指定:

每部
  1. 一行(無論哪個部分的),即每個條目

    func tableView(tableView: UITableView, numberOfRowsInSection:Int) -> Int 
    { 
        return 1 // All sections have one row 
    } 
    
  2. 一個部分中數據模型/陣列/等。

    func numberOfSectionsInTableView(tableView: UITableView) -> Int 
    { 
        return myArray.count // One section per item(row) 
    } 
    

    ...然後,配置每個單元格時,看看傳遞給tableView(_: cellForRowAtIndexPath:)的TE指數路徑的部分:

    let item = mayArray[indexPath.section] 
    // configure cell using item, and return it 
    
0

你想你的tableview中的第一部分有多個章節和休息部分每個有1個單元格。首先在代碼中將你的tableview連接到IBOutlet。然後在你的類聲明有委託和數據源一樣

class HomeViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { 

然後在viewDidLoad中

tableView.delegate = self 
tableView.dataSource = self 

後實現了數據源功能

func tableView(tableView: UITableView, numberOfRowsInSection:Int) -> Int 
{ 
if section == 0{ 
return 3 // your value to return rows for 1st section 
}else{ 
return 1 // rest for all sections 
} 
} 

,當你想獲得儘可能多的部分

func numberOfSectionsInTableView(tableView: UITableView) -> Int 
{ 
return value // your value to return for number of sections 
} 
相關問題