2013-10-19 73 views
0

我有一個UITebleView與服裝UITableViewCells。每次刷新它們時,內容都會重新排序。有誰知道爲什麼?爲什麼我的UITabeViewContent每次刷新時都會重新排序?

我從一個JSON fatching的數據,我不這樣做某種排序的,我只是顯示數據acording到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; 
} 

回答

0

由於您從JSON中獲取數據,因此您從中獲取的服務器可能無法爲您分類數據。當然,如果沒有你泄露到服務器的API中,我們不可能知道它在做什麼。

你可以做什麼,沒有提供進一步的信息從服務器接收新的數據後,只是做一個排序:

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

只是不要把這些代碼你的cellForRowAtIndexPath裏面,因爲這一切會做每次創建行時都進行排序!

我想我知道問題是什麼。

您在cellForRowAtIndexPath中有異步操作。你應該直接用字典對象和操作隊列設置單元UI元素。

看起來像它需要異步完成的唯一的事情是圖像下載。我建議你使用github中的SDWebImage來處理下載,並在內存和磁盤上緩存圖像。

+0

好的,我在問題中增加了一些說明 –

+0

爲什麼要排列數組? –

+0

確保數據有序。如果你打印出你的JSON結果,你可能會發現它的順序與tableView中表示的順序相同。 –

相關問題