zoukankan      html  css  js  c++  java
  • ios-表视图-demo7-cell的编辑

    //
    //  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
    1.这里只记录一些学习笔记 2.这里只记录一些学习心得,如果心得方向有错,请留言 2.这里只记录一些日记(只为提升英语,暂时有点忙,等转行了开始写)
  • 相关阅读:
    软件工程个人作业1
    构建之法问题以及阅读计划
    软件工程概论课后作业1
    动手动脑7补
    学习进度条06
    软件工程课堂测试07(结对开发)
    团队介绍
    软件工程结对作业02(借鉴版)
    软件工程结对作业02(原创版)
    构建之法阅读笔记06
  • 原文地址:https://www.cnblogs.com/liyang31tg/p/3700268.html
Copyright © 2011-2022 走看看