1

UITableView に新しいセクションを挿入するのに苦労しています。基本的に達成したいことは次のとおりです。-各セクションが各年に対応するカレンダーを作成しようとしています。各セクションには 30 個のセルがあります。

最初のセクションはかなりうまくロードされますが、最初のセクションの 8 番目のセルに移動すると、新しいセクションを挿入しようとします

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]'
"
}
   }

//読み取るセクションの数

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

//セクションごとの行数

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

//行メソッドの CEll は次のように読み取ります

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

興味深いことに、すばやくスクロールしないとクラッシュしないことがあります。しかし、10行目から11行目までゆっくりスクロールすると、クラッシュしないことがあります

4

2 に答える 2

1

編集:これで解決しました:

-(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]; 
}
于 2013-07-23T08:25:35.063 に答える