2010-10-26 46 views
0

我已經發布了我的應用程序,並且在CellForRowAtIndexPath中滾動時注意到了干擾,這是因爲我通過URL從JSON Feed呈現圖像。我搜索了互聯網,並找到了幾個例子,這一個我幾乎工作。問題是當它呈現圖像不匹配正確的標題。有人可以請看看,看看我是否做了明顯錯誤的事情。Iphone cellForRowAtIndexPath當異步滾動問題時出現問題

反正這裏是我的代碼:

- (void)displayImage:(UIImage *)image { 
[photo setImage:image]; 

}

-(void)loadImage:(NSString *)url { 
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]]; 
UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease]; 
[imageData release]; 
[self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO]; 

}

的cellForRowAtIndexPath Methord

#define DATELABEL_TAG 1 #define MAINLABEL_TAG 2 #define PHOTO_TAG 3 UIImageView *photo; 



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




static NSString *MainNewsCellIdentifier = @"MainNewsCellIdentifier"; 

UILabel *mainLabel, *dateLabel; 


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: MainNewsCellIdentifier]; 

if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: MainNewsCellIdentifier] autorelease]; 
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

    dateLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15.0,15.0,170.0,15.0)] autorelease]; 
    dateLabel.tag = DATELABEL_TAG; 
    dateLabel.font = [UIFont systemFontOfSize:10.0]; 
    dateLabel.textAlignment = UITextAlignmentLeft; 
    dateLabel.textColor = [UIColor darkGrayColor]; 
    dateLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; 
    [cell.contentView addSubview:dateLabel];  

    mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15.0,28.0,170.0,60.0)] autorelease]; 
    mainLabel.tag = MAINLABEL_TAG; 
    mainLabel.font = [UIFont boldSystemFontOfSize:14.0]; 
    mainLabel.textColor = [UIColor blackColor]; 
    mainLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin; 
    mainLabel.numberOfLines = 0; 
    [cell.contentView addSubview:mainLabel]; 


    photo = [[[UIImageView alloc] initWithFrame:CGRectMake(190.0,15.0,85.0,85.0)] autorelease]; 
    photo.tag = PHOTO_TAG;  
    photo.contentMode = UIViewContentModeScaleAspectFit; 

    [cell.contentView addSubview:photo]; 
} 
else { 

    dateLabel = (UILabel *)[cell.contentView viewWithTag:DATELABEL_TAG]; 
    mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG]; 
    photo = (UIImageView *)[cell.contentView viewWithTag:PHOTO_TAG]; 
} 

NSUInteger row = [indexPath row]; 
NSDictionary *stream = (NSDictionary *) [dataList objectAtIndex:row]; 
NSString *title = [stream valueForKey:@"title"]; 


NSString *titleString = @""; 

if(! [title isKindOfClass:[NSString class]]) 
{ 
    titleString = @""; 
} 
else 
{ 
    titleString = title; 
} 

CGSize maximumSize = CGSizeMake(180, 9999); 

UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14]; 
CGSize dateStringSize = [titleString sizeWithFont:dateFont 
           constrainedToSize:maximumSize 
            lineBreakMode:mainLabel.lineBreakMode]; 

CGRect dateFrame = CGRectMake(15.0, 28.0, 170.0, dateStringSize.height); 
mainLabel.frame = dateFrame; 

mainLabel.text = titleString; 
dateLabel.text = [stream valueForKey:@"created"]; 

NSString *i = [NSString stringWithFormat:@"http://www.domain.co.uk/images/stories/%@", [stream valueForKey:@"image"]]; 


photo.image = [UIImage imageNamed:@"i_digital_media.png"]; 
NSOperationQueue *queue = [NSOperationQueue new]; 
NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
            initWithTarget:self 
            selector:@selector(loadImage:) 
            object:i]; 
[queue addOperation:operation]; 
[operation release]; 


return cell;} 
+0

有大量的代碼在那裏,有很多問題,爲什麼照片中的靜態變量? – 2010-10-26 11:28:23

+0

因此,在'displayImage'方法中可以看出,這是錯誤的方法嗎?任何提示都會有幫助。 – Robert 2010-10-26 11:34:48

回答

0

通過你的圖像加載的時候,照片變量i小號指出,不同的照片:)

你需要傳遞兩個要更新背部加載圖像的UIImageView的 - 嘗試使用的NSDictionary在線程之間傳遞變量:

- (void)displayImage:(NSDictionary *)info { 
    [[info objectForKey:@"photo"] setImage:[info objectForKey:@"image"]]; 
} 


-(void)loadImage:(NSDictionary *)info { 
    NSString *imagePath = [info objectForKey:@"imagePath"]; 
    UIImageView *photo = [info objectForKey:@"photo"]; 

    // Load the image 
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]]; 
    UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease]; 
    [imageData release]; 

    // Pass it back to the main thread 
    NSDictionary *mainThreadInfo = [NSdictionary dictionaryWithObjectAndKeys:image, @"image", photo, @"photo", nil]; 
    [self performSelectorOnMainThread:@selector(displayImage:) withObject:mainThreadInfo waitUntilDone:YES]; 
} 

所以現在,您的操作會將照片和圖像數據傳回主線程。創建這樣的操作:

NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:i, @"imagePath", photo, @"photo", nil]; 
NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
            initWithTarget:self 
            selector:@selector(loadImage:) 
            object:info]; 

,擺脫全局變量照片:)的

+0

回頂部top top top – Robert 2010-10-26 13:08:05

+0

我打算命名我的第二個孩子Dean。 – Robert 2010-10-26 13:08:23

+0

我的其他代碼在內存泄漏方面的任何提示? – Robert 2010-10-26 13:09:07