2015-12-16 63 views
0

我正在編寫一個應用程序,只是爲了好玩和改進。3具有公共數據源和委託的TableView - 錯誤的行數

我的問題是下列之一:

在我的應用程序的視圖控制器,我有3個tableViews共享數據源和委託(將它的viewController個體經營)。我爲每個做了3個插座,通過這種方式,我可以通過對象參考比較(tableView === outletTableView)確定numberOfRowsInSectioncellForRowAtIndexPath的返回值。 每次都返回正確的值(用某個調用打印進行檢查),但設備似乎使用一個tableView中的numberOfRows作爲3個tableViews! 我的第一個有13行,第二個21和第三隻1.當第二的tableView的第14行被加載,這個錯誤的程序崩潰:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath: 0xc000000001a00016> {length = 2, path = 0 - 13})' 

我查過很多科目,但我沒有發現任何解決方案...

感謝


這裏我的代碼:

@IBOutlet weak var mainSkillTableHeight: NSLayoutConstraint! 
@IBOutlet weak var secondarySkillTableHeight: NSLayoutConstraint! 
@IBOutlet weak var exoticSkillTableHeight: NSLayoutConstraint! 

[...] 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if (tableView === mainSkillsTable) { 
      print("CM \(character.main_skills.count + 1)") 
      return character.main_skills.count + 1 
     } 
     else if (tableView === secondarySkillsTable) { 
      print("CS \(character.secondary_skills.count + 1)") 
      return character.secondary_skills.count + 1 
     } 
     else if (tableView === exoticSkillsTable) { 
      print("CE \(character.exotic_skills.count + 1)") 
      return character.exotic_skills.count + 1 
     } 
     return 0 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell1: CreationMainSkillCell? 
     var cell2: CreationSecondarySkillCell? 
     var cell3: CreationExoticSkillCell? 
     if (tableView === mainSkillsTable) { 
      if indexPath.row == 0 { 
       return mainSkillsTable.dequeueReusableCellWithIdentifier("mainHeader", forIndexPath: indexPath) as! HeaderMainSkillCell 
      } 
      cell1 = mainSkillsTable.dequeueReusableCellWithIdentifier("mainDataC", forIndexPath: indexPath) as? CreationMainSkillCell 
      cell1!.character = character 
      cell1!.row = indexPath.row 
      return cell1! 
     } 
     else if (tableView === secondarySkillsTable) { 
      if indexPath.row == 0 { 
       return mainSkillsTable.dequeueReusableCellWithIdentifier("secondHeader", forIndexPath: indexPath) as! HeaderSecondarySkillCell 
      } 
      cell2 = mainSkillsTable.dequeueReusableCellWithIdentifier("secondDataC", forIndexPath: indexPath) as? CreationSecondarySkillCell 
      cell2!.character = character 
      cell2!.row = indexPath.row 
      return cell2! 
     } 
     else if (tableView === exoticSkillsTable) { 
      if indexPath.row == 0 { 
       return mainSkillsTable.dequeueReusableCellWithIdentifier("exoticHeader", forIndexPath: indexPath) as! HeaderExoticSkillCell 
      } 
      cell3 = mainSkillsTable.dequeueReusableCellWithIdentifier("exoticDataC", forIndexPath: indexPath) as? CreationExoticSkillCell 
      cell3!.character = character 
      cell3!.row = indexPath.row 
      return cell3! 
     } 

     return UITableViewCell() 
    } 

numberOfRows沒有鏈接到一個特定的tableView?

+0

如果共享一個公共數據源,行數會有多少? – pbush25

+0

你能給詳細的工具嗎?我認爲問題是你的代碼錯誤的每種表? –

+0

你可以發佈你的代碼嗎? –

回答

0

那麼你的代碼似乎有問題。當數據源相同時,3個表不可能有不同的numberOfRows。你顯然正在改變你傳遞給tableView的數組。

請檢查最初加載視圖時返回的numberOfRows。每當你在任何一個表中進行更改時,都需要反映「數據源」數組而不是本地數組。你似乎在使用中間數組,因爲你在顯示numberOfRows時出現不一致。

+0

我遵循這個例子:https://gist.github.com/mchirico/50cdb07d20b1b0f73d7c 我已經編輯我的帖子添加代碼!這很容易理解我的問題:p – percypyan

+0

@percypyan您可以共享完整的代碼嗎?您需要專門爲每個表分別設置代表和每個表的數據源。 – Karthik

+0

我沒有了:/我已經重寫了所有這部分,現在我使用了一個單獨的委託和數據源類。我在Storyboard中創建了3個對象,併爲每個對象分配了我的dataSource類。之後,我只需將3個dataSource鏈接到3個對象,並且所有工作都完美無缺! – percypyan