【问题标题】:Setting an image in a UIImageView inside a table view cell在表格视图单元格内的 UIImageView 中设置图像
【发布时间】:2023-03-27 07:00:01
【问题描述】:

我使用NSThread 下载了一些图像。下载完所有图片后,我必须将它们放入cell.myimageview。给我一个用户自定义方法设置图片的解决方案。

- (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
}

【问题讨论】:

    标签: objective-c cocoa-touch ios uitableview uiimageview


    【解决方案1】:

    在包含视图控制器中

    方法tableView:cellForRowAtIndexPath:下创建新单元格时,广告代码:

    cell.imageView.image = [UIImage imageNamed@"name of image.png"];

    【讨论】:

      【解决方案2】:

      只要保留对单元格的引用,就不会太难。我会这样处理:

      - (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;
      }
      

      【讨论】:

      • 我刚刚对此进行了测试,对我来说效果很好。我用了cell.imageView.image = [UIImage imageNamed:@"testImage.png"];,但想法是一样的。尝试在downloadDone 时设置一条日志语句,上面写着NSLog(@"image is %@", img); 之类的内容,以确保图像存在。我的猜测是,图像要么不存在,要么在你设置它之前就被释放了(我们可以在找出问题后修复)
      【解决方案3】:

      即使你能够在downloadDone: 方法中设置它,由于可重复使用的单元格,你稍后也会遇到问题。所以设置图像的正确位置是tableView:cellForRowAtIndexPath: 本身。那么如何加载图像呢?将它们保存在数组或字典中。假设计数不变,我们可以使用NSMutableArray 对象来存储NSNull 单例对象的计数以及以后,

      在tableView:cellForRowAtIndexPath:

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

      在LoadImage

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

      在downloadDone:

      - (void)downloadDone:(NSNumber *)row {
      
          [self.tableView reloadRowsAtIndexPaths:[NSIndexPath indexPathForRow:[row intValue] inSection:0]
                                withRowAnimation:UITableViewRowAnimationTop];
      }
      

      【讨论】:

        【解决方案4】:

        您可以在 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*/
        
            }
        

        【讨论】:

          【解决方案5】:

          在你的 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 非常好。可能对你有用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-05-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-12-14
            相关资源
            最近更新 更多