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]'
はあなたの最後のセルに戻ってきている。ここでは、問題を修正したコードはありますか? –
@KrishnaK - コピー/貼り付けが悪いように見えるので、コードサンプルに追加しました。それは私の元のコードにも存在します。 – 5StringRyan
xcodeで試してみましたが、あなたのコードは例外なく実行されます。このコードからわかる限り、stringArrayの内容は上記の2つの呼び出しの間で変更されています。 numberOfRowsInSectionに戻るときにカウント値をチェックしてみてください。 –