2015-09-19 21 views
0

我使用以下viewController設置錯誤:「無法出列與標識小區的小區」

enter image description here

我所試圖做的是從placesTabBar訪問的地方的array,並顯示在兩個navigationControllers。其中一個控制器將包含地點列表,而另一個控制器將僅用於您要參加的地點。在選擇一個單元格時,它會在切換時更新另一個視圖控制器,以表示您要去那個地方。我使用下面的代碼:

AllPlaces的ViewController

class AllPlaces: UITableViewController { 

var delegate:placesTabBar! 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return delegate.placeDataArray.count 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell 

    cell.textLabel?.text = delegate.placeDataArray[indexPath.row].description 

    return cell 
} 

} 

AttendingPlaces的ViewController 類AttendingPlaces:{的UITableViewController

var delegate:placesTabBar! 

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

    return delegate.placeDataArray.count 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell 

    cell.textLabel?.text = delegate.placeDataArray[indexPath.row].description 
    return cell 
} 

}

鉭bBarController

class placeData: Equatable { 
var description : String 
var selected : Bool 

init (description : String, selected : Bool) { 
    self.description = description 
    self.selected = selected 
} 
} 
class placesTabBar: UITabBarController, CustomTabBarDelegate { 

var placeDataArray = Array<placeData>() 

override func viewDidLoad() { 

    placeDataArray = [placeData(description: "Afghanistan", selected: false), placeData(description: "Albania", selected: false)] 

    var table1 = AttendingPlaces() 
    var table2 = AllPlaces() 

    table1.delegate = self 
    table2.delegate = self 

    var navController1 = UINavigationController(rootViewController: table1) 
    var navController2 = UINavigationController(rootViewController: table2) 

    self.viewControllers = [navController1, navController2] 

} 



override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
} 

現在我只是試圖加載與兩個地方我有數組中的表。但是,我收到「無法使帶標識符單元的單元出列」錯誤。重複標識符在tableViewControllers中設置正確,所以我不確定發生了什麼。難道這就是我試圖分享兩個viewControllers之間的數據嗎?

+0

你在故事板設置哪些細胞再利用標識? – Fogmeister

+0

我將標識符設置爲故事板中的單元格 – Ryguyu

+0

在我嘗試從'tabBarViewController'訪問數組之前,'tableViews'正在工作,然後在我實現該代碼後停止。我覺得這個錯誤可能會引起誤解。 – Ryguyu

回答

1

由於您正在創建與故事板沒有關聯的兩個表格視圖控制器(例如table1 = AttendingPlaces())的新實例,因此不會「瞭解」故事板中設置的標識符。

你應該能夠只是爲了得到這些的同時實例化的tabBarController現有的表視圖控制器的引用:

var nav1 = self.viewControllers[0] as! UINavigationController 
var table1 = nav1.topViewController as! AttendingPlaces 
var nav2 = self.viewControllers[1] as! UINavigationController 
var table2 = nav2.topViewController as! AllPlaces 

table1.delegate = self 
table2.delegate = self