私は少しこれを理解しようとしています。自分のxibファイルにカスタムセルを作成します。私のビューコントローラでは、セクションを持つテーブルビューコントローラをセットアップしました。テーブルビューにプルされているデータは、私が持っているいくつかのコアデータからのフェッチ要求コントローラに基づいています。私はcellForRowAtIndexPath関数でカスタムセルを設定しました。この関数内の各セルのラベルを作成し、そのラベルに管理対象オブジェクトのデータを設定しています。私が最初に走った時、すべてが大丈夫と思われます。しかし、上下にスクロールして新しいセルを再利用しようとすると、ラベル内のデータが間違ったセルに配置されます。私はこれが細胞の再利用と関係しているのを見て聞いたことがあります。しかし、この問題の修正に関する多くの例は見ていない。以下は、私のcellForRowAtIndexPath関数にあるコードの一部です。他の入力が必要な場合は教えてください。助けてくれてありがとう。セルが再利用されたときにラベルが表示されているIOSカスタムセル
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"CustomCell"];
/* do this to get unique value per cell due to sections. */
NSInteger indexForCell = indexPath.section * 1000 + indexPath.row + 1;
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
NSString *lastSession = nil;
UILabel *lastSessionLabel = nil;
if(cell == nil) {
lastSession = [managedObject valueForKey:@"last_session"];
[self.tableView registerNib:[UINib nibWithNibName:@"CustomCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:@"CustomCell"];
self.tableView.backgroundColor = [UIColor clearColor];
cell = [aTableView dequeueReusableCellWithIdentifier:@"CustomCell"];
lastSessionLabel = [[UILabel alloc]initWithFrame:CGRectMake(410,55, 89, 35)];
lastSessionLabel.textAlignment = UITextAlignmentLeft;
lastSessionLabel.tag = indexForCell;
lastSessionLabel.font = [UIFont systemFontOfSize:17];
lastSessionLabel.highlighted = NO;
lastSessionLabel.backgroundColor = [UIColor clearColor];
cell.contentView.tag = indexForCell;
[cell.contentView addSubview:lastSessionLabel];
} else {
lastSessionLabel = (UILabel *)[cell viewWithTag:indexForCell];
}
if (lastSession && lastSession.length) {
lastSessionLabel.text = lastSession;
}
cell.textLabel.text = [NSString stringWithFormat:@"%@%@%@%@", @"Dr. ",
[managedObject valueForKey:@"first_name"],
@" " ,
[managedObject valueForKey:@"last_name"]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.editingAccessoryType = UITableViewCellAccessoryNone;
return cell;
}
は**改訂コードは**
は、以下のコードを変更している:あなたは新しいセルを作成するときにのみlastSession
を取得している
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"CustomCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:@"CustomCell"];
self.tableView.backgroundColor = [UIColor clearColor];
}
in -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"CustomCell"];
NSInteger indexForCell = indexPath.section * 1000 + indexPath.row + 1;
NSLog(@"index for cell: %d",indexForCell);
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
NSString *lastSession = [managedObject valueForKey:@"last_session"];
UILabel *lastSessionLabel = nil;
if(cell == nil) {
NSLog(@"Cell is nil! %@", [managedObject valueForKey:@"first_name"]);
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
self.tableView.backgroundColor = [UIColor clearColor];
}
lastSessionLabel = [[UILabel alloc]initWithFrame:CGRectMake(410,55, 89, 35)];
lastSessionLabel.textAlignment = UITextAlignmentLeft;
lastSessionLabel.tag = indexForCell;
lastSessionLabel.font = [UIFont systemFontOfSize:17];
lastSessionLabel.highlighted = NO;
lastSessionLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lastSessionLabel];
/* Appropriate verbiage for nil last session. */
if (lastSession && lastSession.length) {
lastSessionLabel.text = lastSession;
}
return cell;
}
I am still having issues again with the label cell text changing when I scroll for different cells. I read some where about maybe having to use the prepareForReuse function for this.
お返事ありがとうございます。私はあなたが言及したいと思ったが、別のセルを表示するために上下にスクロールすると、ランダムに変化するラベルテキストの問題がまだ残っている。 – doubleace3
私の更新 –
同じ問題を参照してください。ラベル内のコンテンツをスクロールすると一貫性がないと思われます。たぶん異なるアプローチが必要かもしれません。 – doubleace3