iOS.UIKit.17.UITableView -- Cells Operation

來源:互聯網
上載者:User

標籤:des   c   style   class   blog   code   

1、案例介紹:表視圖中儲存格的增加、刪除、移動,01,02

圖01圖02

2、.h

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

3、.m

#import "CQ26ViewController.h"@interface CQ26ViewController ()@end@implementation CQ26ViewController- (void)viewDidLoad{    [super viewDidLoad];        // 1、設定導覽列    self.navgationItem.rightBarButtonItem = self.editButtonItem;    self.navgationItem.title = @"儲存格插入和刪除";        // 2、設定儲存格文字框    self.txtField.hidden = YES;    self.txtField.delegate = self;        // 3、將當前視圖控制器分配給表示圖的委託和資料來源    self.tableView.delegate = self;    self.tableView.dataSource = self;        self.listTeams = [[NSMutableArray alloc] initWithObjects:@"黑龍江",@"吉林",@"遼寧", nil];}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 50;}#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{    // 1、載入cell    static NSString *CellIdentifier = @"Cell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];    }    // 2、設定cell的內容    BOOL b_addCell = (indexPath.row == self.listTeams.count);    if (!b_addCell) {        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;        cell.textLabel.text = [self.listTeams objectAtIndex:indexPath.row];    }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 UITableViewCellEditingStyleDelete;    }}#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:UITableViewRowAnimationAutomatic];    }else{        [self.listTeams insertObject:self.txtField.text atIndex:[self.listTeams count]];        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];    }    [self.tableView reloadData];}#pragma mark -- UITextFieldDelegate委託方法,關閉鍵盤- (BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];    return  YES;}#pragma mark -- UITextFieldDelegate委託方法,避免鍵盤遮擋文字框- (void) textFieldDidBeginEditing:(UITextField *)textField {    UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];    [self.tableView setContentOffset:CGPointMake(0.0, cell.frame.origin.y) animated:YES];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

4、行動程式碼

- (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 --UITableViewDelegate 協議方法- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    return  UITableViewCellEditingStyleNone;}#pragma mark -- UIViewController生命週期方法,用於響應視圖編輯狀態變化- (void)setEditing:(BOOL)editing animated:(BOOL)animated {        [super setEditing:editing animated:animated];        [self.tableView setEditing:editing animated:YES];    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.