开发者

iPhone: adding rows in UITableView without arrays

Given:

I created a Navigation-based Application for Obj-C iPhone. In the method of - (void) viewDidLoad in the file RootViewController.m added button +:

 / * Create the button that appears on the Right Side * /
 UIBarButtonI开发者_StackOverflow社区tem * 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];

In the method of - (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section changed the return value from 1 to 4:

return 4;

A method of - (void) performRight: (id) paramSender

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


     // Add a row (?).

}

Question:

How can I make the row added to the table when I click on the button +?

Explanation:

All I found examples using arrays, which I do not need.

Thank you.


This is a default code. In the message above, I wrote a full process of creating the application.

// 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;
}

I added this code

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

But I get an error Thread 1: Program received signal "SIGABRT". I quote below 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

What kind of data are you presenting that doesn't need an array? – Deepak 7 hours ago

This is a sample application, which posed to perform at Courses iOS Developer.


Solution!

In RootViewController.h add

@ interface RootViewController:
UITableViewController {
     int rows; }

In RootViewController.m add

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

and

 - (Void) performRight: (id) paramSender

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

In RootViewController.xib add outlets

 tableView - Table View

Thank you all for your support!


- (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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜