UITableView 滑动删除数据

偶尔在cocosa网站看到大神写的帖子 转载过来 留着以后用 虽然代码不复杂 但是在协议的用法还是很模糊啊


#import <UIKit/UIKit.h>

@interface UITableCellSwapDeleteViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{
    UITableView *testTableView;
    NSMutableArray *dataArray;
}
@property (nonatomic, retain) UITableView *testTableView;
@property (nonatomic, retain) NSMutableArray *dataArray;
@end



//
//  UITableCellSwapDeleteViewController.m
//  UITableCellDelete
//
//  Created by Mobile Dev on 12-7-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UITableCellSwapDeleteViewController.h"

@interface UITableCellSwapDeleteViewController ()

@end

@implementation UITableCellSwapDeleteViewController
@synthesize testTableView;
@synthesize dataArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"did");
    self.view.backgroundColor=[UIColor redColor];
    testTableView=[[UITableView alloc]init];
    testTableView.frame=CGRectMake(0,0, 320,480);
    [testTableView setDelegate:self];
    [testTableView setDataSource:self];
    testTableView.backgroundColor=[UIColor clearColor];
//    testTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
    [self.view addSubview:testTableView];
   
    // Do any additional setup after loading the view.
}
//第二步,展示数据.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    // Return the number of sections.
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    // Return the number of rows in the section.
    
    return [dataArray count];
    
}

// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {   
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
    }  
    
    // Configure the cell...
    
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    
    return cell;
    
}
//删除数据
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [dataArray removeObjectAtIndex:indexPath.row];
        
        // Delete the row from the data source.
       //删除数据 第二个参数是动画
        [testTableView 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.
        
    }    
    
}
//改变下载的按钮名字 现在为下载  默认为delete
//- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    return @"下载";
//    
//}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值