tableView滚动速度很慢 - 我调用的方法太多了?

时间:2014-06-27 00:38:26

标签: ios iphone objective-c xcode uitableview

我有一个tableView,显示来自用户iPod库的歌曲列表。每个单元调用太多方法,这可能是为什么滚动如此迟缓(我认为'调用方法'是正确的术语,我对编程来说相当新),就像这样:

- (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;
}

这些是方法:

- GET SONG TITLE

-(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;
}

有没有办法让滚动滚动到这里?每种方法都很重要。

非常感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:4)

您不应该在cellForRowAtIndexPath中调用您使用的方法 - 每次需要在单元格中填充标签时,您都不应该创建这些数组。应该在cellForRowAtIndexPath之外创建一次数组,然后在该方法中查询以从数组中获取正确的项。

答案 1 :(得分:0)

在绘制表格之前,请始终准备好数据。在诸如cellForRow,heightForCellAtIndexPath等方法中,您应该只访问您的数据,而不是修改它和/或操纵它。在您的示例中,您不仅要为每个单元格迭代数组,甚至还要调用极慢的方法,例如字符串比较。你可以在cellForRowAtIndex路径中放一个断点,看看它在滚动过程中调用了多少次,然后想象一下你想做多少工作。