當(dāng)我們點(diǎn)擊添加按鈕時(shí)可以向提示框輸入數(shù)據(jù)添加到表格,當(dāng)我們點(diǎn)擊編輯時(shí)我們可以對(duì)表格進(jìn)行移動(dòng)刪除操作
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
UITableView *_table;
NSMutableArray *marr;
UIButton *leftBtn;
}
@end
@implementation ViewController
//這里主要實(shí)現(xiàn)表格的初始化,數(shù)組的初始化,左右導(dǎo)航按鈕的初始化
- (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];
//標(biāo)題顏色
[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:)];
//創(chuàng)建表格并初始化
_table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_table.delegate = self;
_table.dataSource = self;
//將表格添加到視圖
[self.view addSubview:_table];
//初始化三個(gè)人名
marr = [NSMutableArray arrayWithObjects:@"王二",@"劉山",@"李四", nil];
}
#pragma mark - 導(dǎo)航按鈕點(diǎn)擊觸發(fā)方法
- (void)leftBarBtnClicked:(id)sender {
//設(shè)置tableview編輯狀態(tài)
BOOL flag = !_table.editing;
[_table setEditing:flag animated:YES];
leftBtn.selected = flag;
}
- (void)rightBarBtnClicked:(id)sender {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"請(qǐng)輸入"
message:@"請(qǐng)輸入姓名"
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
{
// 取消選中狀態(tài)
[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沒(méi)有任何的樣式
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右邊有一個(gè)小箭頭耳鸯,距離右邊有十幾像素撇寞;
//? ? cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//cell右邊有一個(gè)藍(lán)色的圓形button;
//? ? cell.accessoryType = UITableViewCellAccessoryCheckmark;//cell右邊的形狀是對(duì)號(hào)胡控;
return cell;
}
#pragma mark - 提交編輯操作
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//只要實(shí)現(xiàn)這個(gè)方法扎瓶,就實(shí)現(xiàn)了默認(rèn)滑動(dòng)刪除Q罕啤s锎搿!0!
if (editingStyle != UITableViewCellEditingStyleDelete)
return;
//刪除數(shù)據(jù)模型
[marr removeObjectAtIndex:indexPath.row];
[_table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
#pragma mark - 移動(dòng)操作
- (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);
//先取出起始位置的數(shù)據(jù)
NSString *fromContent = marr[fromRow];
//把起始位置的數(shù)據(jù)插入終止位置
[marr insertObject:fromContent atIndex:toRow];
NSLog(@"%@",marr);
}
@end