Ios 使用其他方法为cellForRowAtIndexPath创建数组
我有一个索引的Ios 使用其他方法为cellForRowAtIndexPath创建数组,ios,objective-c,arrays,uitableview,Ios,Objective C,Arrays,Uitableview,我有一个索引的tableView,显示用户iPod库中的歌曲列表。这就是我当前获取歌曲标题的方式,这会导致滚动速度非常慢 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { cell.textLabel.text= [self titleForRow:indexPath]; //getting cell content } …它调用以下
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;
}
但这似乎是非常错误的,我真的很困惑如何解决这个问题。我不知道如何用这些方法创建一个单独的数组,用歌曲标题填充newArray
,我在谷歌搜索了很长一段时间,找不到任何对我有帮助的东西
有人能告诉我正确的方向吗,或者请告诉我如何创建新数组。谢谢任何帮助都将不胜感激。这一切的中心是:
cell.textLabel.text = [self titleForRow:indexPath];
现在,您实际使用的是NSString
。简单。相反,您正在为每次调用的每个单元格创建一个新数组??
为什么不使用一个数组,比如NSMutableArray。将其作为@属性
或本地iVar
@property (strong, nonatomic) NSMutableArray *rowArray;
// Instantiate in -viewDidLoad
rowArray = [NSMutableArray array]; // Much more elegant IMHO
现在调用一个执行所有内容加载的方法,但只执行一次。您可以随时再次调用它以刷新
比如:
// In -viewDidLoad
rowArray = [NSMutableArray array];
[self loadArrayContent];
然后,您的
cellforrowatinexpath
应该只使用预加载的rowArray
并从中简单地提取NSString
标题。我处理表视图的一般方法是(在表视图设置期间)为行创建一个NSMutableArray(锚定在数据源对象中)。数组中的每个条目都是一个NSMutableDictionary,它用该行的基本内容“预处理”,如果需要开发更多信息来显示该行,则缓存在字典中。(如果有多个节,我使用包含行数组的节数组。)
对于播放列表,我可以使用空字典初始化数组,然后在滚动时访问每个播放列表元素并将其缓存在字典中
事实上,如果真的需要,可以通过后台任务(带有适当的锁定)来更新字典,这样滚动速度就不会受到查找操作的影响。表视图单元格以串行方式排队和出列。利用这一能力,您应该期望只有部分的变化频率较低。您可以使用静态变量来免除节的获取。看看我的代码
-(NSString *)titleForRow:(NSIndexPath *)indexpath{
static NSIndexPath *funIndexPath;
static NSMutableArray *funSectionArray;
if (!funIndexPath || funIdexpath.section != indexpath.section) {
funIndexPath = indexpath;
funSectionArray = [self getArrayOfRowsForSection:indexpath.section];
}
rowArray = funSectionArray;
NSString *titleToBeDisplayed = [rowArray objectAtIndex:indexpath.row];
return titleToBeDisplayed;
}
这是第一次可能的优化
当我查看您的获取结果数组时,有些事情并不清楚。为什么要迭代检查section==i
。您可以用代码中的i
替换section
-(NSMutableArray *)getNewArray:(NSInteger)section {
NSString *rowTitle;
NSString *sectionTitle;
sectionTitle = [self.alphabetArray objectAtIndex:section]; //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;
}
现在还不清楚你在这里想要实现什么
与我自己的答案相反,我建议您不要在视图渲染期间计算要显示的数据。您需要尽可能快地创建单元格。为什么不创建一个属性或iVar(如@bauerMusic所建议的)并在viewDidLoad
中计算日期,或者声明一个自定义函数来重新加载属性(可能是数组
或字典
),在呈现UI时,只需设置值(在viewForIndexPath
中)
可以使用两级深度的数组进行存储,以维护节和行。i、 e.第1级-节和行数组,第2级-行。能否添加数据源的外观(字母表数组)。您有很多代码,看起来它正在搜索您的数据源并创建一个新的数据源,所有这些都是为了创建一个单元格。清理一下会有很大帮助。谢谢你的回答,看看Hot Clicks的答案,我也许应该在@property NSMutableDictionary中添加这些信息。我该怎么做?这实际上是我最初的问题,但我确实不清楚(我道歉)。我试图在我的
getNewArray
方法中添加所有歌曲标题,但失败得很惨。如何使用这些方法将所有标题添加到@property NSMutableDictionary
,以保持索引它们在概念上存在一些差异,但需要注意的是,您仍然需要一个数组
,只有该数组在NSDictionary
中。有一个文本快捷方式@{@“key”:@“value”}
,否则,请使用setObject:forKey:
。例如:[myDictionary setObject:myArray forKey:@“歌曲列表”]代码>这里是完整的参考:谢谢你的回答,我最初的问题有点不清楚,因为我在我的OP中尝试在getNewArray
中向一个新数组添加所有标题,但它出了可怕的错误。如何使用这些方法将所有歌曲标题添加到@property NSMutableDictionary
:S@user3127576--创建NSMutableArray。使用大小合适的NSMutableDictionary(如上所述)或NSNull(纯简版本)对象填充它。在引用每行时更新数组。测试给定的数组元素是否已经更新很容易。谢谢你的帮助。在getNewArray
中,我试图用我所有的歌曲标题创建一个新数组,这样我只能加载一次,然后在cellForRow
中引用一个数组。很抱歉我不清楚。当我评论Hot Clicks和bauerMusic的答案时,我对如何使用这些方法创建歌曲标题并将其添加到@property NSMutableDictionary中感到困惑:s
-(NSMutableArray *)getNewArray:(NSInteger)section {
NSString *rowTitle;
NSString *sectionTitle;
sectionTitle = [self.alphabetArray objectAtIndex:section]; //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;
}