偶尔在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