如何从表格中删除单元格 [英] How to remove a cell from table

查看:81
本文介绍了如何从表格中删除单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asihttprequest下载文件,但下载失败时,与表中文件相关联的单元格不会从表视图中删除.

I use asihttprequest to download a file but when the download fails, the cell associated with the file in the table doesn't remove from the tableview.

滑动以删除时,出现此错误:

When I swipe to delete, I have this error :

*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* -[NSMutableArray objectAtIndex:]:索引0超出范围 用于空数组"

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'

如何移除电池?

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;
}

推荐答案

从tableView删除行时,需要在删除行的同时从数据源中删除对象.如果从数据源中删除一行,则需要在删除时更新表(-deleteRowsAtIndexPaths:withRowAnimation :),以维护tableView和数据源之间的关系.

When deleting a row from a tableView, you need to remove the object from the datasource at the same time you delete the row. If you delete a row from the datasource, you need to update the table (-deleteRowsAtIndexPaths:withRowAnimation:) at the time of deletion to maintain the relationship between the tableView and the datasource.

这篇关于如何从表格中删除单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆