Mann
Mann

Reputation: 5507

Inserting new sections into UITableView causing crash

I am struggling with inserting new sections into UITableView. What basically i want to achieve is:- I am trying to create calendar where each SECTION corresponds to each YEAR. Each section is having 30 cells.

First section loads pretty well but when i go to 8th cell of first section then i try to insert new section

sectionNo =1; //in viewDidLoad method 

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *) cell              forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 10 && indexPath.section == sectionNo-1)
{
    sectionNo = sectionNo+1;
    [mainTable insertSections:[NSIndexSet indexSetWithIndex:sectionNo-1] withRowAnimation:UITableViewRowAnimationNone]; //APPLICATION CRASHES HERE  ERROR IS "Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
"
}
   }

//Number of sections reads as

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return sectionNo;    //count of section
}

//Number of Rows per section

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

//CEll for row method reads as

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:MyIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
  cell.textLabel.text = @"Hello";
return cell;
}

Interesting thing is sometimes when i do not scroll it rapidly then it does not crash. but if i scroll slowly from 10th row to 11 then sometimes it does not crash WHAT I AM DOING WRONG HERE

Upvotes: 1

Views: 1581

Answers (2)

Julien Klindt
Julien Klindt

Reputation: 313

EDIT: We solved it with this:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ 
  if(indexPath.row == 10 && indexPath.section == _sectionNo-1) 
  { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self addSectionToTableView]; 
    }); 
  } 
} 

- (void)addSectionToTableView{ 
_sectionNo++; 
 [_mainTable insertSections:[NSIndexSet indexSetWithIndex:_sectionNo-1] withRowAnimation:UITableViewRowAnimationNone]; 
}

Upvotes: 1

Midhun MP
Midhun MP

Reputation: 107131

Possibly the issue is causing with this code:

if(indexPath.row == 10 && indexPath.section == sectionNo-1)
{
    sectionNo = sectionNo+1;
    [mainTable insertSections:[NSIndexSet indexSetWithIndex:sectionNo-1] withRowAnimation:UITableViewRowAnimationNone];
}

Change that to:

if(indexPath.row == 10 && indexPath.section == sectionNo-1)
{

    [mainTable insertSections:[NSIndexSet indexSetWithIndex:sectionNo-1] withRowAnimation:UITableViewRowAnimationNone];
    sectionNo = sectionNo+1;
}

Upvotes: 0

Related Questions