您的位置:首页 > 其它

118删除单元格(扩展知识:添加单元格和移动单元格的位置)

2015-06-16 11:55 267 查看
效果如下:





[b]ViewController.h[/b]

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController
@property (strong, nonatomic) NSMutableArray *mArrDataSource;

@end


ViewController.m

#import "ViewController.h"

@interface ViewController ()
- (void)layoutUI;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self layoutUI];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

//设置表格是否处于编辑模式;默认为NO
[self.tableView setEditing:YES animated:YES];
}

- (void)layoutUI {
_mArrDataSource = [[NSMutableArray alloc] initWithArray:
@[@"A", @"B", @"C", @"D", @"E", @"F", @"G",
@"H", @"I", @"J", @"K", @"L", @"M", @"N",
@"O", @"P", @"Q", @"R", @"S", @"T", @"U",
@"V", @"W", @"X", @"Y", @"Z", @"添加单元格"]];

self.navigationItem.prompt = @"TableView里面,单元格也被称为Row";
self.navigationItem.title = @"单元格操作(删除、添加、移动)";
}

#pragma mark - TableView
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @"数据列表";
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_mArrDataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = _mArrDataSource[indexPath.row];
return cell;
}

#pragma mark - TableView, insert or delete row
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
//编辑模式下,设置最后的Row为插入模式
UITableViewCellEditingStyle style = tableView.editing && indexPath.row == (_mArrDataSource.count - 1) ? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleDelete;
return style;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
switch (editingStyle) {
case UITableViewCellEditingStyleNone: {
break;
}
case UITableViewCellEditingStyleDelete: {
//删除数据源数据
[_mArrDataSource removeObjectAtIndex:indexPath.row];
//删除单元格;注意必须保证操作之前已删除对应的数据源数据,否则报错
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
case UITableViewCellEditingStyleInsert: {
//添加数据源数据
[_mArrDataSource insertObject:@"我是添加的单元格" atIndex:(_mArrDataSource.count - 1)];
//添加单元格;注意必须保证操作之前已添加对应的数据源数据,否则报错
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
}
}

#pragma mark - TableView, move row
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
//设置最后的Row不可移动
return indexPath.row < (_mArrDataSource.count - 1);
}

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
//限制单元格移动到最后的Row下方
NSIndexPath *indexPath = proposedDestinationIndexPath.row < (_mArrDataSource.count - 1) ? proposedDestinationIndexPath : sourceIndexPath;
return indexPath;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSUInteger sourceRow = sourceIndexPath.row;
NSUInteger destinationRow = destinationIndexPath.row;
//单元格向下移动时;循环进行当前数据与下一位数据交换位置
while (sourceRow < destinationRow) {
[_mArrDataSource exchangeObjectAtIndex:sourceRow withObjectAtIndex:(sourceRow + 1)];
sourceRow++;
}
//单元格向上移动时;循环进行当前数据与上一位数据交换位置
while (sourceRow > destinationRow) {
[_mArrDataSource exchangeObjectAtIndex:sourceRow withObjectAtIndex:(sourceRow - 1)];
sourceRow--;
}
}

@end


AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navigationController;

@end


[b]AppDelegate.m[/b]

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *viewController = [[ViewController alloc] init];
_navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
_window.rootViewController = _navigationController;
//[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无
[_window makeKeyAndVisible];
return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
}

- (void)applicationWillTerminate:(UIApplication *)application {
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: