iOS 編輯UITableView(根據iOS編程編寫)


  上個項目我們完成了 JXHomepwner 簡單的應用展示,項目地址。本節我們需要在上節項目基礎上,增加一些響應用戶操作。包括添加,刪除和移動表格。

  • 編輯模式

  UITableView 有一個名為  editing 的屬性,如果將其設置為  YES UITableView 就會進入編輯模式。在編輯模式下,用戶可以管理 UITableView 中的表格行,我們可以添加、刪除和移動等操作。但是編輯模式沒有聽過修改行的內容的功能。

  首先要更新界面,使用戶可以將 UITableView 對象設置為編輯模式。示例程序是為 UITableView 對象的 表頭視圖 增加一個按鈕,然后通過按鈕使  UITableView 對象進入或者退出編輯模式。表頭視圖是指 UITableView 對象可以在其表格上方顯示特定視圖,適合放置針對某個表格段或者整張表格的標題和控件。表頭視圖可以是任意的 UIView 對象。

  表頭視圖有兩種,分別針對表格段和表格。類似的,還有 表尾視圖 也具有表格段和表格兩種。

  接下來創建一個針對表格的表頭視圖。這個表頭視圖包含兩個 UIButton 對象,其中一個負責切換 UITableView 對象的編輯模式,另一個負責創建新的 JXItem 對象並加入 UITableView 對象。可以使用代碼,也可以使用 XIB 方式來創建。

#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

  構建並運行結果

  • 增加行

  通常有兩種方式可以在運行時為 UITableView 對象增加行。

  * 在表視圖上方放置添加按鈕。如果數據的字段比較多的話,就需要顯示一個用於輸入的詳細視圖,就可以使用這種方式。例如,在iOS自帶通訊錄中,點擊添加按鈕。

  * 在 UITableViewCell 對象左邊顯示一個綠色加號按鈕。在為數據添加一個新字段時可以使用這種方式。在聯系人應用中為聯系人添加生日。

 本節采用另一種方式:在  headView 中放置一個標題為 New 的按鈕。當用戶點擊這個按鈕,添加一行新數據。

#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

  構建並運行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM