StoryBoard「CategoryCell」にcustomCellがあり、StoryBoardにも関連付けられているセルの一部でもあるセルを持つUIImageViewがあります。UIImageView は無地の黄色で塗りつぶされます。この無地の黄色の画像にラベルを追加するつもりです。ラベルはセルによって異なります。
以下のコードは最初は問題なく動作しますが、tableView をスクロールすると、画像のラベルが乱れているのがわかります。それは、テキストの上に新しいテキストを書き込もうとしているようなものです。cellForRow が再びヒットし、新しいラベルが追加されていると考えました。古いラベルの上に新しいラベルを作成しないようにするにはどうすればよいですか?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([tableView isEqual:wordsTableView])
{
CategoryCell *cell = [CategoryTableView dequeueReusableCellWithIdentifier:@"CategoryCellIdentifier" forIndexPath:indexPath];
if(!cell)
cell = [[CategoryCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:@"CategoryCellIdentifier"];
NSString *text = [[theCategories objectAtIndex:indexPath.row] uppercaseString];
//Add label now
catLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 40, 20)];
catLabel.text = text;
[cell.codeImageView addSubview:catLabel];
return cell;
}