我有一個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;
}
有什麼辦法來平滑滾動這裏?每種方法都很重要。
任何幫助將不勝感激,謝謝!
使用Time Profiler工具來確定您的CPU工作正在進行和優化的位置。這是最好的方式來調試 – jakenberg