2014-03-03 184 views
0

我正在開發和iOS應用程序,我的UITableView有點慢。我想知道滾動緩慢的所有可能原因。 請注意,我在故事板中使用原型單元格,並且沒有任何圖像。TableView慢速滾動

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *identifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (cell == nil) 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 

    Classification *classification = [classifications objectAtIndex:indexPath.row]; 
    UILabel *teamLabel = (UILabel *) [cell viewWithTag:101]; 
    [teamLabel setText:[classification team]]; 
    [cell setBackgroundColor:[UIColor clearColor]]; 
    return cell; 

} 

此外,它在模擬器中運行平穩。 我使用的是iPad 2的

+0

如果你可以發佈你的tableView實現:cellForRowAtIndexPath:方法...那將是偉大的 –

+0

是來自web服務的數據? –

+0

編輯。是的,它來自web服務。我將數據解析到自定義對象中,然後將它們保存到數組中。 tableview從該數組讀取。 –

回答

0

異步下載數據,您:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

使用2個獨立的UITableViewCell對象,一個以填補文本數據,一個用於它需要在線程這樣的圖像作爲dispatch_async,從我的項目例如:

if (imageUrl.length > 0) 
     { 
      NSString *completeURL = @""; 
      completeURL = [completeURL stringByAppendingString:kPROFILE_IMAGE_URL]; 
      completeURL = [completeURL stringByAppendingString:@"/2/"]; 
      completeURL = [completeURL stringByAppendingString:imageUrl]; 
      completeURL = [completeURL stringByAppendingString:@".png"]; 

      NSString *fileName = [NSString stringWithFormat:@"%@.png", imageUrl]; 
      NSString* path = GetMediaFolder(); 
      BOOL imageExists = [[NSFileManager defaultManager] fileExistsAtPath:[path stringByAppendingPathComponent:fileName]]; 

      if (imageExists) 
      { 
       // NSLog(@"Image exists"); 
       // Update UI 

       dispatch_async(dispatch_get_main_queue(),^
       { 
        UITableViewCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath]; 

        UIImageView *imageView = (UIImageView*)[updateCell viewWithTag:kTAG_IMAGE]; 
        imageView.image=[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:fileName]]; 
        imageView.layer.cornerRadius = 23.0; 
        imageView.layer.borderColor = (__bridge CGColorRef)([UIColor clearColor]); 
        imageView.layer.borderWidth = 1.0; 
        imageView.clipsToBounds = YES; 
        imageView.contentMode = UIViewContentModeScaleAspectFill; 

        [updateCell setNeedsLayout]; 
       }); 
      } 
      else 
      { 
       // NSLog(@"Image not exists"); 
       // Download The Image 

       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^
       { 
        NSURL *imageURL=[NSURL URLWithString:completeURL]; 
        NSData *image=[NSData dataWithContentsOfURL:imageURL]; 
        [image writeToFile:[path stringByAppendingPathComponent:fileName] atomically:YES]; 

        // Update UI 
        dispatch_async(dispatch_get_main_queue(),^
        { 
         UITableViewCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath]; 

         UIImageView *imageView = (UIImageView*)[updateCell viewWithTag:kTAG_IMAGE]; 
         imageView.image=[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:fileName]]; 
         imageView.layer.cornerRadius = 23.0; 
         imageView.layer.borderColor = (__bridge CGColorRef)([UIColor clearColor]); 
         imageView.layer.borderWidth = 1.0; 
         imageView.clipsToBounds = YES; 
         imageView.contentMode = UIViewContentModeScaleAspectFill; 

         [updateCell setNeedsLayout]; 
        }); 
       }); 
      } 
     } 
    } 
    return cell; 

編輯1:

我看到您不使用圖像,您仍需要異步下載所有數據,並在下載完成時(而不是我的圖像下載,臨時數據和下載的數據)將其填入新的UITableViewCell

+0

我不認爲這適用於我的案例...我只從web服務下載數據,然後自定義對象留在內存中。 –

+0

沒關係,如果你在你的UITableView中使用下載的數據,那麼這就是你需要的。不要先下載數據,等待它,然後填寫它。 –