Ios tableView滚动速度非常慢-I';我调用的方法太多了?
我有一个Ios tableView滚动速度非常慢-I';我调用的方法太多了?,ios,iphone,objective-c,xcode,uitableview,Ios,Iphone,Objective C,Xcode,Uitableview,我有一个tableView,显示用户iPod库中的歌曲列表。每个单元格调用太多的方法,这可能就是为什么滚动如此缓慢的原因(我认为“调用方法”是正确的术语,我对编程相当陌生),比如: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Configure the cell... cell.textLabel.text
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;
}
-(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;
}
-(NSString*)titleForRow:(nsindepath*)indepath{
NSMutableArray*rowArray=[[NSMutableArray alloc]initWithCapacity:0];
rowArray=[self-GetArrayFrowsForSection:indexpath.section];
NSString*TitletoBedisplated=[rowArray objectAtIndex:indexpath.row];
返回显示的标题框;
}
-(NSMutableArray*)GetArrayFrowsForSection:(NSInteger)节
{
NSString*行标题;
NSString*节标题;
NSMutableArray*rowContainer=[[NSMutableArray alloc]initWithCapacity:0];
对于(int i=0;i您使用的方法不应该在CellForRowatineXpath中调用——您不应该每次需要在单元格中填充标签时都创建这些数组。数组应该在CellForRowatineXpath外部创建一次,然后在该方法内部查询,以从数组中获取正确的项。您使用的方法不应在CellForRowatineXpath中调用'you using'。您不应在每次需要在单元格中填充标签时都创建这些数组。数组应在CellForRowatineXpath外部创建一次,然后在该方法内部查询,以从数组中获取正确的项。在绘制标签之前始终准备好数据表本身。在cellForRow、heightForCellAtIndexPath等方法中,您应该只访问数据,而不是修改和/或操作数据。在您的示例中,您不仅对每个单元格的数组进行迭代,还可以调用非常慢的方法,例如字符串比较。您可以在cellForRowAtIndex路径中设置断点,并查看次数它在滚动过程中被调用,然后想象你要做多少工作。在绘制表格之前,一定要准备好数据。在cellForRow、heightForCellAtIndexPath等方法中,你应该只访问数据,而不是修改和/或操纵数据。在你的示例中,你不仅要对每个单元格的数组进行迭代你甚至可以调用非常慢的方法,比如字符串比较。你可以在CellForRowatinex路径中设置一个断点,看看在滚动过程中它被调用了多少次,然后想象你要做多少工作。使用时间分析器工具来确定你的CPU的工作方向并进行优化。这是最好的方法调试此方法使用时间分析器工具确定CPU的工作方向并进行优化。这是调试此方法的最佳方法。我想我理解您的建议-在CellForRowatineXpath方法之外创建这些数组。然后参考此方法中的数组,而现在我正在创建这些数组每个单元格都有一个,而不是整个列表都有一个。对吗?我不完全确定实际如何做到这一点:S@user3127576,是的,这就是我的意思。我想我理解你的建议-在CellForRowatineXpath方法之外创建这些数组。然后在这个方法中引用这些数组,而现在我正在创建这些数组每个单元格都有一个数组,而不是整个列表都有一个数组。是这样吗?我不完全确定实际如何做到这一点:S@user3127576是的,我就是这个意思。
-(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;
}