2013-03-04 81 views
0

我有一个tableview,每个单元格包含另一个水平滚动的tableview。主桌面视图只有6个单元高,所以最初我没有重复使用单元格,虽然垂直滚动是波涛汹涌,所以我决定重用单元格。在垂直tableview中有3种不同类型的水平单元格。 2种类型工作得很好。 1类型正在复制。我在这里展示的图希望解释这更好:在某些情况下重复使用的UITableViewCells

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行显示的数据是一样的在第二行显示的数据显然有其他行是是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

看到没人评论,我解决了这个问题。以为我会分享答案。我没有在我的细胞中执行-(void) prepareForReuse。我只是在该方法重新加载水平tableView,解决了我的问题

相关问题