在tableview单元格上动态显示图像

时间:2010-07-20 06:27:38

标签: iphone uitableview

在我的应用中,我需要在tableview单元格上显示图像或图像,具体取决于它们在数据库中的可用性。我所做的是我已经种植了四个网页浏览[显示来自网址的图片],我隐藏或显示这些视图取决于它们的可用性。

当我完全隐藏网页浏览时,可能还会出现无图像的情况。

但随机滚动后,我的应用程序崩溃了。此外,滚动它时显示较旧的图像。所以,我首先能够看到一个图像,然后在滚动相同的单元格后,两个图像相互引导[早期照片的某些部分可见],依此类推。这可能是因为细胞可重用性吗?关于这个问题的理想方法是什么?

编辑:

我正在使用以下代码:

noOfPhotos = [photos_array count];

if(noOfPhotos > 0){

    commentWhenWebviewNotThere.alpha = 0.0;         //Things that are not visible when images are there
    noOfCommentsWhenWebviewNotThere.alpha = 0.0;
    commentsLblWhenWebviewAbset.alpha = 0.0;

    image1.hidden = NO;
    image2.hidden = NO;
    image3.hidden = NO;
    image4.hidden = NO;

    commentsLblWhenWebview.alpha = 1.0;
    noOfComments.alpha = 1.0;
    comment.alpha = 1.0;

for(NSInteger x = 0; x < noOfPhotos; x++){
    photoName = [photos_array objectAtIndex:x];

    NSString *urlAddress = [NSString stringWithFormat:@"%@",photoName];   
    NSURL *url = [NSURL URLWithString:urlAddress];           
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];            

    if(x == 0){
        image1 = [[UIWebView alloc] initWithFrame:CGRectMake(10, comment.frame.origin.y - 5, 60, 60)];
    [image1 loadRequest:requestObj];              //Load the request in the UIWebView.
        [self addSubview:image1];
    }

    else if(x == 1){
        image2 = [[UIWebView alloc] initWithFrame:CGRectMake(88, comment.frame.origin.y - 5, 60, 60)];

    [image2 loadRequest:requestObj];              //Load the request in the UIWebView.
        [self addSubview:image2];

    }

    else if(x == 2){
        image3 = [[UIWebView alloc] initWithFrame:CGRectMake(169, comment.frame.origin.y - 5, 60, 60)];

        [image3 loadRequest:requestObj];              //Load the request in the UIWebView.
        [self addSubview:image3];

    }

    else if(x == 3){
        image4 = [[UIWebView alloc] initWithFrame:CGRectMake(251, comment.frame.origin.y - 5, 60, 60)];

        [image4 loadRequest:requestObj];              //Load the request in the UIWebView.
        [self addSubview:image4];

    }

}
}

else{
    commentWhenWebviewNotThere.alpha = 1.0;    //Should be visible when images are not there
    noOfCommentsWhenWebviewNotThere.alpha = 1.0;
    commentsLblWhenWebviewAbset.alpha = 1.0;

    commentsLblWhenWebview.alpha = 0.0;
    noOfComments.alpha = 0.0;
    comment.alpha = 0.0;

    image1.hidden = YES;
    image2.hidden = YES;
    image3.hidden = YES;
    image4.hidden = YES;
}

提前完成。

1 个答案:

答案 0 :(得分:0)