參考: http://www.open-open.com/lib/view/open1430008922468.html
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.dataArray = [NSMutableArray arrayWithArray: @[@"1",@"2",@"3",@"4",@"5",@"6",@"7"]];
[self createView];
self.view.backgroundColor = [UIColor greenColor];
}
-(void)createView{
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ce"];
// self.tableView.bounces = 0;
// self.tableView.showsVerticalScrollIndicator = 0;
self.tableView.tableFooterView = [[UIView alloc]init];//沒有的不展示 line
UIBarButtonItem *deleteBar = [[UIBarButtonItem alloc]initWithTitle:@"刪除" style: UIBarButtonItemStylePlain target:self action:@selector(deleteBtn)];
self.navigationItem.rightBarButtonItem = deleteBar;
UIBarButtonItem *addBar1 = [[UIBarButtonItem alloc]initWithTitle:@"添加1 " style: UIBarButtonItemStylePlain target:self action:@selector(addBtn)];
UIBarButtonItem *addBar2 = [[UIBarButtonItem alloc]initWithTitle:@" 添加2(模式)" style: UIBarButtonItemStylePlain target:self action:@selector(addBtn2)];
self.navigationItem.leftBarButtonItems = @[addBar1,addBar2];
}
//刪除(進入編輯模式)
- (void)deleteBtn{
self.isAdd = NO;
static int flog = 1;
flog ^= 1;
if (flog) {
[self.tableView setEditing:YES animated:YES];
}else{
[self.tableView setEditing:NO animated:YES];
}
}
//增加1
- (void)addBtn{
[self.dataArray addObject:@"新增一條"];
//在 <指定行> 添加
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:self.dataArray.count - 1 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];
}
- (void)addBtn2{
self.isAdd = YES;
static int flog = 1;
flog ^= 1;
if (flog) {
[self.tableView setEditing:YES animated:YES];
}else{
[self.tableView setEditing:NO animated:YES];
}
}
#pragma mark --dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ce"];
cell.textLabel.text = self.dataArray[indexPath.row];
return cell;
}
#pragma mark -- delegate 編輯模式(增加/刪除/移動)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.isAdd) {
return UITableViewCellEditingStyleInsert; //添加
}else{
return UITableViewCellEditingStyleDelete; //刪除
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger sourceRow = indexPath.row;
//刪除
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.dataArray removeObjectAtIndex:sourceRow];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
}
//添加(點擊+號按鈕)
else if (editingStyle == UITableViewCellEditingStyleInsert){
[self.dataArray insertObject:@"新增" atIndex: sourceRow+1];
//在指定位置插入
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:sourceRow+1 inSection:indexPath.section];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil] withRowAnimation:UITableViewRowAnimationRight];
}
}
//移動(只有寫了這個方法才能移動,但此時不能左滑)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSInteger sourceRow = sourceIndexPath.row;
NSInteger destinRow = destinationIndexPath.row;
NSString *moveObject = [self.dataArray objectAtIndex:sourceRow];
[self.dataArray removeObjectAtIndex:sourceRow];
[self.dataArray insertObject:moveObject atIndex:destinRow]; //改變在<對象>數組中的位置
}
#pragma mark --左滑選項(title可自已定義)
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
//1.刪除
UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" 刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {//title可自已定義
NSLog(@"點擊了-刪除");
[self.dataArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
}];
deleteRoWAction.backgroundColor = [UIColor greenColor]; //定義button的顏色,默認是紅色的
//test
UITableViewRowAction *test = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" 置頂" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {//title可自已定義
NSLog(@"點擊了-test");
}];
test.backgroundColor = [UIColor blueColor];
return @[deleteRoWAction,test];//最后返回這倆個RowAction 的數組
}
//注釋:從 xib 加載 cell 的方式
方法一:
第一步: [self.collectionView registerNib:[UINib nibWithNibName:@"QGLShareBtnCell" bundle:nil] forCellWithReuseIdentifier:@"QGLShareBtnCell”]; 第二步: QGLShareBtnCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QGLShareBtnCell" forIndexPath:indexPath];
方法二:
QGLIMGroupListCell *cell = (QGLIMGroupListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell= (QGLIMGroupListCell *)[[[NSBundle mainBundle] loadNibNamed:@"QGLIMGroupListCell" owner:self options:nil] lastObject]; }
//另外: 從xib 加載視圖 UIView
UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"QGLIMGroupListCell" owner:self options:nil] lastObject];
// 獲取 view 上的自視圖,通過 tag 獲取 UIButton *btn = [view viewWithTag:111];