#import "ViewController.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray *datalist;
@property (nonatomic,strong)UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
_datalist = [NSMutableArray arrayWithArray:[UIFont familyNames]];
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH-64) style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
}
//进入编辑模式
- (IBAction)editor:(UIButton *)sender {
sender.selected = !sender.selected;
//表视图开启编辑模式
[_tableView setEditing:sender.selected animated:YES];
}
//进入多选模式
- (IBAction)multipleSelect:(UIButton *)sender {
if (sender.selected == YES) {
//1.获取所有选中的单元格下标
NSArray *indexPaths = [_tableView indexPathsForSelectedRows];
//2.删除数据
/**
* 为了防止下标越界,所以采用倒序遍历并删除
*/
for (NSInteger i = indexPaths.count-1; i>=0; i--) {
NSIndexPath *indexP = indexPaths[i];
[_datalist removeObjectAtIndex:indexP.row];
}
//3.删除单元格
[_tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
sender.selected = !sender.selected;
//在编辑期间允许单元格多选 --> 开启该选项 插入单元格和删除单个单元格 功能就不存在了
_tableView.allowsMultipleSelectionDuringEditing = sender.selected;
//表视图开启编辑模式
[_tableView setEditing:sender.selected animated:YES];
NSLog(@"%@",_datalist);
}
//
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
//奇数行 删除 ;偶数行 插入
// return indexPath.row%2==0?UITableViewCellEditingStyleInsert:UITableViewCellEditingStyleDelete;
}
//是否开启单元格的移动权限
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
return NO;
}
return YES;
}
//是否开启单元格的编辑权限
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
//如果是0的话,第一行的加号不会显示
if (indexPath.row == 1) {
return NO;
}
return YES;
}
//移动单元格调用
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSLog(@"source:%ld destination:%ld",sourceIndexPath.row,destinationIndexPath.row);
if (sourceIndexPath.row <= destinationIndexPath.row) {
[_datalist insertObject:_datalist[sourceIndexPath.row] atIndex:destinationIndexPath.row+1];
[_datalist removeObjectAtIndex:sourceIndexPath.row];
}else{
[_datalist insertObject:_datalist[sourceIndexPath.row] atIndex:destinationIndexPath.row];
[_datalist removeObjectAtIndex:sourceIndexPath.row+1];
}
}
//编辑模式 事件方法 插入和删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
/**
* UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete, 删除
UITableViewCellEditingStyleInsert 插入
*/
if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除单元格的代码
//1.删除数据
[_datalist removeObjectAtIndex:indexPath.row];
//2.删除单元格
/**
* UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
*/
[_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
// [_datalist insertObject:@"1234567" atIndex:1];
//
// [tableView reloadData];
//1.插入数据
[_datalist insertObject:@"1234567" atIndex:1];
//2.插入单元格
NSIndexPath *ip = [NSIndexPath indexPathForRow:1 inSection:0];
[tableView insertRowsAtIndexPaths:@[ip] withRowAnimation:UITableViewRowAnimationTop];
}
}
#pragma mark --UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _datalist.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identy = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identy];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy];
}
cell.textLabel.text = [_datalist objectAtIndex:indexPath.row];
return cell;
}
@end
屏幕快照 2016-03-03 下午8.33.43.png
屏幕快照 2016-03-03 下午8.33.12.png
网友评论