2015-06-15 62 views
0

我有static UITableView與兩個sections。當用戶在第1部分的索引2處敲擊單元格時,需要在竊聽單元格下方添加UITableViewCell。隨後的每個單元點擊應該導致另一個單元添加到任何已添加的單元下方。添加行到靜態UITableView:獲取NSRangeException

這是我到目前爲止已經試過,但我有NSRangeException崩潰,說明指數6超出了可用的範圍所以我顯然不能更新行正確莫名其妙的數..

根據Adding unknown number of rows to 'Static Cells' UITableView我已經覆蓋了所有使用indexPath的UITableView方法,但它們的情況與我的略有不同。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if section == 1 { 
     // subTaskRow is an array holding all the indexes of the added cells 
     // so I know where to add the next one. I also know how many I've added 
     return 6 + subTaskRow.count 
    } else { 
     return super.tableView(tableView, numberOfRowsInSection: section) 
    } 
} 
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    // If the Sub Task cell is picked 
    if (indexPath.section == 1 && indexPath.row == 3) { 
     // If this is the first subtask we want it to appear under Sub Tasks 
     // We know the index of the cell below sub task so we can add it in directly 
     if (subTaskRow.count == 0) { 
      subTaskRow.addObject(5) 
      tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 5, inSection: 1)], withRowAnimation: .Automatic) 
      tableView.reloadData() 
     // Other sub tasks should appear below previous sub tasks 
     } else { 
      // The last object in sub tasks will always be the 
      // indexPath of the previous added cell 
      let row = (subTaskRow.lastObject as! Int) + 1 
      subTaskRow.addObject(row) 
      tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: 1)], withRowAnimation: .Automatic) 
      tableView.reloadData() 
     } 
    } 

    tableView.deselectRowAtIndexPath(indexPath, animated: true) 
} 

// Now we override all the other methods to ensure we don't get a crash 
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    return false 
} 

override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    return false 
} 

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { 
    return .None 
} 

override func tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int { 
    return super.tableView(tableView, indentationLevelForRowAtIndexPath: indexPath) 
} 

我的猜測是,在加入的部分中間的單元格不更新已有的細胞或某事的indexPaths?我不知道我該怎麼做。

+0

你的cellForRowAtIndexPath方法是什麼樣的?你可以將它添加到你的tableview方法列表中嗎?另外,在界面構建器中,您是否設置了「索引行限制」? – erparker

+0

@erparker我沒有真正使用過一個,因爲它們都是靜態單元,沒有什麼可配置的......我需要將它用於我添加的行嗎? –

+0

我注意到的另一件事是,你是否試圖做自己的UITableView的子類?你在所有的tableview數據源和委託方法前面都有覆蓋,如果你只是使用tableview作爲視圖控制器的一部分,那麼你不需要這些覆蓋。 – erparker

回答

1

我得到這個工作,但是這個代碼是隻使用一節:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // #warning Potentially incomplete method implementation. 
     // Return the number of sections. 
     return 1 
    } 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // subTaskRow is an array holding all the indexes of the added cells 
    // so I know where to add the next one. I also know how many I've added 
    return 6 + subTaskRow.count 
} 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    // If the Sub Task cell is picked 
    if (indexPath.section == 0 && indexPath.row == 3) { 
     // If this is the first subtask we want it to appear under Sub Tasks 
     // We know the index of the cell below sub task so we can add it in directly 
     if (subTaskRow.count == 0) { 
      subTaskRow.append(5) 
      tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 5, inSection: 0)], withRowAnimation: .Automatic) 
      tableView.reloadData() 
      // Other sub tasks should appear below previous sub tasks 
     } else { 
      // The last object in sub tasks will always be the 
      // indexPath of the previous added cell 
      let row = subTaskRow.last! + 1 
      subTaskRow.append(row) 
      tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: 0)], withRowAnimation: .Automatic) 
      tableView.reloadData() 
     } 
    } 

    tableView.deselectRowAtIndexPath(indexPath, animated: true) 
} 


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

    cell.textLabel!.text = "the row: \(indexPath.row)" 

    return cell 
} 

嘗試這個代碼,看看它是否適合你。在這裏,你只需要添加代碼來處理部分,這意味着將代碼添加到numberOfSectionsInTableView。你不應該在這裏調用super.numberOfRowsInTableview,因爲如果你有兩段數據,你應該知道每行有多少行