0

我有一件UITebleView带服装的UITableViewCells。每次我刷新它们时,内容都会自行重新排序。有谁知道为什么?

我正在从 JSON 中获取数据,我不进行某种排序,我只是根据 TableViewCell indexpath.row 显示数据

这是我设置 UITableViewCell 内容的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *costumeCell = @"Cell";

    StoreCell *cell = [tableView dequeueReusableCellWithIdentifier:costumeCell];

    if (!cell) {
        cell = [[StoreCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:costumeCell];
    }

    NSDictionary *dict;
    dict = [application objectAtIndex:indexPath.row];

    [downloadQueue addOperationWithBlock:^{

        name = [dict objectForKey:@"name"];
        detileName = [dict objectForKey:@"detailName"];
        itmsLink = [dict objectForKey:@"itms-serviceLink"];
        icon = [dict objectForKey:@"icon"];
        developer = [dict objectForKey:@"developer"];
        version = [dict objectForKey:@"version"];
        category = [dict objectForKey:@"category"];
        rating = [dict objectForKey:@"rating"];
        ratingNumbers = [dict objectForKey:@"ratingNumber"];
        description = [dict objectForKey:@"description"];
        developerEmails = [dict objectForKey:@"developerEmail"];

        cell.AppName.text = name;
        cell.category.text = category;
        cell.rater.text = [NSString stringWithFormat:@"(%@)", ratingNumbers];
        if ([rating intValue] == 1) {
            cell.rating.image = [UIImage imageNamed:@"1.png"];
        }
        if ([rating intValue] == 2) {
            cell.rating.image = [UIImage imageNamed:@"2.png"];
        }
        if ([rating intValue] == 3) {
            cell.rating.image = [UIImage imageNamed:@"3.png"];
        }
        if ([rating intValue] == 4) {
            cell.rating.image = [UIImage imageNamed:@"4.png"];
        }
        cell.itms = itmsLink;
        [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:icon]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse* response, NSData* data, NSError* error){

            if(error)
            {
                // Error Downloading image data
                cell.AppIcon.image = [UIImage imageNamed:@"placeholder.png"];
            }
            else
            {
                [cell.AppIcon setImage:[UIImage imageWithData:data]];

            }
        }];
        cell.AppIcon.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:icon]]];
        cell.number.text = [NSString stringWithFormat:@"%li", (long)indexPath.row + 1];
    }];

    cell.AppIcon.layer.masksToBounds = YES;
    cell.AppIcon.layer.cornerRadius = 16.0;
    cell.installButton.layer.masksToBounds = YES;
    cell.installButton.layer.cornerRadius = 5.0f;
    cell.installButton.layer.borderColor = [UIColor darkGrayColor].CGColor;
    cell.installButton.layer.borderWidth = 1.0f;



    return cell;
}
4

1 回答 1

0

由于您是从 JSON 中获取的,因此您从中获取它的服务器可能不会为您对数据进行排序。当然,如果您不了解服务器的 API,我们不可能知道它在做什么。

在不提供更多信息的情况下,您可以做的就是在从服务器接收到新数据后进行排序:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name"  ascending:YES];
NSArray *sortedApplicationArray = [application sortedArrayUsingDescriptors:@[descriptor]];
application = sortedApplicationArray;

只是不要将该代码放在您的 cellForRowAtIndexPath 中,因为每次创建一行时它都会进行排序!

我想我知道问题是什么。

您的 cellForRowAtIndexPath 中有一个异步操作。您应该直接使用 dict 对象并在操作队列之外设置单元格 UI 元素。

唯一看起来需要异步完成的是图像下载。我建议您使用来自 github 的 SDWebImage,它在自己的队列中处理下载并将图像缓存在内存和磁盘上。

于 2013-10-19T21:58:24.120 回答