私は indexPath.row を使用して、テーブルビューのどの行で何かを行うかを決定します。私のセルのタイトルには、最初の行に 1、最後の行に 18 の数字が含まれているため、18 行あります。これは最初の11行で機能しますが、その後、タイトルにランダムに生成されたように見える数字があります! 時には 16、次に 5、次に 18、そして 12... などです。何が問題なのですか / indexPath.row 変数がそのように動作するのはなぜですか?
私の cellForRowAtIndexPath メソッド:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"myCell" owner:self options:nil];
cell = cell0;
self.cell0 = nil;
}
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];
return cell;
}
問題を解決する方法について、他に何か提案はありますか? 今まで動かなかったのに…
// さらにコードを追加して更新します:
セルを宣言する方法は次のとおりです。これは、ライブラリのセルをIBに入れただけのXIBファイル(テンプレート「空のXIB」)にあります。
@interface myViewController : UITableViewController {
UITableViewCell *cell0;
}
@property (nonatomic, retain) IBOutlet UITableViewCell *cell0;
次に、myViewController.m ファイルの先頭で:
@synthesize cell0;
私の cellForRowAtIndexPath メソッドは既に上に投稿されています。SDK ドキュメントの cellForRowAtIndexPath メソッドと同等であり、Apple の例では機能しているようです。