2013-03-04 22 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,解決了我的問題