修改儲存格——刪除、插入、移動(IOS)

來源:互聯網
上載者:User

標籤:ios   ios開發   objective-c   uiviewcontroller   uitableviewcell   

插入和刪除時序:

client: setEditing: animated: -----> 設定進入表視圖

表視圖---->委託: (<UITableViewDelegate>)tableView:editingStyleForRowAtIndexPath:方法進行單元格編輯表徵圖的設定

方法進行單元格編輯表徵圖的設定

表視圖---->資料來源:(<UITableViewDataSource>)tableView:commiEditingStyle:forRowAtIndexPath訊息實現刪除或者插入的處理

這裡由點擊insert or delete控制項來實現

                               刪除和增加單元格要通過[self.tableView reloadData]來重新載入表視圖數!!

移動時序:

client:setEditing:animated:----->設定進入表視圖

表視圖--->委託:(<UITableViewDelegate>)tableView:editingStyleForRowAtIndexPath:方法進行單元格編輯表徵圖的設定

方法進行單元格編輯表徵圖的設定

表視圖---->資料來源:(<UITableViewDataSource>)tableView:canMoveRowAtIndexPath 訊息允許資料來源移動單元格

表視圖---->委託:(<UITableViewDelegate>)tableView:moveRowAtIndexPath:toIndexPath:方法對listTeams重新排序

黨使用者拖動排序控制項來實現。




ViewController.h:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>@property (weak, nonatomic) IBOutlet UINavigationItem *navgationItem;@property (weak, nonatomic) IBOutlet UITableView *tableView;@property (strong, nonatomic) IBOutlet UITextField *txtField;@property (nonatomic, strong) NSMutableArray *listTeams;@end

ViewController.m:

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];      // 設定導覽列    // editButtonItem在視圖控制器中已經定義好,self.editButtonItem可以擷取指標    // rightBarbuttonitem為導覽列右邊的按鈕    self.navgationItem.rightBarButtonItem = self.editButtonItem;    self.navgationItem.title = @"儲存格插入和刪除";        // 設定儲存格文字框    self.txtField.hidden = YES;    self.txtField.delegate = self;        // 將當前視圖控制器分配給表視圖的委託和資料來源    self.tableView.delegate = self;    self.tableView.dataSource = self;        self.listTeams = [[NSMutableArray alloc] initWithObjects:@"黑龍江",@"吉林",@"遼寧", nil];    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark -- UIViewController 生命週期方法,用於響應視圖編輯狀態變化,判斷是否能夠進入編輯狀態!- (void)setEditing:(BOOL)editing animated:(BOOL)animated{    [super setEditing:editing animated:animated];        [self.tableView setEditing:editing animated: YES];    if (editing) {        self.txtField.hidden = NO;    }    else    {        self.txtField.hidden = YES;    }}#pragma mark -- UITableViewDataSource 資料來源協議方法- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    // 需要增加一個空白儲存格    return [self.listTeams count] + 1;}- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *CellIdentifier = @"Cell";    BOOL b_addCell;        // 判斷是否為需要插入的儲存格    if (indexPath.row == self.listTeams.count) {        b_addCell = YES;    }    else    {        b_addCell = NO;    }        // 建立重用Cell    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];    }        if (!b_addCell) {        // 普通儲存格        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  // 設定為右箭頭        cell.textLabel.text = [self.listTeams objectAtIndex:indexPath.row]; // 擷取text    }    else{        // 新插入的儲存格        // 只有進行插入操作時候,才能初始化到這個部分!        self.txtField.frame = CGRectMake(10, 0, 300, 44);        self.txtField.text = @"";        [cell.contentView addSubview:self.txtField]; // 實現文字框的注入    }    return cell;}#pragma mark -- UITableViewDelegate 委託協議方法- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    // 編輯表徵圖設定    // 是刪除還是增加    if (indexPath.row == [self.listTeams count]) {        return UITableViewCellEditingStyleInsert;    }    else{        return UITableViewCellEditingStyleNone;//重排序控制項同時存在。        //return UITableViewCellEditingStyleDelete;    }}- (BOOL) tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row == [self.listTeams count]) {        return NO;    // 除了最後一行,都加高亮    }    else{        return YES;    }}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 50;}- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{    // 先擷取目標    NSString *stringToMove = [self.listTeams objectAtIndex:sourceIndexPath.row];    // 從目標位置刪除    [self.listTeams removeObjectAtIndex:sourceIndexPath.row];    // 再目標位置插入資料    [self.listTeams insertObject:stringToMove atIndex:destinationIndexPath.row];}#pragma mark -- UITableViewDataSource協議方法- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    if (editingStyle == UITableViewCellEditingStyleDelete) {        [self.listTeams removeObjectAtIndex:indexPath.row];        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];        [self.tableView reloadData]; //??    }    else        if (editingStyle == UITableViewCellEditingStyleInsert) {            // 在最末位添加到listTeams            // 先添加資料,然後再顯示            [self.listTeams insertObject:self.txtField.text atIndex:[self.listTeams count]];            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];            // 重新載入在表格裡的資料            [self.tableView reloadData];        }}#pragma mark -- UITextFieldDelegate 委託方法,1.關閉鍵盤 2.避免鍵盤遮擋文字框-(BOOL) textFieldShouldReturn:(UITextField *)textField{    // 回收鍵盤    [textField resignFirstResponder];    return YES;}-(void) textFieldDidBeginEditing:(UITextField *)textField{    UITableViewCell *cell = (UITableViewCell *) [[textField superview] superview];    [self.tableView setContentOffset:CGPointMake(0.0, cell.frame.origin.y) animated:YES];}@end




修改儲存格——刪除、插入、移動(IOS)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.