【发布时间】:2014-06-29 01:34:13
【问题描述】:
我有一个已编入索引的tableView,它显示了用户 iPod 库中的歌曲列表。这是我目前获取歌曲标题的方式,这导致滚动非常慢:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.text= [self titleForRow:indexPath]; //getting cell content
}
...调用这些方法:
-(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;
}
问题是:对于每个cellForRowAtIndexPath,我正在调用这些方法并为每个单元格创建这些数组。所以我需要创建一个单独的数组并简单地从该数组中调用objectAtIndex。
这是我迄今为止尝试过的:创建了一个NSMutableArray *newArray,以及以下方法:
-(NSMutableArray *)getNewArray:(NSInteger)section
{
NSString *rowTitle;
NSString *sectionTitle;
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
{
[newArray addObject:title]; //adding the row contents of a particular section in array
}
}
}
}
return newArray;
}
但这似乎非常错误,我真的很困惑如何解决这个问题。我不知道如何用这些方法创建一个单独的数组,用歌曲标题填充newArray,我已经在谷歌搜索了很长一段时间,但找不到任何对我有帮助的东西。
谁能指出我正确的方向,或者请告诉我如何创建 newArray? I've attached my tableView data source here。谢谢。任何帮助将不胜感激。
【问题讨论】:
-
你能添加你的数据源的样子吗(你的字母数组)。您有很多代码看起来像是在搜索您的数据源并创建一个新的数据源,所有这些都是为了创建一个单元格。清理它会有很大帮助。
标签: ios objective-c arrays uitableview