iOS tableview customcell - 屏幕外单元格上的控件有时不可见

时间:2015-07-31 13:22:38

标签: ios objective-c iphone xcode uitableview

我在一个包含5个单元格的表视图上遇到了一个奇怪的错误。由于大的单元高度,第五个单元最初在屏幕外,用户需要向下滚动才能看到它。单元格显示下载按钮,进度指示器,如果下载完成,则显示“查看”按钮,而不是下载和进度。

前4个单元格正确显示。然而,第五个单元格有时会丢失下载按钮,但有时它会在每次重启应用程序时以大约50%的几率正确显示。源数据在任何时候都不会更改。

我使用了断点来查看向下滚动时会发生什么,但是我无法发现任何异常情况,并且当单元格出现时数据已正确提供。

有人可以说出我应该关注的地方吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
    static NSString *MyIdentifier = @"cell1";
    NSDictionary *categoryDictionary= [[appdelegate categoriesArray] objectAtIndex:indexPath.row];    
    categoryTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];    
    rect=cell.labelProgressView.frame;
    cell.backgroundColor = [UIColor PGColorBackground];    
    cell.buttonView.hidden=YES;        
    //Set background colors and icons
    switch ([[categoryDictionary valueForKey:@"categoryID"] intValue])
    {
        case 1:
            [cell.buttonView setBackgroundColor: [UIColor PGColorAttractions]] ;
            [cell.labelProgressView setBackgroundColor: [UIColor PGColorAttractions]] ;
            [cell.ivCategoryBannerImage setImage: [UIImage imageNamed:@"attraction-bg"]];
            cell.ivcategoryLogoImage.image =[UIImage imageNamed:@"attractionWhite"];
            break;
        case 2:
            [cell.buttonView setBackgroundColor: [UIColor PGColorRestaurants]];
            [cell.labelProgressView setBackgroundColor: [UIColor PGColorRestaurants]];
            [cell.ivCategoryBannerImage setImage: [UIImage imageNamed:@"restaurant-bg"]];
            cell.ivcategoryLogoImage.image =[UIImage imageNamed:@"restaurantWhite"];
            break;
        case 3:
            [cell.buttonView setBackgroundColor: [UIColor  PGColorShopping]];
            [cell.labelProgressView setBackgroundColor:[UIColor PGColorShopping]];
            [cell.ivCategoryBannerImage setImage: [UIImage imageNamed:@"shopping-bg"]];
            cell.ivcategoryLogoImage.image =[UIImage imageNamed:@"shoppingWhite"];
            break;
        case 4:
            [cell.buttonView setBackgroundColor: [UIColor  PGColorAccomodations]];
            [cell.labelProgressView setBackgroundColor:[UIColor PGColorAccomodations]];
            [cell.ivCategoryBannerImage setImage: [UIImage imageNamed:@"accomodation-bg"]];
            cell.ivcategoryLogoImage.image =[UIImage imageNamed:@"accomodationWhite"];
            break;

        case 5:
            [cell.buttonView setBackgroundColor: [UIColor PGColorArtGalleries]];
            [cell.labelProgressView setBackgroundColor:[UIColor PGColorArtGalleries]];
            [cell.ivCategoryBannerImage setImage: [UIImage imageNamed:@"artGallery-bg"]];
            cell.ivcategoryLogoImage.image =[UIImage imageNamed:@"artGalleryWhite"];
            break;

        default:
            break;            
    }

    //DOWNLOAD Button
    [cell.buttonViewDownload setBackgroundColor:[UIColor clearColor]];
    cell.buttonViewDownload.layer.borderColor=[UIColor whiteColor].CGColor;
    cell.buttonViewDownload.layer.borderWidth=1.0f;
    [cell.buttonViewDownload.titleLabel setFont:[UIFont fontWithName:@"OpenSans-Bold" size:10]];
    [cell.buttonViewDownload setTag:[[categoryDictionary valueForKey:@"categoryID"] intValue]];

    //VIEW Button
    cell.buttonView.layer.borderColor=[UIColor whiteColor].CGColor;
    cell.buttonView.layer.borderWidth=1.0f;
    [cell.buttonView.titleLabel setFont:[UIFont fontWithName:@"OpenSans-Bold" size:10]];
    [cell.buttonView setTag:[[categoryDictionary valueForKey:@"categoryID"] intValue]];
    [cell.buttonView addTarget:self action:@selector(viewbuttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    //CATEGORY label

    NSString *categoryName =[[NSString stringWithFormat:@"%@",[categoryDictionary valueForKey:@"categoryNameen"]] uppercaseString];

    NSMutableAttributedString *attributedString;
    attributedString = [[NSMutableAttributedString alloc] initWithString:categoryName];

    [attributedString addAttribute:NSKernAttributeName
                            value:[NSNumber numberWithFloat:8.0]
                            range:NSMakeRange(0, [categoryName length])];

    [attributedString addAttribute:NSFontAttributeName
                            value:[UIFont fontWithName:@"OpenSans-Bold" size:19]
                            range:NSMakeRange(0, [categoryName length])];


    [cell.labelCategory setAttributedText:attributedString];

    cell.selectionStyle=UITableViewCellAccessoryNone;    
    //Set State    
    cell.ivCategoryBannerImage.alpha =0.1f;

    BOOL isAvailable = [[categoryDictionary valueForKey:@"isAvailable"] boolValue];
    BOOL userTappedDownload = [[categoryDictionary valueForKey:@"userTappedDownload"] boolValue];

    NSLog(@"isAvailable: %d", isAvailable);
    NSLog(@"userTappedDownload: %d", isAvailable);

    if (isAvailable && userTappedDownload)
    {
        cell.buttonView.hidden=NO;
        [cell.buttonView setTitle:@"VIEW" forState:UIControlStateNormal];
        cell.buttonViewDownload.hidden=YES;
        cell.ivCategoryBannerImage.alpha =1.0f;
        rect.size.width=208.0f;
    }
    else if(!isAvailable && !userTappedDownload)
    {
        rect.size.width=0.1f;
        [cell.buttonViewDownload setTitle:[NSString stringWithFormat:@"DOWNLOAD (%@ KB)",[categoryDictionary valueForKey:@"totalImagesSize"]] forState:0];            
    }
    else if(!isAvailable && userTappedDownload)
    {
        rect.size.width=[[categoryDictionary valueForKey:@"totalImagesAvailable"] intValue];
        [cell.buttonViewDownload setTitle:@"DOWNLOADING CONTENT" forState:UIControlStateNormal];
        cell.buttonViewDownload.userInteractionEnabled = NO;      
    }    
    return cell;
}

1 个答案:

答案 0 :(得分:1)

可能是第五个单元格正在重复使用设置<table> <tr> <th scope="col"></th> <th scope="col">noRisk</th> <th scope="col">lowRisk</th> <th scope="col">mediumRisk</th> <th scope="col">HighRisk</th> </tr> <tr> <th scope="row"> <div class="lefttext">How old are you?</div> </th> <td> <input type="radio" id="value1" name="selectedAge" value="0" checked>1-25</td> <td> <input type="radio" id="value1" name="selectedAge" value="5">26-40</td> <td> <input type="radio" id="value1" name="selectedAge" value="8">41-60</td> <td> <input type="radio" id="value1" name="selectedAge" value="10">1-25</td> </tr> <tr> <th scope="row"> <div class="lefttext">What is you BMI?</div> </th> <td> <input type="radio" id="value2" name="selectedBmi" value="0" checked>0-25</td> <td> <input type="radio" id="value2" name="selectedBmi" value="0">26-30</td> <td> <input type="radio" id="value2" name="selectedBmi" value="9">31-35</td> <td> <input type="radio" id="value2" name="selectedBmi" value="10">35+</td> </tr> <tr> <th scope="row"> <div class="lefttext">Does anybody in your family have diabetes?</div> </th> <td> <input type="radio" id="value3" name="selectedDiabete" value="0" checked>No</td> <td> <input type="radio" id="value3" name="selectedDiabete" value="7">Grandparent</td> <td> <input type="radio" id="value3" name="selectedDiabete" value="15">Sibling</td> <td> <input type="radio" id="value3" name="selectedDiabete" value="15">Parent</td> </tr> <tr> <th scope="row"> <div class="lefttext">How would you describe your diabete</div> </th> <td> <input type="radio" id="value4" name="description" value="0" checked>Low sugar</td> <td> <input type="radio" id="value4" name="description" value="0">Normal sugar</td> <td> <input type="radio" id="value4" name="description" value="7">Quite high sugar</td> <td> <input type="radio" id="value4" name="description" value="10">High sugar</td> </tr> </table> <input type="button" name="submit" value="Submit" onclick="javascript:addNumbers()" />Total = <input type="text" id="answer" name="answer" value="" /> <section> <h2>Your results:</h2> <div class="results" id="lowResult"> <p>Your results show that you currently have a low risk of developing diabetes. However, it is important that you maintain a healthy lifestyle in terms of diet and exercise. </p> </div> <div class="results" id="mediumResult"> <p>Your results show that you currently have a medium risk of developing diabetes. For more information on your risk factors, and what to do about them, please visit our diabetes advice website at <a href="http://www.zha.org.zd">http://www.zha.org.zd</a>. <p> </div> <div class="results" id="highRisk"> <p>Your results show that you currently have a HIGH risk of developing diabetes. [Your main risk factors are your BMI and your diet.] We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you. </div> </section>的前一个单元格之一。

尝试将cell.buttonViewDownload.hidden=YES;添加到应显示下载按钮的分支。