0

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!

Kets
  • 438
  • 2
  • 8
  • 24
  • Try to create an NSMutableArray of NSMutableArrays (not NSArray - immutable array) – AndrewShmig Mar 30 '14 at 09:53
  • Can you share the stack trace when it crashed? – ismailgulek Mar 30 '14 at 09:53
  • Did an edit to my post. Sorry for the lak of information. – Kets Mar 30 '14 at 10:08
  • Not sure your update helps, how does `[self _diceArray]` relate to __Array? You've given us the stack dump, but there should be an error message that goes with it that tells you what object doesn't respond to selector `-[NSObject(NSObject) doesNotRecognizeSelector:] + 205` ? – Flexicoder Mar 30 '14 at 10:14
  • Is _diceArray an `NSMutableArray` – Flexicoder Mar 30 '14 at 10:15
  • Yeah _diceArray is an NSMutableArray. Also edited it, __Array is actually my diceArray sorry for that. And here is the property: @property (copy, nonatomic) NSMutableArray * _diceArray; – Kets Mar 30 '14 at 10:18
  • Still need to know this though - You've given us the stack dump, but there should be an error message that goes with it that tells you what object doesn't respond to selector -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 ? – Flexicoder Mar 30 '14 at 10:21
  • Gosh sorry, it's still early in the morning for me: 2014-03-30 12:05:38.524 multiDice[801:60b] -[__NSCFString count]: unrecognized selector sent to instance 0x10955c680 2014-03-30 12:05:38.526 multiDice[801:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance 0x10955c680' – Kets Mar 30 '14 at 10:21
  • OK so the actual error is that you are trying to ask a `NSString` for its `count`, which as the error message shows is invalid. Where in your code are you using count? Also if you add a global breakpoint http://stackoverflow.com/questions/1163981/how-to-add-a-breakpoint-to-objc-exception-throw it will stop where the error happened – Flexicoder Mar 30 '14 at 10:25
  • Thanks for the global breakpoint thingy! It stops here at my numberOfRowsInSection return [[__diceArray objectAtIndex:section] count]; but the section is an NSInteger? – Kets Mar 30 '14 at 10:30

2 Answers2

1

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

Flexicoder
  • 8,251
  • 4
  • 42
  • 56
  • Hmm oke, you have a point there didn't think about it yet, I wanted to add an option to the exiting section where they clicked the add option. Need to figure out a way to get to know the section for the UIAlertView add. Also rethinking my array.. Because the last index path isn't going to be always empty, for my addOption prototype cell.. Need to rethink that – Kets Mar 30 '14 at 10:46
0

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

mtnBarreto
  • 282
  • 1
  • 3
  • 9