NEO-SHOCKER.COM

UITableViewCell で文字が入れ替わる!の対処

UITableViewCell で、画面をスクロールすると他のセルの内容と入れ替わってしまう場合があります。

そうした現象が発生すると思われるのは大体が
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
に、こんな風に書いていると思います。

[code:cpp]
static NSString *CellIdentifier = @”Cell”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}[/code]

CellIdentifier に同じ “Cell” というのが入っていて、文字が入れ替わるんじゃないのかと推測(テキトーだなー)
それなら、それぞれに違う値を入れてやります。
例えば、Cell が 10個あった場合、for とかで 10回まわして 10個用意してしまえ!という感じで、こうかな。

[code:cpp]
NSMutableArray *identifiers = [NSMutableArray array];
for ( int i = 1; i <= 10; i++ ) { [identifiers addObject:[NSString stringWithFormat:(@"cell%d"), i]]; } NSString *CellIdentifier = [identifiers objectAtIndex:[indexPath section]]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if ( cell == nil ) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } [/code]

ソニーストア
ブックオフオンライン【PC・スマホ共通】