gpt4 book ai didi

iphone - 自定义表格 View 中的图像未按正确顺序显示

转载 作者:行者123 更新时间:2023-11-29 13:07:21 24 4
gpt4 key购买 nike

我有一个应用程序在我完成数组后从数据库中加载图像我从图像数组中为缩略图图像生成一个新数组,当应用程序加载时,我使用 sql 从数据库中排序数据我记录图像和缩略图数组都排序正常,问题是 tableview 中的缩略图图像没有按照缩略图数组的相同顺序排序。

这是我的代码

用于获取数据排序的 php 代码:

$result = mysql_query("SELECT * FROM $tableName WHERE user = '$u' ORDER BY id DESC");

然后是xcode代码

- (void)viewDidLoad {
username = [savedStock objectForKey:@"username"];

//========Load The Array From Server=============\\

NSString *strURL = [NSString stringWithFormat:@"%@Load.php?username=%@", siteHost,username];

NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

// to receive the returend value
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

tempArray = [strResult componentsSeparatedByString:@"+-+"];

NSString *tempURL = [tempArray objectAtIndex:0];
NSString *tempDEC = [tempArray objectAtIndex:1];
NSString *tempTIME = [tempArray objectAtIndex:2];
NSString *tempDATE = [tempArray objectAtIndex:3];

tempArrayURL = [tempURL componentsSeparatedByString:@"/u/"];
array = [[NSMutableArray alloc] initWithArray:tempArrayURL];

tempArrayTime = [tempTIME componentsSeparatedByString:@"/t/"];
arrayTime = [[NSMutableArray alloc] initWithArray:tempArrayTime];

tempArrayDate = [tempDATE componentsSeparatedByString:@"/d/"];
arrayDate = [[NSMutableArray alloc] initWithArray:tempArrayDate];

tempArrayDesc = [tempDEC componentsSeparatedByString:@"/r/"];
arrayDesc = [[NSMutableArray alloc] initWithArray:tempArrayDesc];

number = [array count] - 1;

arrayThumbs = [[NSMutableArray alloc] init];

for (int i = 0; i<number; i++) {
NSString *tempString = [array objectAtIndex:i];
NSString *lastURl = [tempString substringFromIndex:[tempString length]-14];
NSString *thumb = [lastURl substringToIndex:[lastURl length]-4];
NSString *newLast = [thumb stringByAppendingString:@"_thumb.jpg"];
NSString *firstURL = [tempString substringToIndex:[tempString length]-14];
NSString *addOne = [firstURL stringByAppendingString:@"thumb/"];
NSString *newURL = [addOne stringByAppendingString:newLast];
[arrayThumbs addObject:newURL];
}

[arrayThumbs addObject:@""];

NSLog(@"%@",array);
NSLog(@"%@",arrayThumbs);
}

加载 TableView 中的所有内容:

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

static NSString *CellIdentifier = @"CustomCell";

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[CustomCell class]]) {
cell = (CustomCell *) currentObject;
break;
}
}

//using the code here to load the thumbnail images misses up the order but everything else is ok
NSString *tempString = [arrayThumbs objectAtIndex:indexPath.row];
NSURL *imageURL = [NSURL URLWithString:tempString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
cell.img.image = image;

}

UIImage *cellImage = [UIImage imageNamed:@"CustomCell.png"];
UIImageView *cellBackgroundImage = [[UIImageView alloc]initWithImage:cellImage];
cell.backgroundView = cellBackgroundImage;

cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CustomCellSelected.png"]];

//using it here will load the thumbnail perfectly but the tableview freeze when i scroll

// NSString *tempString = [arrayThumbs objectAtIndex:indexPath.row];
// NSURL *imageURL = [NSURL URLWithString:tempString];
// NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
// UIImage *image = [UIImage imageWithData:imageData];
// cell.img.image = image;

cell.URL.text = [array objectAtIndex:indexPath.row];
cell.Time.text = [arrayTime objectAtIndex:indexPath.row];
cell.Date.text = [arrayDate objectAtIndex:indexPath.row];
cell.Des.text = [arrayDesc objectAtIndex:indexPath.row];

cell.URL.textColor = [UIColor whiteColor];
cell.Time.textColor = [UIColor whiteColor];
cell.Date.textColor = [UIColor whiteColor];
cell.Des.textColor = [UIColor whiteColor];

return cell;
}

**请注意,当我更改缩略图代码位置时它工作正常但在滚动时卡住 tableview

这是两个数组的日志

2013-08-16 07:04:29.367 img[12575:c07] (
"http://localhost/img/i/YzrLnuCFmR.jpg",
"http://localhost/img/i/43BXPwhEqg.jpg",
"http://localhost/img/i/qEuPrbSBfX.jpg",
"http://localhost/img/i/ZDs0q5aoSx.jpg",
"http://localhost/img/i/EbfO8a9gn5.jpg",
"http://localhost/img/i/oaSzbYdmQM.jpg",
"http://localhost/img/i/dD5sZYltqV.jpg",
"http://localhost/img/i/RBLZbcN0Cd.jpg",
""
)
2013-08-16 07:04:29.367 img[12575:c07] (
"http://localhost/img/i/thumb/YzrLnuCFmR_thumb.jpg",
"http://localhost/img/i/thumb/43BXPwhEqg_thumb.jpg",
"http://localhost/img/i/thumb/qEuPrbSBfX_thumb.jpg",
"http://localhost/img/i/thumb/ZDs0q5aoSx_thumb.jpg",
"http://localhost/img/i/thumb/EbfO8a9gn5_thumb.jpg",
"http://localhost/img/i/thumb/oaSzbYdmQM_thumb.jpg",
"http://localhost/img/i/thumb/dD5sZYltqV_thumb.jpg",
"http://localhost/img/i/thumb/RBLZbcN0Cd_thumb.jpg",
""
)

知道该怎么做吗? :)

最佳答案

我建议您使用 SDWebImage Framework用于表格 View 中的延迟加载图像

SDWebImage 是一个异步图像下载器,具有 UIImageView 类别的缓存支持。

这个库为 UIImageVIew 提供了一个类别,支持来自服务器的远程图像。

示例代码

[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

您可以在此处提供一个占位符图像,它将一直显示到服务器图像准备好显示为止。

您面临的问题是因为您将图像加载代码保持在 if 条件下,这就是您遇到奇怪问题的原因。

if (cell == nil) { ... } if 条件仅在没有单元可重复使用时为真。你肯定想重复使用你的细胞。所以当你有一个有效的单元格时配置它们。

关于iphone - 自定义表格 View 中的图像未按正确顺序显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18265833/

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