テーブルビューがあり、各セルには水平スクロール用の別のテーブルビューが含まれています。メインのテーブルビューの高さはわずか6セルなので、最初はセルを再利用していませんでしたが、垂直スクロールが途切れ途切れだったため、セルを再利用することにしました。垂直テーブルビュー内には、3つの異なるタイプの水平セルがあります。2種類が問題なく動作しています。1種類が重複しています。これをよりよく説明するために、ここに図を示します。
Vertical TableView
Row 0 Cell type A
Row 1 Cell type B
Row 2 Cell type C
Row 3 Cell type C
Row 4 Cell type C
Row 5 Cell type B
Row 6 Cell type C
行6に表示されるデータが行2に表示されるデータと同じであるという理由を除いて、これらはすべて正しく表示されています。タイプCである他の行が明らかにありますが、重複しているのは2〜6行のみです。データ配列を確認しましたが、正しいデータがその行に渡されています。さらに、行6をスクロールして行2に移動すると、同じインデックスにスクロールされます。セルの再利用をオフにすると、この問題は解決しますが、スクロールがスムーズになりません。問題のあるセルタイプのテーブルビューコードは次のとおりです。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"HorizontalCell";
HorizontalTableCell *cell = (HorizontalTableCell *)[self.homeTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[HorizontalTableCell alloc] initWithFrame:CGRectMake(0, 0, self.homeTable.frame.size.width, self.homeTable.frame.size.height)];
// Configure cell
UIImage *bgImage = [UIImage imageNamed:@"verticalCell-back.png"];
UIImageView *bgView = [[UIImageView alloc]initWithFrame:cell.frame];
[bgView setImage:bgImage];
cell.backgroundView = bgView;
// Set up the title label
UILabel * titleLabel = [[UILabel alloc]init];
titleLabel.tag = TITLE_LABEL_TAG;
titleLabel.font = [UIFont boldSystemFontOfSize:13];
titleLabel.textColor = [UIColor lightGrayColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.shadowColor = [UIColor blackColor];
titleLabel.shadowOffset = CGSizeMake(0, -1);
// Set up the value label
UILabel * titleValueLabel = [[UILabel alloc]init];
titleValueLabel.tag = TITLE_VALUE_LABEL_TAG;
titleValueLabel.font = [UIFont boldSystemFontOfSize:13];
titleValueLabel.textColor = [UIColor whiteColor];
titleValueLabel.backgroundColor = [UIColor clearColor];
titleValueLabel.shadowColor = [UIColor blackColor];
titleValueLabel.shadowOffset = CGSizeMake(0, -1);
[cell addSubview:titleValueLabel];
[cell addSubview:titleLabel];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.data = [dataArray objectAtIndex:indexPath.section];
cell.horizontalTableView.scrollsToTop = NO;
for (int i = 0; i < [[dataArray objectAtIndex:indexPath.section] count]; i++ ) {
PosterData *poster = [[dataArray objectAtIndex:indexPath.section] objectAtIndex:i];
NSLog(@"Row Number %u Data %@", indexPath.section, poster.name);
}
//Set up the title label
NSArray *labelTextArray = [[titlesArray objectAtIndex:indexPath.section] componentsSeparatedByString:@"**"];
CGSize expectedLabelSize = [[labelTextArray objectAtIndex:0] sizeWithFont:[UIFont boldSystemFontOfSize:13]
constrainedToSize:CGSizeMake(280, 15)
lineBreakMode:NSLineBreakByTruncatingTail];
// Get the labels
UILabel * titleLabel = (UILabel *)[cell viewWithTag:TITLE_LABEL_TAG];
UILabel * titleValueLabel = (UILabel *)[cell viewWithTag:TITLE_VALUE_LABEL_TAG];
titleLabel.frame = CGRectMake(8, 13, expectedLabelSize.width + 2, 15);
titleValueLabel.frame = CGRectMake(expectedLabelSize.width + 8, 13, 150, 15);
titleLabel.text = [labelTextArray objectAtIndex:0];
NSLog(@"Label Text Array %@", labelTextArray);
if ([labelTextArray count] > 1) {
titleValueLabel.text = [labelTextArray objectAtIndex:1];
}
else titleValueLabel.text = @"";
return cell;