我有一个tableview,每个单元格包含另一个用于水平滚动的tableview。主要的tableview只有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行时,它也会滚动到相同的索引。当我关闭重用单元格时,这个问题消失了,但滚动不顺畅。这是故障单元类型的tableview代码。
- (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;
答案 0 :(得分:0)
看到没有人评论,我解决了这个问题。以为我会分享答案。我没有在我的单元格中实现-(void) prepareForReuse
。我只是在该方法中重新加载水平tableView,解决了我的问题