2011-05-25 165 views
0

我使用NSThread下載了一些圖像。當下載所有圖像時,我必須將它們放入cell.myimageview。給我提供用戶定義方法設置圖像的解決方案。在表視圖單元格內的UIImageView中設置圖像

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


    static NSString *CellIdentifier = @"Cell"; 
    TableCell *cell = (TableCell *)[TableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[[TableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

    } 


    NSString *bedsbaths=[NSString stringWithFormat:@"Beds:%@ Baths:%@",[[AppDeleget.statuses valueForKey:@"beds"] objectAtIndex:indexPath.row],[[AppDeleget.statuses valueForKey:@"baths"] objectAtIndex:indexPath.row]]; 
    cell.mlsno.text=[[AppDeleget.statuses valueForKey:@"mlsno"] objectAtIndex:indexPath.row]; 
    cell.price.text=[[AppDeleget.statuses valueForKey:@"price"] objectAtIndex:indexPath.row]; 
    cell.address.text=[[AppDeleget.statuses valueForKey:@"address"] objectAtIndex:indexPath.row]; 
    cell.bedsbaths.text=bedsbaths; 
    cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton; 
return cell; 

} 
-(void)LoadImage 
{ 
    for(int x=0;x<[ListPhotos count];x++) 
    { 
     NSData *imageData =[ListPhotos objectAtIndex:x]; 
     id path = imageData; 
     NSURL *url = [NSURL URLWithString:path]; 
     NSLog(@"%@",url); 
     NSData *data = [NSData dataWithContentsOfURL:url]; 
     UIImage *img = [[UIImage alloc] initWithData:data]; 
     [self performSelectorOnMainThread:@selector(downloadDone:) withObject:img waitUntilDone:NO]; 
    } 

} 
-(void)downloadDone:(UIImage*)img { 

    // I have to set the cell here. How?   
    cell.myimageView.image=img 
} 

回答

0

只要你保持對單元格的引用,它並不難。我會去了解它是這樣的:

- (void)downloadDone:(UIImage*)img { 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:yourCellRow inSection:yourCellSection]; 
    UITableViewCell *cell = [myTableView cellForRowAtIndexPath:indexPath]; 
    // now you have the cell you wish to modify; 
    cell.myimageView.image=img; 
    [myTableView reloadData]; 
    // if you would prefer, you could also call [myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone] 
    // if you only want to reload that specific cell; 
} 
+0

不工作的代碼plz幫助 – user763657 2011-05-25 09:29:45

+0

我只是測試這一點,它爲我工作得很好。我使用了'cell.imageView.image = [UIImage imageNamed:@「testImage.png」];',但它是一樣的想法。嘗試在'downloadDone'中設置一個日誌語句來說明'NSLog(@「圖像是%@」,img);'以確保圖像存在。我的猜測是,該圖像要麼不存在,要麼在發佈之前就已經發布(我們可以在以後找出問題後再修復) – justin 2011-05-25 18:08:24

0

即使你可以將其設置在downloadDone:方法,你以後會面對的,因爲可重複使用的電池的問題。所以設置圖像的正確位置應該是tableView:cellForRowAtIndexPath:本身。那麼你如何加載圖像?將它們保存在數組或字典中。說的次數不改變,我們就可以在​​

for(int x=0;x<[ListPhotos count];x++) 
{ 
    ... 
    [photos replaceObjectAtIndex:x withObject:image]; 
    [self performSelectorOnMainThread:@selector(downloadDone:) 
          withObject:[NSNumber numberWithInt:x]; 
         waitUntilDone:NO]; 
} 

downloadDone:

使用 NSMutableArray對象 NSNull單一對象的店鋪計數以後,

tableView:cellForRowAtIndexPath:

if ([[images objectAtIndex:indexPath.row] isMemberOfClass:[UIImage class]]) { 
    cell.myImageView.image = [images objectAtIndex:indexPath.row]; 
} 

- (void)downloadDone:(NSNumber *)row { 

    [self.tableView reloadRowsAtIndexPaths:[NSIndexPath indexPathForRow:[row intValue] inSection:0] 
          withRowAnimation:UITableViewRowAnimationTop]; 
} 
+0

無法使用此代碼plz help – user763657 2011-05-25 09:30:09

+0

您面臨的錯誤是什麼? – 2011-05-25 10:10:24

0

您可以將圖像設置爲cellForRowAtIndexPath方法本身中的圖像視圖。試試下面的代碼:

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


    static NSString *CellIdentifier = @"Cell"; 
    TableCell *cell = (TableCell *)[TableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[[TableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

    } 


    NSString *bedsbaths=[NSString stringWithFormat:@"Beds:%@ Baths:%@",[[AppDeleget.statuses valueForKey:@"beds"] objectAtIndex:indexPath.row],[[AppDeleget.statuses valueForKey:@"baths"] objectAtIndex:indexPath.row]]; 
    cell.mlsno.text=[[AppDeleget.statuses valueForKey:@"mlsno"] objectAtIndex:indexPath.row]; 
    cell.price.text=[[AppDeleget.statuses valueForKey:@"price"] objectAtIndex:indexPath.row]; 
    cell.address.text=[[AppDeleget.statuses valueForKey:@"address"] objectAtIndex:indexPath.row]; 
    cell.bedsbaths.text=bedsbaths; 
    NSString *filePath = [self getImagePath]; 
    UIImage * img = [UIImage imageWithContentsOfFile:filePath]; 
    cell.myimageView.image=img; 
    cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton; 
    return cell; 

} 
    -(NSString *)getImagePath 
    { 
     /*here you can get the path of your image here*/ 

    } 
0

在你的cellForRowAtIndexPath

UIImageView *logoImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 55)]; 
logoImgView.backgroundColor = [UIColor clearColor]; 
[cell.Imageview addSubview:logoImgView]; 
NSMutableArray *arrChng = [[NSMutableArray alloc] init]; 
[arrChng addObject:logoImgView]; 
[arrChng addObject:[Array objectAtIndex:indexPath.row]; 
[self performSelectorInBackground:@selector(setImagesAfterShow:) withObject:arrChng]; 


-(void)setImagesAfterShow:(NSMutableArray *)array 
{ 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
NSURL *url = [NSURL URLWithString:[array objectAtIndex:1]]; 
NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 
UIImageView *img = [array objectAtIndex:0]; 
img.image = [UIImage imageWithData:data]; 
[pool release]; 
} 

這種方法在後端用於加載圖像,因此很容易平滑表的滾動

使用ASIHTTPRequest還有一個方法,其中圖像加載在後端,它是非常好的使用檢查link。它可能對你有用。

2

在含視圖控制器

下的方法的tableView:的cellForRowAtIndexPath:創建一個新的小區時,廣告代碼:

cell.imageView.image = [UIImage [email protected]"name of image.png"];

相關問題