gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:01:37 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com