2016-08-04 26 views

回答

2

當然可以。只需實例化視圖控制器的新實例,將其掛接到數據源並委託並呈現它。

標籤欄控制器中的25個選項卡是瘋狂的。您需要重新考慮您的用戶界面。

0

是的,你可以。

您可以使用開關檢測選定的tabbar項目索引,然後您可以根據您的要求分配數據源。

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

     //suppose index is your tab Bar item index 
     switch index { 
     case 0: 
      return YOUR_DATA_ARRAY1.count 
     case 1: 
      return YOUR_DATA_ARRAY2.count 
      . 
      . 
      . 
     case 25: 
      return YOUR_DATA_ARRAY25.count 
     default: 
      break 
     } 
    } 

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

     let cell = tableView.dequeueReusableCellWithIdentifier("YourCellIdentifier", forIndexPath: indexPath) 

     //You can use same switch here to access objects from your data array and use it to assign values for cell 
     return cell; 
    } 

邊注:其實你的做法是錯誤的,因爲你的數據是非常大的。你仍然可以使用這種方法,但我強烈建議重新考慮它。

0

是的,你可以。我的建議是使用tableView創建一個基本ViewController,並將每個ViewController連接到您的選項卡。然後,所有連接的視圖控制器都應該根據需要更改數據數組。

SO,可以說,你有一個基本的ViewController名稱BaseViewController.swift。在該控制器有一個名爲數據源的數組喜歡 -

var dataSource = [String]() 

然後添加所需的代表和數據源的協議列表,喜歡 -

class BaseViewController: UIViewController, UITableViewDataSource, UITableViewDelegate 

現在後添加您的tableview權利。

var myTableView: UITableView! 

現在在viewDidLoad方法中,添加你的表。

override func viewDidLoad() { 
     super.viewDidLoad() 

     self.myTableView = UITableView.init(frame: self.view.bounds) 
     self.myTableView.delegate! = self 
     self.myTableView.dataSource! = self 
     self.view.addSubview(self.myTableView) 
    } 

現在只需實現數據源方法。

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
     let cell = tableView.dequeueReusableCellWithIdentifier("myTableViewCell", forIndexPath: indexPath) 
     cell.textLabel.text = self.dataSource[indexPath.row] //assuming you only have a string in your array 
     return cell; 
} 

現在你所需要做的就是擴展這個類。假設您的第一個Tab與ViewControllerA.swift相對應。

所以,僅僅延長像這個 -

class ViewControllerA: BaseViewController 

,然後在viewDidLoad方法,分配你的數據陣列作爲dataSource陣列。

因此,比方說,第一個控制器將顯示一個數組名稱dataA。

所以,你只需做你的viewDidLoad方法,

self.dataSource = dataA 

如果您的數據源需要顯示像圖像,標題或多個標籤類對象複雜的數據,你可以覆蓋在cellForRowAtIndexPath那個特定的類。這樣,它會影響其他控制器。

希望這會有所幫助。

相關問題