之前我们实现了页面切换以及UITextField键盘隐藏及防止键盘遮挡
今天我们来实现一下简单的UITableView的插入删除移动
首先我们在.m文件中
#import <UIKit/UIKit.h>
#import "JBaseViewController.h"
@interface JThirdViewController : UITableViewController<UITableViewDataSource,UITableViewDataSource>
@property (strong, nonatomic) NSMutableArray *list;
@end
接下来我们在.h文件中实现
#import "JThirdViewController.h"
@interface JThirdViewController ()
@end
@implementation JThirdViewController
@synthesize list;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[[self navigationItem]setRightBarButtonItem:[self editButtonItem]];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"美国", @"菲律宾",@"黄岩岛", @"中国", @"泰国", @"越南", @"老挝",@"日本" , nil];
self.list = array;
self.navigationItem.title = @"列表";
self.tableView.editing = YES;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
- (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 list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]autorelease];
// cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
// cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
// cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]autorelease];
}
// Configure the cell...
NSUInteger row = [indexPath row];
cell.textLabel.text=[self.list objectAtIndex:row];
UIImage *image = [UIImage imageNamed:@"add_file.png"];
cell.imageView.image = image;
UIImage *highLighedImage = [UIImage imageNamed:@"add_list.png"];
cell.imageView.highlightedImage = highLighedImage;
cell.detailTextLabel.text =@"haha";
return cell;
}
#pragma mark - Table view delegate
//选中效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSString *rowString = [self.list objectAtIndex:[indexPath row]];
// UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
// [alter show];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// cell.accessoryType = UITableViewCellAccessoryDetailButton;
// cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//删除 插入
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.list removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:
UITableViewRowAnimationAutomatic
//UITableViewRowAnimationBottom
//UITableViewRowAnimationFade
//UITableViewRowAnimationLeft
//UITableViewRowAnimationMiddle
//UITableViewRowAnimationNone
//UITableViewRowAnimationRight
//UITableViewRowAnimationTop
];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath, nil];
[self.list insertObject:@"aaaa" atIndex:row];
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];
}
}
//移动
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// return UITableViewCellEditingStyleNone;
return UITableViewCellEditingStyleInsert;
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSUInteger fromRow = [sourceIndexPath row];
NSUInteger toRow = [destinationIndexPath row];
id object = [self.list objectAtIndex:fromRow];
[self.list removeObjectAtIndex:fromRow];
[self.list insertObject:object atIndex:toRow];
}
@end
具体的介绍可以参考我之前发过的
iOS学习之TableView的简单使用http://blog.csdn.net/zhangyankan/article/details/12776961
iOS学习之分段Table View的使用(Grouped样式表格)http://blog.csdn.net/zhangyankan/article/details/12776965
iOS学习之UITableView中Cell的操作http://blog.csdn.net/zhangyankan/article/details/12776975