在表视图单元格内的 UIImageView 中设置图像

发布于 2024-11-09 17:29:56 字数 1737 浏览 0 评论 0原文

我使用 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
}

I download some images using an NSThread. When all the images are downloaded, I have to put them in cell.myimageview. Give me the solution for setting an image in a user-defined method.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苦笑流年记忆 2024-11-16 17:29:57

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

    }

You can have set image to the Image view in the cellForRowAtIndexPath method itself. Try the below code:

- (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*/

    }
老街孤人 2024-11-16 17:29:57

在您的 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 的方法,其中图像在后端加载,使用起来非常好,请查看 链接。它可能对你有用。

In your 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];
}

this method used for loading images in backend so it's easy to smooth scrolling of table

One more method using ASIHTTPRequest in which the images are loading in backend and it's very good to use check out the link. It may be useful to you.

七七 2024-11-16 17:29:56

在包含视图控制器的

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

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

In the containing view controller

Under the method tableView:cellForRowAtIndexPath: when creating a new cell, ad the code:

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

一瞬间的火花 2024-11-16 17:29:56

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

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

As long as you keep a reference to the cell, it's not too hard. I'd go about it like this:

- (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;
}
浊酒尽余欢 2024-11-16 17:29:56

即使您能够在 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];
}

Even if you are able to set it in the downloadDone: method, you will face a problem later because of reusable cells. So the right place for setting the image would be the tableView:cellForRowAtIndexPath: itself. So how do you load the images? Save them in an array or a dictionary. Say the count doesn't change, we can use an NSMutableArray object to store count number of NSNull singleton objects and later,

in tableView:cellForRowAtIndexPath:

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

in LoadImage

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

in downloadDone:

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

    [self.tableView reloadRowsAtIndexPaths:[NSIndexPath indexPathForRow:[row intValue] inSection:0]
                          withRowAnimation:UITableViewRowAnimationTop];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文