Ios DidSelectRowatineXpath中的NSIndexPath和objectAtIndex崩溃?
我正在做一个小的iOS项目,带有UITableView 我做了一张桌子,里面放了一些东西:Ios DidSelectRowatineXpath中的NSIndexPath和objectAtIndex崩溃?,ios,uitableview,nsindexpath,Ios,Uitableview,Nsindexpath,我正在做一个小的iOS项目,带有UITableView 我做了一张桌子,里面放了一些东西: -(void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath { NSString *fileName = [testList objectAtIndex:[indexPath row]]; NSString *docsDir = [NSSearchPathForDirectoriesI
-(void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
NSString *fileName = [testList objectAtIndex:[indexPath row]];
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *testPath = [docsDir stringByAppendingPathComponent:fileName];
NSMutableDictionary *plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:testPath];
[[cell textLabel] setText:[plistDict valueForKey:@"title"]];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"indentifier"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
[cell autorelease];
}
[self configureCell:cell forIndexPath:indexPath];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:docsDir error:nil];
testList = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.stest'"]];
return [testList count];
}
这个很好用。我有一张表,里面有一些虚拟内容。但当我添加此代码时:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[testTable deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"%d",[indexPath row]);
NSLog(@"%@",[testList objectAtIndex:[indexPath row]]);
}
当我按下表中的一个单元格时,iOS模拟器崩溃(它没有退出,但应用程序不再响应)。原因是下一行:
NSLog(@"%@",[testList objectAtIndex:[indexPath row]]);
当我删除这一行,它的作品完美。此日志:
NSLog(@"%d",[indexPath row]);
返回正常情况下的行号
奇怪的是,我在configureCell函数中做了完全相同的操作:
NSString *fileName = [testList objectAtIndex:[indexPath row]];
但这很好
这里出了什么问题?您需要保留
testList
。表视图中的以下行:numberofrowsin节:
不保留它:
testList = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.stest'"]];
filteredArrayUsingPredicate
返回您不拥有的对象(根据对象所有权策略)。由于您直接访问ivartestList
,因此需要通过向对象发送一条retain消息(并在将来某个时候释放)来声明对象的所有权
请注意,testList=…
和self.testList=…
是不同的。前者直接访问ivar,而后者通过属性testList
(如果有)的访问器访问。因此,如果您有一个testList
retain属性,它就这么简单:
self.testList = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.stest'"]];
如果您不具有testList
retain属性,则可以如下方式保留对象:
testList = [[dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.stest'"]] retain];
我鼓励您使用属性,因为它们封装了内存管理代码,从而减少了样板代码。您是个天才。保留方法有效。感谢您提供有关物品所有权的手册。欢迎光临!记住在你不再需要它时释放它,否则它会泄漏:)另外,每次刷新单元格时从文件加载是不可取的,我怀疑你的滚动会受到影响-----也就是说,在你让它工作之前不需要优化