Kets
Kets

Reputation: 468

NSMutableArray / UITableView

I'm trying to have a UITableView (grouped) with these options:

Section 1:
TextField for name of something
Label + stepper for increasing the Sections where ppl can add options (Subclassed UITableViewCell)

Section 2: (Default is this section, that they already can add things)
First cell of section is an cell (Add Option). People click this to get an extra cell in section 2 where they can get things going. When they click this, an UIAlertView will show up with a textfield.

Section 3:
Same as section 2 from now on. 

So I have already this setup, but now I'm trying to get the "Add Option" to sections with Arrays. Because The add option is always the last index path of the section. so linked the identifier to it. Then when i'm initializing the app, the default array would be something like this:

__diceArray = [NSMutableArray arrayWithObjects:[NSArray arrayWithObject:@"THIS IS FOR SECTION ONE"], [NSArray arrayWithObjects:@"Option in section 2", @"Option for section 2", nil], nil];

But now i'm getting into some problems when trying to add stuff into my section 2, like i can not get a array into my array from section 2. When i try to reload the tableview it crashes.

Anyone got a better way to do this?

EDIT:

Forgotten some things, here they are:

I'm updating the array like this.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSString *name = [alertView textFieldAtIndex:0].text;
        [[self _diceArray] insertObject:name atIndex:2];
        NSLog(@"%@", self._diceArray);

        [[self _tabel]reloadData];
    }
}

Inserts an object but not in the array of the section: ( ( "" // This is for the User configuration in Section One ), ( Test, // Need to be in here. Test // This last is for the add Option prototype cell. ), adsf // This is the added object. )

Also the reload data crashes:

*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101bb1495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001016b199e objc_exception_throw + 43
    2   CoreFoundation                      0x0000000101c4265d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000101ba2d8d ___forwarding___ + 973
    4   CoreFoundation                      0x0000000101ba2938 _CF_forwarding_prep_0 + 120
    5   multiDice                           0x0000000100003752 -[AddViewController tableView:numberOfRowsInSection:] + 146
    6   UIKit                               0x0000000100471e1e -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2245
    7   UIKit                               0x00000001004751c2 -[UITableViewRowData numberOfRows] + 97
    8   UIKit                               0x000000010032323c -[UITableView noteNumberOfRowsChanged] + 114
    9   UIKit                               0x0000000100322d27 -[UITableView reloadData] + 717
    10  multiDice                           0x00000001000042dc -[AddViewController alertView:clickedButtonAtIndex:] + 332
    11  UIKit                               0x0000000100785ec8 -[_UIModalItemsCoordinator _notifyDelegateModalItem:tappedButtonAtIndex:] + 151
    12  UIKit                               0x000000010034c53e -[_UIModalItemAlertContentView tableView:didSelectRowAtIndexPath:] + 364
    13  UIKit                               0x00000001003265c2 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1312
    14  UIKit                               0x00000001003266eb -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 221
    15  UIKit                               0x0000000100277100 _applyBlockToCFArrayCopiedToStack + 316
    16  UIKit                               0x0000000100276f71 _afterCACommitHandler + 460
    17  CoreFoundation                      0x0000000101b7cdc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    18  CoreFoundation                      0x0000000101b7cd37 __CFRunLoopDoObservers + 391
    19  CoreFoundation                      0x0000000101b5c522 __CFRunLoopRun + 946
    20  CoreFoundation                      0x0000000101b5bd83 CFRunLoopRunSpecific + 467
    21  GraphicsServices                    0x0000000103d28f04 GSEventRunModal + 161
    22  UIKit                               0x000000010025ee33 UIApplicationMain + 1010
    23  multiDice                           0x0000000100002c73 main + 115
    24  libdyld.dylib                       0x00000001022495fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

Kind Regards!

Upvotes: 0

Views: 126

Answers (2)

mtnBarreto
mtnBarreto

Reputation: 282

https://github.com/xmartlabs/XLData automatically updates the UITableView whenever you insert/remove items sections to/from the XLDataSet.

Upvotes: 0

Flexicoder
Flexicoder

Reputation: 8501

UPDATED:

You initialise your array with 2 arrays

__diceArray = [NSMutableArray arrayWithObjects:[NSArray arrayWithObject:@"THIS IS FOR SECTION ONE"], 

This code will add a string into your NSMutableArray after the 2 arrays you initialised it with

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSString *name = [alertView textFieldAtIndex:0].text;
        [[self _diceArray] insertObject:name atIndex:2];

Therefore when its asking for numberOfRowsInSection, when it gets to the third entry, its a NSString which obviously doesn't respond to count.

I'm not sure what you are trying to achieve with the add from the alertview, are you trying to add to an existing section or a new section? Either way you need to remember the section you want to add it to, get hold of the NSMutableArray inside that section and insert the Object. But you will need to change your initialise of __diceArray as I stated at the beginning.....

You need to help us out if we are going to help you, what error are you getting for example, but as a starter if you are trying to add elements to the array for each section then they also need to be NSMutableArray

__Array = [NSMutableArray arrayWithObjects:[NSMutableArray arrayWithObject:@"THIS IS FOR SECTION ONE"], [NSMutableArray arrayWithObjects:@"Option in section 2", @"Option for section 2", nil], nil];

But as I say you need to tell us the error, it could also be useful to see how you are trying to update __Array

Upvotes: 1

Related Questions