Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iphone中滚动表格时图像会发生变化吗?_Iphone_Objective C_Cocoa Touch - Fatal编程技术网

在iphone中滚动表格时图像会发生变化吗?

在iphone中滚动表格时图像会发生变化吗?,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我使用xml获取数据并将其显示在uitable中。在XML中,图像有一个单独的标记“link”。我将此图像显示为细胞图像。当应用程序加载时,一切正常,但当我滚动表格时,单元格中的图像会发生变化 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell";

我使用xml获取数据并将其显示在uitable中。在XML中,图像有一个单独的标记“link”。我将此图像显示为细胞图像。当应用程序加载时,一切正常,但当我滚动表格时,单元格中的图像会发生变化

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType=UITableViewCellAccessoryNone;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        //cell. separatorStyle=UITableViewCellSeparatorStyleNone;

        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 190, 130)];
        [titleLabel setTag:2];
        titleLabel.numberOfLines=7;
        [titleLabel setFont:[UIFont systemFontOfSize:14]];
        [titleLabel setTextColor:[UIColor blackColor]];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        [cell.contentView addSubview:titleLabel];
        [titleLabel release]; 


        imageView= [[UIImageView alloc] initWithFrame:CGRectMake(5, 20, 90, 110)];
        [cell.contentView addSubview:imageView];
    }

    NSDictionary *appRecord = [responseArr objectAtIndex:indexPath.row];

    NSString *urlLink;
    urlLink=[NSString stringWithFormat:@"%@",[appRecord objectForKey:@"link"]];
    NSLog(@"LINK : %@",urlLink);

    if([urlLink length] ==0)
    {
        imageView.image=[UIImage imageNamed:@"ioffer.png"];

    }
    else {

    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlLink]];
    UIImage *image = [[UIImage alloc] initWithData:imageData];
    imageView.image = image;

    }   

    UILabel *desclbl = (UILabel*)[cell.contentView viewWithTag:2];
    desclbl.text = [NSString stringWithFormat:@"%@",[appRecord objectForKey:@"description"]];


    return cell;

}
我已经在表的CellForRowatineXpath中使用了这些代码。responseArr是我存储链接的数组

在这个问题上有谁能帮我吗

提前谢谢
马拉提人

我也有同样的问题。滚动时,这些单元格将在其他索引处重复使用。 尝试将用数据填充单元格的代码放在“if(cell==nil)”之外。为我工作

致以最良好的祝愿,
Alexander

如果重用现有的表视图单元格,即代码跳过第一个If块,则不会初始化
imageView
(它在哪里声明?)。这意味着,即使您设置了一个新文本,也会重复使用一些随机图像

解决方法是确保在所有情况下都初始化了
imageView

请用这个方法在本地声明它。其他一切对我来说似乎都很危险。

if(cell==nil)的“隐含的其他”是它在重用表缓存中的单元格。它不会重置任何关于这些的内容,它只是拾取并使用它们。这意味着你在上面的任何元素都需要手动清除,特别是如果它们需要一段时间才能被填充的话

您已经在进行一些默认操作,如下所示:

if([urlLink length] ==0)
{
    imageView.image=[UIImage imageNamed:@"ioffer.png"];

}
如果你不加条件的话

    imageView.image=[UIImage imageNamed:@"ioffer.png"];
您将得到的是,您的单元格将加载其默认图像,然后当图像远程加载时,它将替换它


现在:您正在同步进行图像加载。这意味着当web请求发出时,整个UI都将冻结。不太好。

您还可以使用自定义单元格而不是此单元格,并每次检查 如下例所示- 条件-

    if(shout.creator.avatarImage){
        avatarView.image = shout.creator.avatarImage;
    }
    else{

        FetchImageOperation *operation = [[FetchImageOperation alloc] init];

        operation.delegate = self;
        operation.urlString = shout.creator.avatarURL;

        [operationQueue addOperation:operation];
        [operation release];
    }

-(void)fetchthumbImageOperation:(FetchThumbImageOperation *)operation didFetchImage:(UIImage *)image

{
    avatarView.image= image;

}

Here, i check the condition for image in tableview data source.

-(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

}

在本例中,下面给出了有关shout和creator的一些详细信息-@interface creator:NSObject和@interface shout:NSObject