(三)UITabBar and UINavigationController基础教程之UITableView的插入删除移动
2013-10-18 09:42
513 查看
之前我们实现了页面切换以及UITextField键盘隐藏及防止键盘遮挡
(一)UITabBar and UINavigationController基础教程之切换页面http://blog.csdn.net/zhangyankan/article/details/12833619
(二)UITabBar and UINavigationController基础教程之UITextField键盘隐藏及防止键盘遮挡http://blog.csdn.net/zhangyankan/article/details/12842205
今天我们来实现一下简单的UITableView的插入删除移动
首先我们在.m文件中
接下来我们在.h文件中实现
具体的介绍可以参考我之前发过的
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
(一)UITabBar and UINavigationController基础教程之切换页面http://blog.csdn.net/zhangyankan/article/details/12833619
(二)UITabBar and UINavigationController基础教程之UITextField键盘隐藏及防止键盘遮挡http://blog.csdn.net/zhangyankan/article/details/12842205
今天我们来实现一下简单的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
相关文章推荐
- (二)UITabBar and UINavigationController基础教程之UITextField键盘隐藏及防止键盘遮挡
- (一)UITabBar and UINavigationController基础教程之切换页面
- ios开发中对UITableView的操作(包括删除,插入,移动位置)
- [iOS]UINavigationController UITabBarController UITableView学习笔记
- iOS_UITableView 编辑(cell的插入, 删除, 移动)
- [iOS]UINavigationController UITabBarController UITableView学习笔记
- iOS UItableView+UINavigationController实现单元格的删除
- iOS UItableView+UINavigationController实现单元格的删除
- UITableView 编辑状态(删除、添加、移动)
- UI基础第十四弹:接使用UITableView Controller
- UITableView的编辑操作(移动&添加&删除-单选删除,或多选删除)
- tableView编辑:插入、删除和移动
- UITableView的删除,移动及修改删除按钮上的文字
- UITableView的移动、删除操作
- UINavigationController and viewWillAppear / viewDidAppear
- UITableView实现删除移动编辑的样式(转)
- UItableView中编辑,删除,移动学习笔记
- UITableView (4): 在TableView中移动cell和Section 从TableView中删除cell和section 添加系统默认刷新控件
- UITableView 移动与插入,
- 【精】表格(UITableView)总结(4):编辑(增加、删除、移动)