我有一个表格视图,每个单元格都包含另一个用于水平滚动的表格视图。主表格视图只有 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;