iOS 开发 UItableView中滑动删除 cell

主要是就是两个函数
一:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
相应editingStyle事件处理
在这里主要相应
UITableViewCellEditingStyleDelete:

二:
(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

在这个函数中设置那个cell设置成 什么样的editingStyle删除,插入,无
UITableViewCellEditingStyleDelete
UITableViewCellEditingStyleInsert
UITableViewCellEditingStyleNone

需要注意的问题:在删除对应cell中的内容时,也要删除这个cell,否则运行时显示效果就像没有刷新 一样,多出一个cell。而使用
[tableView reloadData];[self loadView];刷新都不管用。必须使用deleteRowsAtIndexPaths:withRowAnimation:函数删除这个cell

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

[tableView reloadData];据说reloadData只是从新加载表项内容,而不会重新设置表内cection中cell的个数。



#pragma mark Table view methods



- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"accessoryTypeForRowWithIndexPath");
    if0 == indexPath.section ){
        ifindexPath.row == [m_URLArray count]){
            return UITableViewCellAccessoryDetailDisclosureButton;
        }
        GFdataURL *currentURl ;
        currentURl = [[[GFdataURL alloc] init] autorelease];
        currentURl = [m_URLArray objectAtIndex: indexPath.row];
        //[currentURl release];
        //int i = [indexPath row];

        if([currentURl.m_strSubscribe isEqualToString:@"y"]){
            return UITableViewCellAccessoryCheckmark;
        }else{
            return UITableViewCellAccessoryNone;
        }
    }else{
        return UITableViewCellAccessoryDetailDisclosureButton;
    }
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
}


- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"accessoryButtonTappedForRowWithIndexPath");
    //NSInteger sectionSelect = [indexPath section];
    //NSInteger rowSelect = [indexPath row];
    //NSLog(@"选中了%d--%d",sectionSelect,rowSelect);

    switch(indexPath.section){
        case 0:
            //资讯设置
            ifindexPath.row == [m_URLArray count]){
                m_addNewsURL = [[addNewsURL alloc] initWithNibName:@"addNewsURL" bundle:nil];
                [self.navigationController pushViewController:m_addNewsURL animated:YES];
            }
            break;
        case 1:
            //货币设置
            switch(indexPath.row){
                case 0:
                    //跳转到默认类型 设置页面
                    m_defaltType = [[currencySettingForDefaultTypeViewController alloc] initWithNibName:@"currencySettingForDefaultType" bundle:nil];
                    m_defaltType.isDefaultType = YES;
                    [self.navigationController pushViewController:m_defaltType animated:YES];
                    break;
                case 1:
                    //跳转到兑换类型 设置页面
                    m_defaltType = [[currencySettingForDefaultTypeViewController alloc] initWithNibName:@"currencySettingForDefaultType" bundle:nil];
                    m_defaltType.isDefaultType = NO;
                    [self.navigationController pushViewController:m_defaltType animated:YES];
                    break;
        
                default :
                    ;
            }
           
            break;
        default :
            ;
    }
   
   
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


//for section 0 中的滑动删除效果

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if( (0 == indexPath.section) && (indexPath.row != [m_URLArray count]) ){
       return UITableViewCellEditingStyleDelete;
    }else{
        return UITableViewCellEditingStyleNone;
    }   
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/



// Override to support editing the table view.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //NSLog(@"commitEditingStyle");
    if (editingStyle == UITableViewCellEditingStyleDelete{
      
        GFdataURL *currentURl ;
        currentURl = [[GFdataURL alloc] init];
        //currentURl = [m_URLArray objectAtIndex: indexPath.row];
        currentURl = [[m_URLArray objectAtIndex:indexPath.row] retain];
       
        GFDBC *mydatabase;
        mydatabase =[[GFDBC alloc] init];
        [mydatabase deleteURL:currentURl.m_strURL];
        [currentURl release];
        currentURl = nil;
        if(m_URLArray !=nil){
            [m_URLArray release];
            m_URLArray =[[NSMutableArray alloc] init];
        }
        m_URLArray = [[mydatabase getAllURL] retain];
        //
        [mydatabase release];
        //[m_tableView deleteRowsAtIndexPaths:indexPath.row withRowAnimation:UITableViewRowAnimationFade];
        //[tableView reloadData];
        //[self loadView];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert{
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    } 
   
}

原文地址:https://www.cnblogs.com/hopeanCom/p/2777046.html