上個項目咱們完成了 JXHomepwner 簡單的應用展現,項目地址。本節咱們須要在上節項目基礎上,增長一些響應用戶操做。包括添加,刪除和移動表格。git
UITableView 有一個名爲 editing 的屬性,若是將其設置爲 YES , UITableView 就會進入編輯模式。在編輯模式下,用戶能夠管理 UITableView 中的表格行,咱們能夠添加、刪除和移動等操做。可是編輯模式沒有聽過修改行的內容的功能。github
首先要更新界面,使用戶能夠將 UITableView 對象設置爲編輯模式。示例程序是爲 UITableView 對象的 表頭視圖 增長一個按鈕,而後經過按鈕使 UITableView 對象進入或者退出編輯模式。表頭視圖是指 UITableView 對象能夠在其表格上方顯示特定視圖,適合放置針對某個表格段或者整張表格的標題和控件。表頭視圖能夠是任意的 UIView 對象。數組
表頭視圖有兩種,分別針對表格段和表格。相似的,還有 表尾視圖 也具備表格段和表格兩種。dom
接下來建立一個針對表格的表頭視圖。這個表頭視圖包含兩個 UIButton 對象,其中一個負責切換 UITableView 對象的編輯模式,另外一個負責建立新的 JXItem 對象並加入 UITableView 對象。可使用代碼,也可使用 XIB 方式來建立。atom
#import "JXItemsViewController.h" #import "JXItem.h" #import "JXItemStore.h" @interface JXItemsViewController () /** 頭部視圖 */ @property (nonatomic,weak) UIView * headerView; /** 編輯按鈕 */ @property (nonatomic,strong) UIButton * editButton; @end @implementation JXItemsViewController - (instancetype)init { // 調用父類的指定初始化方法 self = [super initWithStyle:UITableViewStylePlain]; if (self) { for (NSInteger i=0; i<15; i++) { [[JXItemStore sharedStore] createItem]; } } return self; } - (instancetype)initWithStyle:(UITableViewStyle)style { return [self init]; } - (void)viewDidLoad { [super viewDidLoad]; // 向控制器註冊 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"]; // 加載頭視圖 [self headerView]; } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[[JXItemStore sharedStore] allItem] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 建立 UITableViewCell 對象,風格使用默認風格 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath]; // 獲取 allItem 的第 n 個 JXItem 對象 // 而後將該 JXItem 對象的描述信息賦值給 UITableViewCell 對象的 textLabel // 這裏的 n 是該 UITableViewCell 對象所對應的表格索引 NSArray * items = [[JXItemStore sharedStore] allItem]; JXItem * item = items[indexPath.row]; cell.textLabel.text = [item description]; return cell; } #pragma mark - 懶加載 - (UIView *)headerView{ if (_headerView == nil) { UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)]; // 設置頭部視圖 self.tableView.tableHeaderView = headerView; headerView.backgroundColor = [UIColor cyanColor]; [headerView addSubview:self.editButton]; _headerView = headerView; } return _headerView; } - (UIButton *)editButton{ if (_editButton == nil) { _editButton = [UIButton buttonWithType:UIButtonTypeCustom]; _editButton.frame = CGRectMake(0, 0, self.view.bounds.size.width / 2, 50); [_editButton setTitle:@"Edit" forState:UIControlStateNormal]; _editButton.backgroundColor = [UIColor greenColor]; [_editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_editButton addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchDown]; } return _editButton; } #pragma mark - 點擊事件 - (void)editClick:(UIButton *)sender { if (self.isEditing) { // 若是是編輯狀態,取消編輯 // 更改文字 [sender setTitle:@"Edit" forState:UIControlStateNormal]; // 取消編輯 [self setEditing:NO animated:YES]; } else { // 更改文字 [sender setTitle:@"Done" forState:UIControlStateNormal]; // 開始編輯 [self setEditing:YES animated:YES]; } } @end
構建並運行結果spa
一般有兩種方式能夠在運行時爲 UITableView 對象增長行。代理
* 在表視圖上方放置添加按鈕。若是數據的字段比較多的話,就須要顯示一個用於輸入的詳細視圖,就可使用這種方式。例如,在iOS自帶通信錄中,點擊添加按鈕。指針
* 在 UITableViewCell 對象左邊顯示一個綠色加號按鈕。在爲數據添加一個新字段時可使用這種方式。在聯繫人應用中爲聯繫人添加生日。code
本節採用另外一種方式:在 headView 中放置一個標題爲 New 的按鈕。當用戶點擊這個按鈕,添加一行新數據。orm
#import "JXItemsViewController.h" #import "JXItem.h" #import "JXItemStore.h" @interface JXItemsViewController () /** 頭部視圖 */ @property (nonatomic,weak) UIView * headerView; /** 編輯按鈕 */ @property (nonatomic,strong) UIButton * editButton; /** 增長按鈕 */ @property (nonatomic,strong) UIButton * addButton; @end @implementation JXItemsViewController - (instancetype)init { // 調用父類的指定初始化方法 self = [super initWithStyle:UITableViewStylePlain]; if (self) { for (NSInteger i=0; i<15; i++) { [[JXItemStore sharedStore] createItem]; } } return self; } - (instancetype)initWithStyle:(UITableViewStyle)style { return [self init]; } - (void)viewDidLoad { [super viewDidLoad]; // 向控制器註冊 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"]; // 加載頭視圖 [self headerView]; } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[[JXItemStore sharedStore] allItem] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 建立 UITableViewCell 對象,風格使用默認風格 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath]; // 獲取 allItem 的第 n 個 JXItem 對象 // 而後將該 JXItem 對象的描述信息賦值給 UITableViewCell 對象的 textLabel // 這裏的 n 是該 UITableViewCell 對象所對應的表格索引 NSArray * items = [[JXItemStore sharedStore] allItem]; JXItem * item = items[indexPath.row]; cell.textLabel.text = [item description]; return cell; } #pragma mark - 懶加載 - (UIView *)headerView{ if (_headerView == nil) { UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)]; // 設置頭部視圖 self.tableView.tableHeaderView = headerView; headerView.backgroundColor = [UIColor cyanColor]; [headerView addSubview:self.editButton]; [headerView addSubview:self.addButton]; _headerView = headerView; } return _headerView; } - (UIButton *)editButton{ if (_editButton == nil) { _editButton = [UIButton buttonWithType:UIButtonTypeCustom]; _editButton.frame = CGRectMake(0, 0, self.view.bounds.size.width / 2, 50); [_editButton setTitle:@"Edit" forState:UIControlStateNormal]; _editButton.backgroundColor = [UIColor greenColor]; [_editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_editButton addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchDown]; } return _editButton; } - (UIButton *)addButton{ if (_addButton == nil) { UIButton * addButton = [UIButton buttonWithType:UIButtonTypeCustom]; addButton.frame = CGRectMake(self.view.bounds.size.width / 2, 0, self.view.bounds.size.width / 2, 50); [addButton setTitle:@"Add" forState:UIControlStateNormal]; addButton.backgroundColor = [UIColor blueColor]; [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [addButton addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchDown]; _addButton = addButton; } return _addButton; } #pragma mark - 點擊事件 - (void)editClick:(UIButton *)sender { if (self.isEditing) { // 若是是編輯狀態,取消編輯 // 更改文字 [sender setTitle:@"Edit" forState:UIControlStateNormal]; // 取消編輯 [self setEditing:NO animated:YES]; } else { // 更改文字 [sender setTitle:@"Done" forState:UIControlStateNormal]; // 開始編輯 [self setEditing:YES animated:YES]; } } /** * 添加表格時,必須保證 UITableView 對象當前顯示的行數與數據源的提供的行數相同。 * 因此,在添加以前,必須先建立一個新的 JXItem 對象並加入到 JXItemStore 中 * * @param sender 按鈕 */ - (void)addClick:(UIButton *)sender { // 建立新的 JXItem 對象,並加入到 JXItemStore 中 JXItem * newItem = [[JXItemStore sharedStore] createItem]; // 獲取新的對象在 allItem 數組中的索引 NSInteger lastRow = [[[JXItemStore sharedStore] allItem] indexOfObject:newItem]; NSIndexPath * indexPath = [NSIndexPath indexPathForRow:lastRow inSection:0]; // 將新航插入 UITableView 對象 [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; } @end
構建並運行
在編輯模式下,UITableViewCell 對象多是會顯示中間有個減號的紅色圓圈。這個紅色圓圈是刪除行控件,按下刪除控件就能夠刪除其所屬的那個表格行。可是目前咱們的應用按下不會有任何操做。
要刪除 JXHomepwner 中的某個表格行,就必須執行兩步:1.從UITableView 對象刪除指定的 UITableViewCell 對象。2.找到和須要刪除的 UITableViewCell 對象對應的 JXItem 對象,也將其從 JXItemStore 中刪除。爲了完成第二步,咱們必須在 JXItemStore 中實現新的方法。
#import <Foundation/Foundation.h> @class JXItem; @interface JXItemStore : NSObject /** 存放 JXItem 對象數組 */ @property (nonatomic,readonly) NSArray * allItem; // 注意,這是一個類方法,前綴是+ + (instancetype)sharedStore; - (JXItem *)createItem; /** * 刪除對象 * * @param item 須要刪除的對象 */ - (void)removeItem:(JXItem *)item; @end
#import "JXItemStore.h" #import "JXItem.h" @interface JXItemStore () /** 可變數組,用來操做 JXItem 對象 */ @property (nonatomic,strong) NSMutableArray * privateItems; @end @implementation JXItemStore // 單粒對象 + (instancetype)sharedStore { static JXItemStore * sharedStore = nil; // 判斷是否須要建立一個 sharedStore 對象 if (!sharedStore) { sharedStore = [[self alloc] init]; } return sharedStore; } - (NSArray *)allItem { return [self.privateItems copy]; } - (JXItem *)createItem { JXItem * item = [JXItem randomItem]; [self.privateItems addObject:item]; return item; } /** * 還能夠調用 [self.privateItems removeObject:item] * [self.privateItems removeObjectIdenticalTo:item] 與上面的方法的區別就是:上面的方法會枚舉數組,向每個數組發送 isEqual: 消息。 * isEqual: 的做用是判斷當前對象和傳入對象所包含的數據是否相等。可能會複寫 這個方法。 * removeObjectIdenticalTo: 方法不會比較對象所包含的數據,只會比較指向對象的指針 * * @param item 須要刪除的對象 */ - (void)removeItem:(JXItem *)item { [self.privateItems removeObjectIdenticalTo:item]; } #pragma mark - 懶加載 - (NSMutableArray *)privateItems{ if (_privateItems == nil) { _privateItems = [[NSMutableArray alloc] init]; } return _privateItems; } @end
接下來爲 JXItemsViewController 實現
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
這個方法是 UITableViewDataSource 協議所聲明的方法之一。當 UITableView 對象在向其數據源發送這個消息的時候,會傳入三個實參。第一個參數是發送該消息的 UITableView 對象。第二個實參是 UITableViewCellEditingStyle 類型的常量(刪除表格行時,傳入的是UITableViewCellEditingStyleDelete)。第三個實參是一個 NSIndexPath 對象,其中包含相應表格行所在的表格段索引和行索引。
#import "JXItemsViewController.h" #import "JXItem.h" #import "JXItemStore.h" @interface JXItemsViewController () /** 頭部視圖 */ @property (nonatomic,weak) UIView * headerView; /** 編輯按鈕 */ @property (nonatomic,strong) UIButton * editButton; /** 增長按鈕 */ @property (nonatomic,strong) UIButton * addButton; @end @implementation JXItemsViewController - (instancetype)init { // 調用父類的指定初始化方法 self = [super initWithStyle:UITableViewStylePlain]; if (self) { for (NSInteger i=0; i<15; i++) { [[JXItemStore sharedStore] createItem]; } } return self; } - (instancetype)initWithStyle:(UITableViewStyle)style { return [self init]; } - (void)viewDidLoad { [super viewDidLoad]; // 向控制器註冊 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"]; // 加載頭視圖 [self headerView]; } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[[JXItemStore sharedStore] allItem] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 建立 UITableViewCell 對象,風格使用默認風格 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath]; // 獲取 allItem 的第 n 個 JXItem 對象 // 而後將該 JXItem 對象的描述信息賦值給 UITableViewCell 對象的 textLabel // 這裏的 n 是該 UITableViewCell 對象所對應的表格索引 NSArray * items = [[JXItemStore sharedStore] allItem]; JXItem * item = items[indexPath.row]; cell.textLabel.text = [item description]; return cell; } /** * 刪除行 * * @param tableView 對象 * @param editingStyle 操做 * @param indexPath 操做的行數 */ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // 若是tableView請求的是刪除對象 if (editingStyle == UITableViewCellEditingStyleDelete) { // 取出須要刪除的對象 NSArray * items = [[JXItemStore sharedStore] allItem]; JXItem * item = items[indexPath.row]; // 刪除對象 [[JXItemStore sharedStore] removeItem:item]; // 刷新表格 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } } #pragma mark - 懶加載 - (UIView *)headerView{ if (_headerView == nil) { UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)]; // 設置頭部視圖 self.tableView.tableHeaderView = headerView; headerView.backgroundColor = [UIColor cyanColor]; [headerView addSubview:self.editButton]; [headerView addSubview:self.addButton]; _headerView = headerView; } return _headerView; } - (UIButton *)editButton{ if (_editButton == nil) { _editButton = [UIButton buttonWithType:UIButtonTypeCustom]; _editButton.frame = CGRectMake(0, 0, self.view.bounds.size.width / 2, 50); [_editButton setTitle:@"Edit" forState:UIControlStateNormal]; _editButton.backgroundColor = [UIColor greenColor]; [_editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_editButton addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchDown]; } return _editButton; } - (UIButton *)addButton{ if (_addButton == nil) { UIButton * addButton = [UIButton buttonWithType:UIButtonTypeCustom]; addButton.frame = CGRectMake(self.view.bounds.size.width / 2, 0, self.view.bounds.size.width / 2, 50); [addButton setTitle:@"Add" forState:UIControlStateNormal]; addButton.backgroundColor = [UIColor blueColor]; [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [addButton addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchDown]; _addButton = addButton; } return _addButton; } #pragma mark - 點擊事件 - (void)editClick:(UIButton *)sender { if (self.isEditing) { // 若是是編輯狀態,取消編輯 // 更改文字 [sender setTitle:@"Edit" forState:UIControlStateNormal]; // 取消編輯 [self setEditing:NO animated:YES]; } else { // 更改文字 [sender setTitle:@"Done" forState:UIControlStateNormal]; // 開始編輯 [self setEditing:YES animated:YES]; } } /** * 添加表格時,必須保證 UITableView 對象當前顯示的行數與數據源的提供的行數相同。 * 因此,在添加以前,必須先建立一個新的 JXItem 對象並加入到 JXItemStore 中 * * @param sender 按鈕 */ - (void)addClick:(UIButton *)sender { // 建立新的 JXItem 對象,並加入到 JXItemStore 中 JXItem * newItem = [[JXItemStore sharedStore] createItem]; // 獲取新的對象在 allItem 數組中的索引 NSInteger lastRow = [[[JXItemStore sharedStore] allItem] indexOfObject:newItem]; NSIndexPath * indexPath = [NSIndexPath indexPathForRow:lastRow inSection:0]; // 將新航插入 UITableView 對象 [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; } @end
接下來,繼續調用數據源代理的另外一個方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
是當在編輯模式下,會在每一個表格行的右側顯示一個換位控件,按住拖動,能夠將相應的表格行移動到手指拖動的位置。對象在向其數據源發送這個消息的時候,一樣會傳入三個實參。第一個參數是發送該消息的 UITableView 對象。第二個實參是 NSIndexPath 對象是表格原位置。第三個實參是一個 NSIndexPath 對象是目的位置
#import <Foundation/Foundation.h> @class JXItem; @interface JXItemStore : NSObject /** 存放 JXItem 對象數組 */ @property (nonatomic,readonly) NSArray * allItem; // 注意,這是一個類方法,前綴是+ + (instancetype)sharedStore; - (JXItem *)createItem; /** * 刪除對象 * * @param item 須要刪除的對象 */ - (void)removeItem:(JXItem *)item; /** * 移動對象 * * @param fromIndex 移動對象的起始位置 * @param toIndex 移動後的位置 */ - (void)moveItemAtIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex; @end
#import "JXItemStore.h" #import "JXItem.h" @interface JXItemStore () /** 可變數組,用來操做 JXItem 對象 */ @property (nonatomic,strong) NSMutableArray * privateItems; @end @implementation JXItemStore // 單粒對象 + (instancetype)sharedStore { static JXItemStore * sharedStore = nil; // 判斷是否須要建立一個 sharedStore 對象 if (!sharedStore) { sharedStore = [[self alloc] init]; } return sharedStore; } - (NSArray *)allItem { return [self.privateItems copy]; } - (JXItem *)createItem { JXItem * item = [JXItem randomItem]; [self.privateItems addObject:item]; return item; } /** * 還能夠調用 [self.privateItems removeObject:item] * [self.privateItems removeObjectIdenticalTo:item] 與上面的方法的區別就是:上面的方法會枚舉數組,向每個數組發送 isEqual: 消息。 * isEqual: 的做用是判斷當前對象和傳入對象所包含的數據是否相等。可能會複寫 這個方法。 * removeObjectIdenticalTo: 方法不會比較對象所包含的數據,只會比較指向對象的指針 * * @param item 須要刪除的對象 */ - (void)removeItem:(JXItem *)item { [self.privateItems removeObjectIdenticalTo:item]; } - (void)moveItemAtIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex { // 若是起始位置和最終位置相同,則不懂 if (fromIndex == toIndex) return; // 須要移動的對象的指針 JXItem * item = self.privateItems[fromIndex]; // 將 item 從 allItem 數組中移除 [self.privateItems removeObjectAtIndex:fromIndex]; // 根據新的索引位置,將item 插入到allItem 數組中 [self.privateItems insertObject:item atIndex:toIndex]; } #pragma mark - 懶加載 - (NSMutableArray *)privateItems{ if (_privateItems == nil) { _privateItems = [[NSMutableArray alloc] init]; } return _privateItems; } @end
#import "JXItemsViewController.h" #import "JXItem.h" #import "JXItemStore.h" @interface JXItemsViewController () /** 頭部視圖 */ @property (nonatomic,weak) UIView * headerView; /** 編輯按鈕 */ @property (nonatomic,strong) UIButton * editButton; /** 增長按鈕 */ @property (nonatomic,strong) UIButton * addButton; @end @implementation JXItemsViewController - (instancetype)init { // 調用父類的指定初始化方法 self = [super initWithStyle:UITableViewStylePlain]; if (self) { for (NSInteger i=0; i<15; i++) { [[JXItemStore sharedStore] createItem]; } } return self; } - (instancetype)initWithStyle:(UITableViewStyle)style { return [self init]; } - (void)viewDidLoad { [super viewDidLoad]; // 向控制器註冊 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"]; // 加載頭視圖 [self headerView]; } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[[JXItemStore sharedStore] allItem] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 建立 UITableViewCell 對象,風格使用默認風格 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath]; // 獲取 allItem 的第 n 個 JXItem 對象 // 而後將該 JXItem 對象的描述信息賦值給 UITableViewCell 對象的 textLabel // 這裏的 n 是該 UITableViewCell 對象所對應的表格索引 NSArray * items = [[JXItemStore sharedStore] allItem]; JXItem * item = items[indexPath.row]; cell.textLabel.text = [item description]; return cell; } /** * 刪除行 * * @param tableView 對象 * @param editingStyle 操做 * @param indexPath 操做的行數 */ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // 若是tableView請求的是刪除對象 if (editingStyle == UITableViewCellEditingStyleDelete) { // 取出須要刪除的對象 NSArray * items = [[JXItemStore sharedStore] allItem]; JXItem * item = items[indexPath.row]; // 刪除對象 [[JXItemStore sharedStore] removeItem:item]; // 刷新表格 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } } /** * 移動行 * * @param tableView 對象 * @param sourceIndexPath 須要移動的行 * @param destinationIndexPath 目標行 */ - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { [[JXItemStore sharedStore] moveItemAtIndex:sourceIndexPath.row toIndex:destinationIndexPath.row]; } #pragma mark - 懶加載 - (UIView *)headerView{ if (_headerView == nil) { UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)]; // 設置頭部視圖 self.tableView.tableHeaderView = headerView; headerView.backgroundColor = [UIColor cyanColor]; [headerView addSubview:self.editButton]; [headerView addSubview:self.addButton]; _headerView = headerView; } return _headerView; } - (UIButton *)editButton{ if (_editButton == nil) { _editButton = [UIButton buttonWithType:UIButtonTypeCustom]; _editButton.frame = CGRectMake(0, 0, self.view.bounds.size.width / 2, 50); [_editButton setTitle:@"Edit" forState:UIControlStateNormal]; _editButton.backgroundColor = [UIColor greenColor]; [_editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_editButton addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchDown]; } return _editButton; } - (UIButton *)addButton{ if (_addButton == nil) { UIButton * addButton = [UIButton buttonWithType:UIButtonTypeCustom]; addButton.frame = CGRectMake(self.view.bounds.size.width / 2, 0, self.view.bounds.size.width / 2, 50); [addButton setTitle:@"Add" forState:UIControlStateNormal]; addButton.backgroundColor = [UIColor blueColor]; [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [addButton addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchDown]; _addButton = addButton; } return _addButton; } #pragma mark - 點擊事件 - (void)editClick:(UIButton *)sender { if (self.isEditing) { // 若是是編輯狀態,取消編輯 // 更改文字 [sender setTitle:@"Edit" forState:UIControlStateNormal]; // 取消編輯 [self setEditing:NO animated:YES]; } else { // 更改文字 [sender setTitle:@"Done" forState:UIControlStateNormal]; // 開始編輯 [self setEditing:YES animated:YES]; } } /** * 添加表格時,必須保證 UITableView 對象當前顯示的行數與數據源的提供的行數相同。 * 因此,在添加以前,必須先建立一個新的 JXItem 對象並加入到 JXItemStore 中 * * @param sender 按鈕 */ - (void)addClick:(UIButton *)sender { // 建立新的 JXItem 對象,並加入到 JXItemStore 中 JXItem * newItem = [[JXItemStore sharedStore] createItem]; // 獲取新的對象在 allItem 數組中的索引 NSInteger lastRow = [[[JXItemStore sharedStore] allItem] indexOfObject:newItem]; NSIndexPath * indexPath = [NSIndexPath indexPathForRow:lastRow inSection:0]; // 將新航插入 UITableView 對象 [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; } @end
構建並運行