iOS學習筆記——表視圖二(編輯)

來源:互聯網
上載者:User

表視圖的可編輯屬性,即對錶的每一個單元UITableViewCell進行操作的。總共有5種方法,其中4種方法在UITableViewDataSource協議裡,最後一種方法在UITableViewDelegate協議中。

在.h檔案中添加協議,建立對象:

@interface LinViewController : UIViewController//建立表視圖對象@property (retain, nonatomic) UITableView *mTableView;//建立數組對象,儲存表視圖資料@property (retain, nonatomic) NSMutableArray *mArray;@end
在.m檔案裡添加實現的方法:

@implementation LinViewController//重寫初始化方法,原方法在AppDeledate.m檔案中- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])    {        //設定導覽列的名稱        self.navigationItem.title = @"首頁";        //設定導覽列的右按鈕,並關聯方法        UIBarButtonItem *pButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"BeginEdit" style:UIBarButtonItemStylePlain target:self action:@selector(tableEdit:)];        //把按鈕添加到導覽列上        self.navigationItem.rightBarButtonItem = pButtonItem;    }    return self;}//導覽列上按鈕對應的方法,實現名稱的切換- (void)tableEdit:(id)sender{    //三目運算,根據表視圖是否可編輯的狀態設定按鈕的標題    [sender setTitle:[self.mTableView isEditing]?@"BeginEdit":@"EndEdit"];    //設定表視圖的編輯狀態,在可編輯與不可編輯之間切換    [self.mTableView setEditing:![self.mTableView isEditing]];}//釋放建立的對象- (void)dealloc{    [_mTableView release];    [_mArray release];    [super dealloc];}//視圖的載入- (void)viewDidLoad{    [super viewDidLoad];    //初始化表視圖對象self.mTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];    //設定委派物件,表視圖的委託    self.mTableView.dataSource = self;    self.mTableView.delegate = self;    //把表視圖添加到當前的視圖中    [self.view addSubview:self.mTableView];    //初始化數組對象    self.mArray = [NSMutableArray arrayWithCapacity:5];    //給數組中元素設定為字串類型    for (int i = 0; i < 5; i++)    {        NSString *pStr = [NSString stringWithFormat:@"%d",i];        [self.mArray addObject:pStr];    }}#pragma mark---UITableVeiwDataSource---表視圖的協議- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    //擷取表視圖中需要載入表的行數    return [self.mArray count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    //設定一個靜態字串做標籤,(靜態防止局部變數多次建立,提高效率,節約記憶體)    static NSString * identifer = @"identifer";    //建立一個cell對象(利用表示圖可以複用的機制)    UITableViewCell * Cell = [tableView dequeueReusableCellWithIdentifier:identifer];    //如果cell為空白,就建立一個新的出來    if (nil == Cell)    {        Cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];    }    //擷取當前表視圖行數    NSInteger row = [indexPath row];    //把數組中對應元素傳遞給視圖的文本    Cell.textLabel.text = [self.mArray objectAtIndex:row];    return Cell;}//設定表的編輯屬性,5種方法//設定當前title是否可編輯屬性,第一個方法- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    if ([indexPath row] == 0)    {        return NO;    }    return YES;}#pragma mark---UITableViewDelegate協議的方法,判斷編輯方式,此處只是用這個方法//設定表視圖的編輯方式,包括刪除與插入,第二個方法- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    if ([indexPath row]%2)    {        return UITableViewCellEditingStyleInsert;    }    return UITableViewCellEditingStyleDelete;}//確定表視圖的編輯方式,第三個方法- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    //判斷編輯方式為刪除    if (editingStyle == UITableViewCellEditingStyleDelete)    {        //通過行數移除數組中元素        [self.mArray removeObjectAtIndex:[indexPath row]];        //表視圖開始更新        [self.mTableView beginUpdates];        //擷取數組元素,在表視圖中刪除        [self.mTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];        //表視圖終止更新        [self.mTableView endUpdates];    }    else if (editingStyle == UITableViewCellEditingStyleInsert)    {        //在數組對應行數的位置添加新元素        [self.mArray insertObject:@"New Cell" atIndex:[indexPath row]];        //表視圖開始更新        [self.mTableView beginUpdates];        //擷取數組元素,在表視圖中插入        [self.mTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];        //表視圖終止更新        [self.mTableView endUpdates];    }}//設定表視圖中行數的可以動屬性,第四個方法- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{    if ([indexPath row] == 2)    {        return NO;    }    return YES;}//編輯表視圖的移動方式第五個方法- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{    //擷取移動前的行    NSInteger fromRow = [sourceIndexPath row];    //擷取要移動到的行    NSInteger toRow = [destinationIndexPath row];    //擷取數組中移動前下標對應的對象    id object = [self.mArray objectAtIndex:fromRow];    //從數組中刪除    [self.mArray removeObjectAtIndex:fromRow];    //在新的位置插入    [self.mArray insertObject:object atIndex:toRow];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];}@end



聯繫我們

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