1

Hi have a complex storyboard uitableview interface with a lot of custom cells. When a condition is satisfied I need to remove some cells and add others. In particular, in section 1 I have initially 2 cells and when the condition is satisfied I need to remove this two cells and add some cells (for instance 3 or 4 cells, in the code: self.viaggio.tragitto count).

Here is my code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    switch (section) {
        case 0:
            return 2;
            break;
        case 1:
            if(self.trenoCompilato) return [self.viaggio.tragitto count];
            else return 2;
        case 2:
            if(self.trenoCompilato) return 1;
            else return 0;
        case 3:
            return 1;
        default:
            return 0;
            break;
    }

} 

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{


    if(indexPath.section == 1 && self.trenoCompilato) {

        static NSString *cellIdentifier = @"cellTragitto";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        }

        Treno *trenoCell = self.viaggio.tragitto[indexPath.row];

        cell.textLabel.text = [trenoCell stringaDescrizione];
        cell.detailTextLabel.text = [[DateUtils shared] showHHmm:[[DateUtils shared] dateFrom:trenoCell.orarioPartenza]];

        return cell;
    }

    return [super tableView:tableView cellForRowAtIndexPath:indexPath];

}

The condition to display the new cells (they are NOT present in the storyboard) is the boolean self.trenoCompilato

Now. When I do [self.tableview reloadData] when the boolean is TRUE i get:

 *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001102caf35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010ff63bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001101c301e -[__NSArrayI objectAtIndex:] + 190
    3   UIKit                               0x0000000111134912 -[UITableViewDataSource tableView:indentationLevelForRowAtIndexPath:] + 106
    4   UIKit                               0x0000000110cdf612 __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 1704
    5   UIKit                               0x0000000110c605ce +[UIView(Animation) performWithoutAnimation:] + 65
    6   UIKit                               0x0000000110cdef5b -[UITableView _configureCellForDisplay:forIndexPath:] + 312
    7   UIKit                               0x0000000110ce64cc -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 533
    8   UIKit                               0x0000000110cc5fb1 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2846
    9   UIKit                               0x0000000110cdbe3c -[UITableView layoutSubviews] + 213
    10  UIKit                               0x0000000110c68973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
    11  QuartzCore                          0x0000000110a7ade8 -[CALayer layoutSublayers] + 150
    12  QuartzCore                          0x0000000110a6fa0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    13  QuartzCore                          0x0000000110a6f87e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    14  QuartzCore                          0x00000001109dd63e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
    15  QuartzCore                          0x00000001109de74a _ZN2CA11Transaction6commitEv + 390
    16  QuartzCore                          0x00000001109dedb5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
    17  CoreFoundation                      0x00000001101ffdc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    18  CoreFoundation                      0x00000001101ffd20 __CFRunLoopDoObservers + 368
    19  CoreFoundation                      0x00000001101f5b53 __CFRunLoopRun + 1123
    20  CoreFoundation                      0x00000001101f5486 CFRunLoopRunSpecific + 470
    21  GraphicsServices                    0x0000000113c309f0 GSEventRunModal + 161
    22  UIKit                               0x0000000110bef420 UIApplicationMain + 1282
    23  SeguiTreno                          0x000000010f6afa03 main + 115
    24  libdyld.dylib                       0x00000001141bf145 start + 1
    25  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I have already tried with:

[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationFade];
// inserisco cella con opzione per il cambio soluzione
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

and it still doesn't work.

I tried to add cells in the storyboard and it works! It seems that I can't get more cells than the number is present in the storyboard. What I'm missing?

zerbfra
  • 190
  • 12
  • francesco289, I am not sure but its look like u try to access beyond you array limit,(for instance if u make your array of size 2 (it means u have [0..1] elements) that will mean if you specify in tableView: numberOfRowsInSection return 2 it will try to access 0,1,2 indexes.(and that may create an exception) – Mike.R Nov 29 '14 at 17:40
  • Possible duplicate of: http://stackoverflow.com/questions/18153702/uitableview-mix-of-static-and-dynamic-cells and http://stackoverflow.com/questions/12858363/uitableview-handle-cell-selection-in-a-mixed-cell-table-view-static-and-dynamic – fishinear Nov 29 '14 at 18:00
  • @Mike.R My arrays are ok so I can't understand why it creates an exception...numberOfRowsInSection return the count of the array and it works if the number is less or equal to the number of storyboards cells. Can it be a "cell" issue? – zerbfra Nov 30 '14 at 10:39
  • I thinks the answer is here: http://stackoverflow.com/questions/20777878/dynamic-uitableview-inside-static-cell?rq=1 – zerbfra Nov 30 '14 at 10:45

0 Answers0