ios-表視圖-demo7-cell的編輯

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   os   

////  RootTableViewController.m//  editcell////  Created by  liyang on 14-4-29.//  Copyright (c) 2014年 liyang. All rights reserved.//#import "RootTableViewController.h"@interface RootTableViewController ()@end@implementation RootTableViewController- (id)initWithStyle:(UITableViewStyle)style{    self = [super initWithStyle:style];    if (self) {                    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];        _fontarrary=[NSMutableArray arrayWithArray:[UIFont familyNames]];     self.navigationItem.rightBarButtonItem=self.editButtonItem;//這個是給這個導航控制器加上一個按鈕,並且這個按鈕還是主動調用了下面-(void)setEditing:(BOOL)editing animated:(BOOL)animated這個方法,這個方法和UITableView有個同名方法,但是是不一樣的。調用這個功能就是這個editButtonItem來實現的}-(void)setEditing:(BOOL)editing animated:(BOOL)animated{//這個是繼承了父類別檢視控制器的方法    if (self.tableView.editing) {        [self.tableView setEditing:NO animated:YES];    }else {        [self.tableView setEditing:YES animated:YES];    }}//使我們的表視圖處於編輯或者非編輯狀態- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{//這個表示哪些行可以進行編輯(增,刪,移動)    NSLog(@"canEditRowAtIndexPath");    if (indexPath.row==0) {        return NO;    }    return YES;}- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{//通過代理方法來進行判斷編輯風格        if (indexPath.row==1) {          NSLog(@"UITableViewCellEditingStyleInsert");        return UITableViewCellEditingStyleInsert;    }    NSLog(@"UITableViewCellEditingStyleDelete");    return UITableViewCellEditingStyleDelete;}//編輯的樣式int count=0;// Override to support editing the table view.- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    if (editingStyle == UITableViewCellEditingStyleDelete) {        [_fontarrary removeObjectAtIndex:indexPath.row];//刪除的時候要先刪除資料        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];    } else if (editingStyle == UITableViewCellEditingStyleInsert) {        NSString *font_new=[NSString stringWithFormat:@"newfont%d",count];        [_fontarrary insertObject:font_new atIndex:indexPath.row+1];        NSIndexPath *_aaindexpath=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];        [tableView insertRowsAtIndexPaths:@[_aaindexpath] withRowAnimation:UITableViewRowAnimationRight];        count++;    }}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [_fontarrary count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellidentifier=@"cell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier];        if (cell==nil) {        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];        UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];        lable.backgroundColor=[UIColor purpleColor];        lable.tag=101;        [cell.contentView addSubview:lable];    }    UILabel *lable=(UILabel *)[cell.contentView viewWithTag:101];    lable.text=_fontarrary[indexPath.row];    return cell;}- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{        NSString *text=[_fontarrary objectAtIndex:fromIndexPath.row];    [_fontarrary removeObjectAtIndex:fromIndexPath.row];    [_fontarrary insertObject:text atIndex:toIndexPath.row];}//移動結束後,為的是修改資料// Override to support conditional rearranging of the table view.- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{     NSLog(@"canMoveRowAtIndexPath");    // Return NO if you do not want the item to be re-orderable.    return YES;}//總結:首先cell展示出來的時候就會判斷這個cell是否可以編輯,然後編輯的時候就會先判斷是否可編輯,然後調用判斷此cell的編輯風格,然後判斷此 cell是否可以移動,咋一看,為毛多此一舉,在出現cell的時候就去判斷是否可編輯,我想,我認為是出於資料結構中的,空間換取效率吧(純屬揣測),最後完成提交編輯的時候還會調用一次,是否可編輯。@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.