ios - tableView 滚动真的很慢——我调用了太多方法?

标签 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;
}

这些是方法:

- 获取歌曲名称

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

这里有什么办法可以平滑滚动吗?这些方法中的每一个都很重要。

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

最佳答案

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

关于ios - tableView 滚动真的很慢——我调用了太多方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24442373/

相关文章:

ios - UIScrollView 内的 UIWebView

ios - 将图像上传到服务器 Ios。我有 uiimageview 并想将 imageview 图像发送到服务器以实现 objective-c

iphone - 发布保留 View 的最佳实践?

objective-c - Yosemite Finder Sync 简单示例

iphone - 用于 UIImageView/UIView 时如何调整使用 resizableImageWithCapInsets 创建的 UIImage 的大小

iphone - 从 UITextField 添加一个对象到 NSMutableArray

iphone - Objective-c iPhone NSTimer 唯一标识符

objective-c - 使用图像和透明度自定义 UITabBar

ios - JSON解析UITableViewController

ios - 从 -drawRewct : using CoreGraphics 调用回调方法