2011-08-22 41 views
0

您好我想在組表中添加uilabel。我使用的方法是,我的整個邏輯單元格創建是在uitableviewcellforrowatindexpath即;我沒有使用單獨的類(例如:customcell.h或類似的東西)。 我成功地添加了標籤,但自定義更改僅反射到零段的行。 然後我試着如果(indexpath.section == 0)和if(indexpath.section == 1)仍然只是反映到第零部分行。也不需要if子句,因爲所有單元格的外觀和感覺是相同。 這裏是我的代碼:?uitableview單元格,並試圖在分組表中添加uilabel:僅反映在零部分的行中的更改

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
//code STATS here : initializing here for cells background view & selection back ground view  
    cell.backgroundView = 
    [[[UIImageView alloc] init] autorelease]; 
    cell.selectedBackgroundView = 
    [[[UIImageView alloc] init] autorelease]; 

    //code ENDS HERE of initializing 
    //custom label 

    UIImage *indicatorImage = [UIImage imageNamed:@"indicator.png"]; 
    cell.accessoryView = 
    [[[UIImageView alloc] 
     initWithImage:indicatorImage] 
    autorelease]; 

    const CGFloat LABEL_HEIGHT = 20; 
    UIImage *image = [UIImage imageNamed:@"filter.png"];//this image is on top //left of section zero rows 

    // 
    // Create the label for the top row 
    // 

     topLabel = 
     [[[UILabel alloc] 
      initWithFrame: 
      CGRectMake(
        image.size.width + 2.0 * cell.indentationWidth, 
        0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT), 
        aTableView.bounds.size.width - 
        image.size.width - 4.0 * cell.indentationWidth 
        - indicatorImage.size.width, 
        LABEL_HEIGHT)] 
     autorelease]; 
     [cell.contentView addSubview:topLabel]; 

     // 
     // Configure the properties for the text that are the **SAME** on every row 
     // 
     topLabel.tag = TOP_LABEL_TAG; 
     topLabel.backgroundColor = [UIColor clearColor]; 
     topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0]; 
     topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0]; 
     topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; 
     //testing: 
     [email protected]"test1"; 
     // 
     // Create the label for the top row of text 
     // 
     bottomLabel = 
     [[[UILabel alloc] 
      initWithFrame: 
      CGRectMake(
        image.size.width + 2.0 * cell.indentationWidth, 
        0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT) + LABEL_HEIGHT, 
        aTableView.bounds.size.width - 
        image.size.width - 4.0 * cell.indentationWidth 
        - indicatorImage.size.width, 
        LABEL_HEIGHT)] 
     autorelease]; 
     [cell.contentView addSubview:bottomLabel]; 

     // 
     // Configure the properties for the text that are the same on every row 
     // 
     bottomLabel.tag = BOTTOM_LABEL_TAG; 
     bottomLabel.backgroundColor = [UIColor clearColor]; 
     bottomLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0]; 
     bottomLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0]; 
     bottomLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2]; 
     [email protected]"test2"; 
    } 


    //testing for section 2 
    //this image is on top left of section one rows 
     UIImage *image1 = [UIImage imageNamed:@"akshardham.jpg"]; 
     topLabel1 = 
     [[[UILabel alloc] 
      initWithFrame: 
      CGRectMake(
        image1.size.width + 2.0 * cell.indentationWidth, 
        0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT), 
        aTableView.bounds.size.width - 
        image1.size.width - 4.0 * cell.indentationWidth 
        - indicatorImage.size.width, 
        LABEL_HEIGHT)] 
     autorelease]; 
     [cell.contentView addSubview:topLabel1]; 

     topLabel1.tag = TOP_LABEL_TAG1; 
     topLabel1.backgroundColor = [UIColor clearColor]; 
     topLabel1.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0]; 
     topLabel1.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0]; 
     topLabel1.font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; 
     //testing: 
     [email protected]"test2section"; 
    } 


} 

else 
{ 
    topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG]; 
    bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG]; 
    topLabel1 = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG1]; 
} 

任何建議感謝..

回答

1

它就像你的整個代碼在

if (cell == nil) { 

} 

所以你不會進去的時候,你會滾動,因爲你將有足夠的單元格創建。您將重複使用它們,並且單元格不會爲零,而且您不會使用您的自定義代碼。

待辦事項:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    static NSString * MYTableViewCellIdentifier = @"MYTableViewCellIdentifier";  

    MyTableViewCell * cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MYTableViewCellIdentifier];  
    if (cell == nil) { 
     // Create the cell 
     // Global layout for the cell   
    }  
    // Customization code  
    return cell;  
} 
+0

嘿感謝您指出我只是評論說,如果(細胞==零){}和事情會很好...但如果它是一個很好的做法...我只是看着這個......一些研發和谷歌搜索 – Alok

+1

儘量在你的代碼中使用相同的模式: *我把它放在我的答案中,因爲註釋對格式化代碼不好* – Zoleas

相關問題