2011-05-20 22 views
-1

鑑於UITableView中添加行:iPhone:無陣列

我創建的OBJ-C iPhone一個Navigation-based Application。在- (void) viewDidLoad文件中的方法RootViewController.m添加的按鈕+

/* Create the button that appears on the Right Side */
UIBarButtonItem * rightButton = 
[[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem: UIBarButtonSystemItemAdd 
    target: self 
    action: @ selector (performRight:)]; 

/* Assign the buttons to the Navigation Item's properties */
self.navigationItem.rightBarButtonItem = rightButton; 

[rightButton release]; 

- (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section方法改變的返回值從1到4:

的方法的 - (void) performRight: (id) paramSender

return 4;

- (Void) performRight: (id) paramSender 
{ 
    /* Perform another action here */


    // Add a row (?). 

} 

問題:

當我點擊按鈕+時,如何將該行添加到表中?

說明:

所有我發現的例子使用陣列,這是我做需要。

謝謝。


這是一個默認代碼。在上面的消息中,我寫了一個創建應用程序的完整過程。

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell. 
    return cell; 
} 

我加入這個代碼

[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:section]] withRowAnimation:UITableViewRowAnimationBottom]; 

但我得到一個錯誤Thread 1: Program received signal "SIGABRT"。我引用下面的NSLog:

2011-05-21 08:17:24.932 MyTable[2981:207] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:995 
2011-05-21 08:17:24.948 MyTable[2981:207] *** 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 (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted).' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x00dc15a9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x00f15313 objc_exception_throw + 44 
    2 CoreFoundation      0x00d79ef8 +[NSException raise:format:arguments:] + 136 
    3 Foundation       0x008163bb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116 
    4 UIKit        0x00091e8b -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8420 
    5 UIKit        0x000811e8 -[UITableView endUpdates] + 42 
    6 MyTable        0x00003229 -[RootViewController performRight:] + 313 
    7 UIKit        0x000134fd -[UIApplication sendAction:to:from:forEvent:] + 119 
    8 UIKit        0x00225cc3 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156 
    9 UIKit        0x000134fd -[UIApplication sendAction:to:from:forEvent:] + 119 
    10 UIKit        0x000a3799 -[UIControl sendAction:to:forEvent:] + 67 
    11 UIKit        0x000a5c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 
    12 UIKit        0x000a47d8 -[UIControl touchesEnded:withEvent:] + 458 
    13 UIKit        0x00037ded -[UIWindow _sendTouchesForEvent:] + 567 
    14 UIKit        0x00018c37 -[UIApplication sendEvent:] + 447 
    15 UIKit        0x0001df2e _UIApplicationHandleEvent + 7576 
    16 GraphicsServices     0x00ffa992 PurpleEventCallback + 1550 
    17 CoreFoundation      0x00da2944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52 
    18 CoreFoundation      0x00d02cf7 __CFRunLoopDoSource1 + 215 
    19 CoreFoundation      0x00cfff83 __CFRunLoopRun + 979 
    20 CoreFoundation      0x00cff840 CFRunLoopRunSpecific + 208 
    21 CoreFoundation      0x00cff761 CFRunLoopRunInMode + 97 
    22 GraphicsServices     0x00ff91c4 GSEventRunModal + 217 
    23 GraphicsServices     0x00ff9289 GSEventRun + 115 
    24 UIKit        0x00021c93 UIApplicationMain + 1160 
    25 MyTable        0x00002979 main + 121 
    26 MyTable        0x000028f5 start + 53 
    27 ???         0x00000001 0x0 + 1 
) 
terminate called after throwing an instance of 'NSException' 
sharedlibrary apply-load-rules all 
Current language: auto; currently objective-c 

什麼樣的數據是你呈現 不需要一個數組的? - Deepak 7 小時前

這是一個示例應用程序,它在課程iOS Developer中提供。

+0

你可以發佈你的代碼爲cellForRowAtIndexPath嗎? – 2011-05-20 18:01:23

+0

您呈現的是什麼樣的數據,不需要數組? – 2011-05-20 20:33:35

回答

1

解決方案!

RootViewController.h添加

@ interface RootViewController: 
UITableViewController { 
    int rows; } 

RootViewController.m添加

- (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section { 
     return rows; } 

- (Void) performRight: (id) paramSender 

{ 
     // Insert a row 
     ++rows; 
     [self.tableView reloadData]; } 

RootViewController.xib增加網點

tableView - Table View 

謝謝大家的支持!

0
- (Void) performRight: (id) paramSender 
{ 
    /* Perform another action here */

    int section = 0; 

    // insert at top 
    int row = 0; 
    // or insert at bottom 
    //int row = [self tableView:tableView numberOfRowsInSection:section]; 

    [self.tableView beginUpdates]; 
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject[NSIndexPath indexPathForRow:row inSection:section]] 
          withRowAnimation:UITableViewRowAnimationBottom]; 
    [self.tableView endUpdates]; 

    [self.tableView reloadData]; 
}