2014-06-27 68 views
0

我有一個tableView,它顯示用戶的iPod庫中的歌曲列表。每個單元調用方法太多這可能是爲什麼滾動如此低迷(我覺得「調用方法」是正確的術語,我是相當新的編程),像這樣:tableView滾動真的很慢 - 我打電話太多的方法?

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

    // Configure the cell... 

    cell.textLabel.text= [self titleForRow:indexPath]; //getting song title 
    cell.detailTextLabel.text = [self subtitleForRow:indexPath]; //get song artist 
    cell.imageView.image = [self artworkForRow:indexPath]; //get song image 

    return cell; 
} 

這些都是方法:

- 獲取歌名

-(NSString *)titleForRow:(NSIndexPath *)indexpath{ 

    NSMutableArray* rowArray=[[NSMutableArray alloc]initWithCapacity:0]; 
    rowArray=[self getArrayOfRowsForSection:indexpath.section]; 
    NSString *titleToBeDisplayed=[rowArray objectAtIndex:indexpath.row]; 
    return titleToBeDisplayed; 

} 

-(NSMutableArray *)getArrayOfRowsForSection:(NSInteger)section 
{ 
    NSString *rowTitle; 
    NSString *sectionTitle; 
    NSMutableArray *rowContainer=[[NSMutableArray alloc]initWithCapacity:0]; 

    for (int i=0; i<self.alphabetArray.count; i++) 
    { 
     if (section==i) // check for right section 
     { 
      sectionTitle= [self.alphabetArray objectAtIndex:i]; //getting section title 
      for (MPMediaItem *song in songs) 
      { 
       NSString *title = [song valueForProperty:MPMediaItemPropertyTitle]; 
       rowTitle= [title substringToIndex:1]; //modifying the statement to its first alphabet 
       if ([rowTitle isEqualToString:sectionTitle]) //checking if modified statement is same as section title 
       { 
        [rowContainer addObject:title]; //adding the row contents of a particular section in array 
       } 
      } 
     } 
    } 
    return rowContainer; 
} 

- 獲取藝術家

-(NSString *)subtitleForRow:(NSIndexPath *)indexpath{ 

    NSMutableArray* subtitleRowArray=[[NSMutableArray alloc]initWithCapacity:0]; 
    subtitleRowArray=[self getSubtitle:indexpath.section]; 

    NSString *subtitleToBeDisplayed=[subtitleRowArray objectAtIndex:indexpath.row]; 
    return subtitleToBeDisplayed; 

} 

-(NSMutableArray *)getSubtitle:(NSInteger)section 
{ 
    NSString *rowTitle; 
    NSString *sectionTitle; 
    NSMutableArray *rowContainer=[[NSMutableArray alloc]initWithCapacity:0]; 

    for (int i=0; i<self.alphabetArray.count; i++) 
    { 
     if (section==i) // check for right section 
     { 
      sectionTitle= [self.alphabetArray objectAtIndex:i]; //getting section title 
      for (MPMediaItem *song in songs) 
      { 
       NSString *title = [song valueForProperty:MPMediaItemPropertyTitle]; 
       NSString *subtitle = [song valueForProperty:MPMediaItemPropertyArtist]; 
       rowTitle= [title substringToIndex:1]; //modifying the statement to its first alphabet 
       if ([rowTitle isEqualToString:sectionTitle]) //checking if modified statement is same as section title 
       { 
        if (subtitle){ 
         [rowContainer addObject:subtitle]; //adding the row contents of a particular section in array 
        } 
        else { 
         [rowContainer addObject:@"Unknown Artist"]; 
        } 
       } 
      } 
     } 
    } 
    return rowContainer; 
} 

- 獲取圖像

-(UIImage *)artworkForRow:(NSIndexPath *)indexpath{ 

    NSMutableArray* artworkRowArray=[[NSMutableArray alloc]initWithCapacity:0]; 
    artworkRowArray=[self getArtwork:indexpath.section]; 

    UIImage *artworkToBeDisplayed=[artworkRowArray objectAtIndex:indexpath.row]; 

    return artworkToBeDisplayed; 
} 

-(NSMutableArray *)getArtwork:(NSInteger)section 
{ 
    NSString *rowTitle; 
    NSString *sectionTitle; 
    NSMutableArray *rowContainer=[[NSMutableArray alloc]initWithCapacity:0]; 

    for (int i=0; i<self.alphabetArray.count; i++) 
    { 
     if (section==i) // check for right section 
     { 
      sectionTitle= [self.alphabetArray objectAtIndex:i]; //getting section title 
      for (MPMediaItem *song in songs) 
      { 
       NSString *title = [song valueForProperty:MPMediaItemPropertyTitle]; 
       MPMediaItemArtwork *artwork = [song valueForProperty:MPMediaItemPropertyArtwork]; 

       UIImage *artworkImage = [artwork imageWithSize: CGSizeMake (50, 50)]; 

       rowTitle= [title substringToIndex:1]; //modifying the statement to its first alphabet 
       if ([rowTitle isEqualToString:sectionTitle]) //checking if modified statement is same as section title 
       { 
        if (artworkImage){ 
         [rowContainer addObject:artworkImage]; //adding the row contents of a particular section in array 
        } 
        else { 
         [rowContainer addObject:[UIImage imageNamed:@"noArtworkSongsCell"]]; 
        } 
       } 
      } 
     } 
    } 
    return rowContainer; 
} 

有什麼辦法來平滑滾動這裏?每種方法都很重要。

任何幫助將不勝感激,謝謝!

+0

使用Time Profiler工具來確定您的CPU工作正在進行和優化的位置。這是最好的方式來調試 – jakenberg

回答

4

您使用的方法不應在cellForRowAtIndexPath中調用 - 您不應在每次需要在單元中填充標籤時創建這些數組。數組應該在cellForRowAtIndexPath之外創建一次,然後在該方法內部查詢以從數組中獲取正確的項目。

+0

我想我明白你在暗示什麼 - 在cellForRowAtIndexPath方法之外創建這些數組。然後參考這個方法中的數組,而現在我爲每個單元格創建這些數組,而不是僅爲整個列表中的一個。是對的嗎?我不完全確定如何做到這一點:S – user3127576

+0

@ user3127576,是的,這就是我的意思。 – rdelmar

0

在繪製表格本身之前,請始終準備好您的數據。在諸如cellForRow,heightForCellAtIndexPath等方法中,您應該只是訪問您的數據,而不是修改和/或操作它。在你的例子中,你不僅遍歷每個單元格的數組,你甚至可以調用非常慢的方法,比如字符串比較。您可以在cellForRowAtIndex路徑中放置一個斷點,並查看在滾動過程中調用了多少次,然後想象您需要完成多少工作。