【发布时间】:2011-07-11 23:29:48
【问题描述】:
我使用 asihttprequest 下载文件,但是当下载失败时,与表格中的文件关联的单元格不会从表格视图中删除。
当我滑动删除时,我有这个错误:
* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[NSMutableArray objectAtIndex:]:索引 0 超出范围 对于空数组'
如何删除单元格?
MANAGER.h
NSMutableArray *contentArray;
MANAGER.m
- (id)initWithStyle:(UITableViewStyle)style {
if (self == [super initWithStyle:style]) {
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
contentArray = [[NSMutableArray alloc] init];
progArray = [[NSMutableArray alloc] init];
self.view.backgroundColor = [UIColor clearColor];
//saveTo = [[NSUserDefaults standardUserDefaults] stringForKey:@"save_to"];
AppDelegate_iPhone* delegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate];
if([delegate saveTo] != nil)
{
saveTo = [delegate saveTo];
}
if(saveTo == nil)
{
saveTo = @"/";
}
allFiles = [[NSMutableArray alloc] init];
NSDirectoryEnumerator *dirEnum = [ [ NSFileManager defaultManager ] enumeratorAtPath:saveTo];
NSString *file;
while ((file = [ dirEnum nextObject ]))
{
if(file != nil)
{
if([file rangeOfString:@".mp4" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".mov" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".m4v" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".pdf" options: NSCaseInsensitiveSearch].length > 0 )
{
[ allFiles addObject: file];
}
}
}
}
return self;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0)
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
Selected = indexPath.row;
//Remove The Temporary File
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:[(ASIHTTPRequest *)[contentArray objectAtIndex:indexPath.row] temporaryFileDownloadPath]])
{
......
- (void)requestFailed:(ASIHTTPRequest *)request
{
[contentArray removeObject:request];
[progArray removeObject:request];
[self reloadMyData];
[self.tableView reloadData];
}
-(void)reloadMyData
{
allFiles = [[NSMutableArray alloc] init];
AppDelegate_iPhone* delegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate];
if([delegate saveTo] != nil)
{
saveTo = [delegate saveTo];
}
NSDirectoryEnumerator *dirEnum = [ [ NSFileManager defaultManager ] enumeratorAtPath:saveTo];
NSString *file;
while ((file = [ dirEnum nextObject ]))
{
if(file != nil)
{
//if([file rangeOfString:@".mp4" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".mov" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".mp3" options: NSCaseInsensitiveSearch].length > 0 )
if([file rangeOfString:@".mp4" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".mov" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".m4v" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".pdf" options: NSCaseInsensitiveSearch].length > 0)
{
[ allFiles addObject: file];
}
}
}
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if(section == 0)
{
return [progArray count];
}
else {
return [allFiles count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:@"CellIdentifier_%i_%i",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if(indexPath.section == 0)
{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[cell addSubview:[progArray objectAtIndex:indexPath.row]];
}
else {
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.text = [allFiles objectAtIndex:indexPath.row];
}
return cell;
}
【问题讨论】:
-
听起来您的数据源缺少您要删除的对象
-
下载失败对象被移除 [contentArray removeObject:request];但不是单元格...然后当我滑动删除并删除单元格时出现错误,因为该对象已被删除..如何在下载失败后立即删除单元格?
-
下载失败时不移除对象,应用会崩溃吗?如果没有,当您执行滑动删除和删除单元格时,该对象是否正确删除?
-
不,当我不删除对象时,应用程序不会崩溃。然后滑动删除正常工作,并正确删除带有对象的单元格
-
很高兴我能帮上忙 - 我发布了一个解释发生了什么的答案,请接受它,这样我就可以得到帮助!
标签: ios xcode uitableview asihttprequest