我有一个应用程序,在我执行数组后从数据库中加载图像数据我从图像数组生成缩略图图像的新数组,当应用程序加载时,我使用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);
}
在表格视图中加载所有内容:
- (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",
""
)
知道该怎么办? :)
答案 0 :(得分:1)
我建议您在表格视图中使用SDWebImage Framework 延迟加载图片。
SDWebImage是一个异步图像下载程序,具有UIImageView类别的缓存支持。
此库为UIImageVIew提供了一个类别,支持来自服务器的远程图像。
示例代码
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
在这里,您可以提供placeholder image
,在服务器映像准备好显示之前,它将一直显示。
您遇到的问题是因为您将图像加载代码保持在if
状态,这就是您遇到奇怪问题的原因。
if (cell == nil) { ... }
如果没有可以重复使用的单元格,那么if条件只有为真。你肯定想重用你的细胞。所以当你有一个有效的单元格时配置它们。