2012-01-08 27 views
1

この問題に関するいくつかのSOの記事を読んだが、UITableViewの上部に追加のセルを追加することに問題があるようだ。ここに私のコードは次のとおりです。UITableViewの問題の先頭に追加のセルを追加する

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // self.StringArray has 5 string objects inside 
    return ([self.stringArray count] + 1); 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // create hardcoded first row in table 
    if([indexPath row] == 0) 
    { 
     cell.textLabel.text = @"Select me"; 
    } 
    else 
    { 
     // decrement the row to get the correct object from the self.stringArray 
     int arrayIndex = [indexPath row] - 1; 
     cell.textLabel.text = [self.stringArray objectAtIndex:arrayIndex]; 
    } 
    return cell; 
} 

すべてが(明らかに何かが、私は例外エラーを取得していた場合にかかわらず、間違っている)よさそうだが、私はそれを眼球に見えることはできません。

例外

:アプリを終了 *キャッチされない例外により 'NSRangeException'、理由: '* - [__ NSArrayM objectAtIndex:]:範囲外のインデックス5 [0 .. 4]'

+0

はあなたの最後のセルに戻ってきている。ここでは、問題を修正したコードはありますか? –

+0

@KrishnaK - コピー/貼り付けが悪いように見えるので、コードサンプルに追加しました。それは私の元のコードにも存在します。 – 5StringRyan

+0

xcodeで試してみましたが、あなたのコードは例外なく実行されます。このコードからわかる限り、stringArrayの内容は上記の2つの呼び出しの間で変更されています。 numberOfRowsInSectionに戻るときにカウント値をチェックしてみてください。 –

答えて

1

私は問題を発見しました。私は、他のUITableViewデリゲートメソッドを考慮しなかったという事実を見落とし、tableView:canEditRowAtIndexPathメソッドのロジックを設定するときに、この余分な行をUITableViewで考慮しませんでした。 ' - :(のUITableView *)のtableView cellForRowAtIndexPath:(NSIndexPath *)indexPath (UITableViewCellの*)のtableView' メソッドを

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // I added code here to intercept the added row (row 0) and make it non-editable 
    if([indexPath row] == 0) 
    { 
     return NO; 
    } 
    else 
    { 
     // decrement the row to get the correct object from the self.stringArray  
     int arrayIndex = [indexPath row] - 1; 
     if([[self.stringArray objectAtIndex:arrayIndex] length] > 10) 
     { 
      return YES; 
     } 
     else 
     { 
      return NO; 
     } 
    } 
} 
関連する問題