gpt4 book ai didi

ios - 滚动时 UITableView 图像发生变化

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:52 24 4
gpt4 key购买 nike

我有一个带有自定义单元格UITableView。自定义单元格包含一个 UIView。图片异步加载到这里。但是当滚动我的表格 View 时,单元格中的图像会发生变化。这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCell";
MSAppointmentsModel *data = [self.appointments objectAtIndex:indexPath.row];

MSAppointmentsCell *cell = (MSAppointmentsCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *ar = [[NSBundle mainBundle] loadNibNamed:[Common checkDeviceforController:@"MSAppointmentsCell"] owner:nil options:nil];
for (id eachObject in ar) {
if ([eachObject isKindOfClass:[UITableViewCell class]]) {
cell = eachObject;
}
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSURL *imgUrl = [NSURL URLWithString:[data.cusDetails objectForKey:@"customer_image"]];
NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
UIImage *image = [UIImage imageWithData:imgData];
dispatch_sync(dispatch_get_main_queue(), ^{
[cell.medallionView setImage:image];
});
});

if (data.comp_status == YES) {
cell.status.image = [Common imageResize:[UIImage imageNamed:@"CheckMark_green.png"] andResizeTo:cell.status.frame.size];
}

}

cell.custName.text = [data.cusDetails objectForKey:@"customer_name"];
cell.service.text = [data service];
cell.empName.text = [NSString stringWithFormat:@"by %@",[data.empDetails objectForKey:@"employee_name"]];


return cell;
}

请帮帮我。

最佳答案

试试这个,

1:取数组缓存图片 NSMutablearray * imageicons;

2:下载数据后向数组添加NULL值;

 imageicons=[[NSMutableArray alloc] init];

for (int i=0; i<[self.appointments count]; i++)
{
[imageicons insertObject:[NSNull null] atIndex:i];
}

3:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row < [imageicons count] && [imageicons objectAtIndex:indexPath.row] != [NSNull null] )
{
[cell.bookimage setImage:[imageicons objectAtIndex:indexPath.row]];
NSLog(@"Array Hit");

}
else{
NSLog(@"Count of %i Size of %li Array and row number %i",[imageicons count],sizeof(imageicons),indexPath.row);
[NSThread detachNewThreadSelector:@selector(DownloadImageInBackground:) toTarget:self withObject:indexPath];
}
}

-(void)DownloadImageInBackground:(NSIndexPath *)path
{
MSAppointmentsModel *data = [self.appointments objectAtIndex:indexPath.row];
NSString *str=[data.cusDetails objectForKey:@"customer_image"];

imagetodisplay = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]]];

if (imagetodisplay==Nil)
{

// set deault image
[imageicons replaceObjectAtIndex:path.row withObject:[UIImage imageNamed:@"defaultimg.png"]];
}
else
{

MSAppointmentsCell *cell=(MSAppointmentsCell *)[self.booktableview cellForRowAtIndexPath:path];
[cell.bookimage setImage:imagetodisplay];
//NSLog(@"Count of %i Size of %li Array and row number %i",[imageicons count],sizeof(imageicons),path.row);

[imageicons replaceObjectAtIndex:path.row withObject:imagetodisplay];


}

}

关于ios - 滚动时 UITableView 图像发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17857300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com