【问题标题】:tableView Scrolling Really Slow - I'm Calling Too Many Methods?tableView 滚动真的很慢 - 我调用了太多方法?
【发布时间】:2014-06-27 00:38:26
【问题描述】:

我有一个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;
}

这些是方法:

- 获取歌曲标题

-(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 工作的去向和优化。这是调试此问题的最佳方法

标签: ios iphone objective-c xcode uitableview


【解决方案1】:

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

【讨论】:

  • 我想我理解你的建议 - 在 cellForRowAtIndexPath 方法之外创建这些数组。然后在此方法中引用这些数组,而现在我正在为每个单元格创建这些数组,而不仅仅是为整个列表创建一个。是对的吗?我不完全确定如何做到这一点:S
  • @user3127576,是的,我就是这个意思。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    相关资源
    最近更新 更多