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;
}

**サムネイル画像のコードの場所を変更すると正常に動作しますが、スクロールするとテーブルビューがフリーズすることに注意してください

これは両方のアレイのログです

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 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 条件は、再利用できるセルがない場合にのみ真になります。そして、細胞を再利用したいのは間違いありません。したがって、有効なセルがあるときにそれらを構成します。

于 2013-08-16T04:52:43.783 に答える