1

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

这是我的代码

用于对数据进行排序的 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",
    ""
)

知道该怎么做吗?:)

4

1 回答 1

1

我建议您使用SDWebImage 框架在表格视图中延迟加载图像。

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 条件才为真。你肯定想重复使用你的细胞。所以当你有一个有效的单元格时配置它们。

于 2013-08-16T04:52:43.783 回答