當我們點擊添加按鈕時可以向提示框輸入數據添加到表格,當我們點擊編輯時我們可以對表格進行移動刪除操作
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
UITableView *_table;
NSMutableArray *marr;
UIButton *leftBtn;
}
@end
@implementation ViewController
//這里主要實現表格的初始化,數組的初始化,左右導航按鈕的初始化
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"表格";
//添加編輯按鈕
leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[leftBtn setTitle:@"編輯" forState:UIControlStateNormal];
[leftBtn setTitle:@"完成" forState:UIControlStateSelected];
leftBtn.frame = CGRectMake(0, 0, 80, 40);
leftBtn.titleLabel.font = [UIFont systemFontOfSize:15];
//標題顏色
[leftBtn setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(leftBarBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:leftBtn];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarBtnClicked:)];
//創建表格并初始化
_table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_table.delegate = self;
_table.dataSource = self;
//將表格添加到視圖
[self.view addSubview:_table];
//初始化三個人名
marr = [NSMutableArray arrayWithObjects:@"王二",@"劉山",@"李四", nil];
}
#pragma mark - 導航按鈕點擊觸發方法
- (void)leftBarBtnClicked:(id)sender {
//設置tableview編輯狀態
BOOL flag = !_table.editing;
[_table setEditing:flag animated:YES];
leftBtn.selected = flag;
}
- (void)rightBarBtnClicked:(id)sender {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"請輸入"
message:@"請輸入姓名"
delegate:self cancelButtonTitle:@"確定"
otherButtonTitles:@"取消", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
}
#pragma mark? 獲得輸入框里的值
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"確定"]){
UITextField *tf=[alertView textFieldAtIndex:0];//獲得輸入框
NSString * res = tf.text;//獲得值
NSLog(@"%@",res);
[marr addObject:res];
[_table reloadData];
}
}
#pragma mark - 表格方法
#pragma mark 選中行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 取消選中狀態
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//表格有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return marr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
}
//將人名添加到表格
cell.textLabel.text = marr[indexPath.row];
//? ? cell.accessoryType = UITableViewCellAccessoryNone;//cell沒有任何的樣式
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右邊有一個小箭頭,距離右邊有十幾像素;
//? ? cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//cell右邊有一個藍色的圓形button;
//? ? cell.accessoryType = UITableViewCellAccessoryCheckmark;//cell右邊的形狀是對號;
return cell;
}
#pragma mark - 提交編輯操作
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//只要實現這個方法,就實現了默認滑動刪除!!!!!
if (editingStyle != UITableViewCellEditingStyleDelete)
return;
//刪除數據模型
[marr removeObjectAtIndex:indexPath.row];
[_table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
#pragma mark - 移動操作
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
//起始位置
NSInteger fromRow = fromIndexPath.row;
//終止位置
NSInteger toRow = toIndexPath.row;
NSLog(@"%ld,%ld",fromRow,toRow);
//先取出起始位置的數據
NSString *fromContent = marr[fromRow];
//把起始位置的數據插入終止位置
[marr insertObject:fromContent atIndex:toRow];
NSLog(@"%@",marr);
}
@end