2013-10-21 59 views
4

我想添加一個按鈕資料轉移到UITableView,這裏是我的代碼:如何添加項目到UITableView?

viewDidLoadrepository = [[NSMutableArray alloc] initWithObjects: nil];

//ADD ITEM TO LIST 
       [repository addObject:repo]; //repository is a NSMutableArray 
       NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([repository count]-1) inSection:0]; 
       NSLog(@"Indexpath to add row ----> %ld", (long)indexPath.row); 
       [self.tableView beginUpdates]; 
       [self.tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationBottom]; 
       [self.tableView endUpdates]; 

現在廣告的項目列表,但只有當已經有在

UITableViewController like i configured here: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"NSMutableArray count in \"numbersOfRowsInSection\" ----> %lu", (unsigned long)[repository count]); 

    return [repository count] + 1; 

} 

這裏的Cell是我的日誌:

Indexpath to add row ----> <NSIndexPath: 0x10d9499a0> {length = 2, path = 0 - 18446744073709551615} //This is "NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[repository count] - 1 inSection:0];" 
2013-10-22 14:48:42.214 ApplicationName[41213:a0b] NSMutableArray count in "numbersOfRowsInSection" ----> 0 //This is the NSInteger returned in - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 
2013-10-22 14:48:42.217 ApplicationName[41213:a0b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:1330 
2013-10-22 14:48:42.236 ApplicationName[41213:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' 
*** First throw call stack: 
(
    0 CoreFoundation      0x0000000101cdf795 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x00000001019bb991 objc_exception_throw + 43 
    2 CoreFoundation      0x0000000101cdf61a +[NSException raise:format:arguments:] + 106 
    3 Foundation       0x0000000101558bf9 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 189 
    4 UIKit        0x00000001006d51db -[UITableView _endCellAnimationsWithContext:] + 11410 
    5 ApplicationName       0x0000000100009b5d -[RepositoryViewController alertView:clickedButtonAtIndex:] + 829 
    6 UIKit        0x0000000100b1dbd8 -[_UIModalItemsCoordinator _notifyDelegateModalItem:tappedButtonAtIndex:] + 151 
    7 UIKit        0x0000000100707629 -[_UIModalItemAlertContentView tableView:didSelectRowAtIndexPath:] + 364 
    8 UIKit        0x00000001006e3d36 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1312 
    9 UIKit        0x00000001006e3e5f -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 221 
    10 UIKit        0x000000010063b0d2 _applyBlockToCFArrayCopiedToStack + 316 
    11 UIKit        0x000000010063af50 _afterCACommitHandler + 460 
    12 CoreFoundation      0x0000000101caaf97 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
    13 CoreFoundation      0x0000000101caaf07 __CFRunLoopDoObservers + 391 
    14 CoreFoundation      0x0000000101c8a672 __CFRunLoopRun + 946 
    15 CoreFoundation      0x0000000101c89ed3 CFRunLoopRunSpecific + 467 
    16 GraphicsServices     0x0000000102efa3a4 GSEventRunModal + 161 
    17 UIKit        0x0000000100623a63 UIApplicationMain + 1010 
    18 ApplicationName       0x000000010000aae3 main + 115 
    19 libdyld.dylib      0x00000001067fd7e1 start + 0 
    20 ???         0x0000000000000001 0x0 + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 
+0

的索引定義新indexpath(庫計) - 1.將新項目添加到數據源,然後嘗試插入超出可用行數的行。 – Tim

回答

9

更新:

首先,你numberOfRowsInSection:方法應該在任何返回的確切數額行代表你的數據源。在這種情況下,庫:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [repository count]; 
} 

其次,只要用戶按下按鈕添加一個新的細胞,你首先需要將項目添加到您的數據源,然後你需要插入細胞本身。

- (void)didPressButton 
{ 
    [repository addObject:repo]; //repository is a NSMutableArray 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[repository indexOfObject:repo] inSection:0]; 
    [self.tableView beginUpdates]; 
    [self.tableView 
insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationBottom]; 
    [self.tableView endUpdates]; 
} 

就這麼簡單。

+0

我得到這個迴應:***終止應用程序,由於未捕獲的異常'NSInternalInconsistencyException',原因:'與UITableView一起使用的索引路徑無效。傳遞給表視圖的索引路徑必須包含恰好兩個指定節和行的索引。如果可能,請在UITableView.h中使用NSIndexPath上的類別。'但我沒有得到它 –

+0

我已經更新了我的答案 – Tim

+0

我添加了我的整個日誌輸出到這個問題,我沒有通過閱讀變得更聰明, –

10

嗯,我無意中發現了這個問題了,而過了,我是用迅速,所以我想在這裏發表我的代碼:

func addOneMoreRow(){ 
    rowsCount++ 
    let indexPath = NSIndexPath(forRow: rowsCount - 1, inSection: 0) 
    tableView.beginUpdates() 
    tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Bottom) 
    tableView.endUpdates() 
}