Objective c 在表视图单元格内的UIImageView中设置图像
我使用Objective c 在表视图单元格内的UIImageView中设置图像,objective-c,cocoa-touch,ios,uitableview,uiimageview,Objective C,Cocoa Touch,Ios,Uitableview,Uiimageview,我使用NSThread下载了一些图像。下载完所有图像后,我必须将它们放在cell.myimageview中。请给出在用户定义的方法中设置图像的解决方案 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; TableCell *cell
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
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
TableCell*cell=(TableCell*)[TableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[TableCell alloc]initWithFrame:CGRectZero重用标识符:CellIdentifier]autorelease];
}
NSString*bedsbaths=[NSString stringWithFormat:@“Beds:%@Baths:%@”,[[AppDeleget.statuses valueForKey:@“Beds”]对象索引:indexath.row],[AppDeleget.statuses valueForKey:@“Baths”]对象索引:indexath.row];
cell.mlsno.text=[[AppDeleget.statuses valueForKey:@“mlsno”]objectAtIndex:indexath.row];
cell.price.text=[[AppDeleget.statuses valueForKey:@“price”]objectAtIndex:indexath.row];
cell.address.text=[[AppDeleget.statuses valueForKey:@“address”]objectAtIndex:indexPath.row];
cell.bedsbaths.text=bedsbaths;
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
返回单元;
}
-(void)加载映像
{
对于(int x=0;x,只要您保留对单元格的引用,这并不太难。我会这样做:
- (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;
}
即使您能够在downloadDone:
方法中设置它,您以后也会遇到问题,因为单元格是可重用的。因此设置图像的正确位置应该是tableView:CellForRowatineXpath:
本身。那么如何加载图像?将它们保存在数组或字典中。假设计数不变,我们可以使用NSMutableArray
对象存储NSNull
单例对象及更高版本的计数编号
在tableView:cellforrowatinexpath:
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];
}
您可以在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*/
}
在你的牢房里
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的方法,其中图像在后端加载,使用该方法非常好。请检查。它可能对您有用。在包含的视图控制器中
在tableView:cellForRowAtIndexPath方法下:创建新单元格时,输入代码:
cell.imageView.image=[UIImage imageNamed@“name of image.png”];
我刚刚测试了这个,效果很好。我使用了cell.imageView.image=[UIImage imageNamed:@“testImage.png”];
,但想法是一样的。在下载完成时尝试设置一个日志语句,类似于NSLog(@“图像是%@”,img);
以确保图像存在。我猜图像要么不存在,要么在您可以设置它之前被释放(我们可以在找出问题后修复)
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];
}