ios开发-UISlider通过滑动添加单元格功能

#import   "XHViewController.h"

@interface   XHViewController   ()

@end

@implementation   XHViewController
{
   
  //用来记录上一次记录的值
   
  CGFloat   oldValue;
   
  //用来判断是添加还是删除
   
  BOOL     isInserted;
}

- (
void )awakeFromNib
{
    [
super   awakeFromNib ];
   
  //创建存储空间对象
   
  self . charList   = [[ NSMutableArray   alloc ]   init ];
   
  //设置默认的值是负数
   
  oldValue   = - 1 ;
}

- (
void )viewDidLoad
{
    [
super   viewDidLoad ];
// Do any additional setup after loading the view, typically from a nib.
}

- (
void )didReceiveMemoryWarning
{
    [
super   didReceiveMemoryWarning ];
   
  // Dispose of any resources that can be recreated.
}

- (
NSInteger )tableView:( UITableView   *)tableView numberOfRowsInSection:( NSInteger )section
{
   
  return   self . charList . count ;
}
-(
UITableViewCell   *)tableView:( UITableView   *)tableView cellForRowAtIndexPath:( NSIndexPath   *)indexPath
{
   
  UITableViewCell   *cell = [tableView   dequeueReusableCellWithIdentifier : @"charlistcell"   forIndexPath :indexPath ];
   
    cell.
textLabel . text   =   self . charList [indexPath. row ];
   
   
  return   cell;
}
- (
IBAction )didClickSlider:( UISlider   *)sender
{
   
  NSInteger   oldInt = ( NSInteger )( oldValue   / sender. maximumValue   *   26 );
   
  NSInteger   currentInt = ( NSInteger )(sender. value   / sender. maximumValue   *   26 );
   
    oldInt = oldInt <
  0   ?   0   : oldInt;
   
   
  //
   
  NSMutableArray   *indexPathList = [[ NSMutableArray   alloc ]   init ];
   
  NSMutableArray   *charList = [[ NSMutableArray   alloc ]   init ];
   
   
  if   ( oldValue   < sender. value )
    {
       
  for   ( NSInteger   row = oldInt; row < currentInt; row++)
        {
           
  NSIndexPath   *indexPath = [ NSIndexPath   indexPathForRow :row   inSection : 0 ];
           
  NSString     *indexString = [[ NSString   alloc ]   initWithFormat : @"%c" , 'A'   + row];
           
  if   ([ self . charList   containsObject :indexString]) {
               
  continue ;
            }
            [indexPathList 
  addObject :indexPath];
            [charList      
  addObject :indexString];
        }
       
       
        [
self . tableView   beginUpdates ];
        [
self . charList     addObjectsFromArray :charList];
        [
self . tableView   insertRowsAtIndexPaths :indexPathList   withRowAnimation : UITableViewRowAnimationAutomatic ];
        [
self . tableView   endUpdates ];
    }
   
   
  if   (sender. value   <   oldValue ) {
       
  for   ( NSInteger   row = currentInt; row < oldInt; row++)
        {
           
  NSIndexPath   *indexPath = [ NSIndexPath   indexPathForRow :row   inSection : 0 ];
           
  NSString     *indexString = [ self . charList   objectAtIndex :row];
           
  if   ([charList   containsObject :indexString])
            {
               
  continue ;
            }
            [indexPathList 
  addObject :indexPath];
            [charList      
  addObject :indexString];
        }

        [
self . tableView   beginUpdates ];
        [
self . charList     removeObjectsInArray :charList];
        [
self . tableView   deleteRowsAtIndexPaths :indexPathList    withRowAnimation : UITableViewRowAnimationAutomatic ];
        [
self . tableView   endUpdates ];
    }
   
   
  oldValue   = sender. value ;
}


@end



#import  "MYYViewController.h"

@interface  MYYViewController  ()

@end

@implementation  MYYViewController
-(
void )awakeFromNib{
    [
super  awakeFromNib ];
   
   
  self . charList  = [[ NSMutableArray    alloc ]  init ];
   

   
}
- (
void )viewDidLoad
{
    [
super  viewDidLoad ];
// Do any additional setup after loading the view, typically from a nib.
   
    [
self . charList  addObject : @"A" ];
    [
self . charList  addObject : @"B" ];
    [
self . charList  addObject : @"C" ];
    [
self . charList  addObject : @"D" ];
    [
self . charList  addObject : @"E" ];
    [
self . charList  addObject : @"F" ];
    [
self . charList  addObject : @"G" ];
    [
self . charList  addObject : @"H" ];
    [
self . charList  addObject : @"I" ];
    [
self . charList  addObject : @"J" ];

}

- (
void )didReceiveMemoryWarning
{
    [
super  didReceiveMemoryWarning ];
   
  // Dispose of any resources that can be recreated.
}

-(
IBAction )didclickRightItem:( UIBarButtonItem  *)sender;
{
 
   
  if  (sender ==  self . editItem ) {

        [
self . tableView  setEditing : YES  animated : YES ];
        [
self . navigationItem    setRightBarButtonItem : self . doneItem  animated : YES ];
    }
else {
        [
self . tableView  setEditing : NO  animated : YES ];
        [
self . navigationItem  setRightBarButtonItem : self . editItem  animated : YES ];
    }
   
   
   
   
}
- (
NSInteger )tableView:( UITableView  *)tableView numberOfRowsInSection:( NSInteger )section{
   
   
  return  self . charList . count ;
   
}



- (
UITableViewCell  *)tableView:( UITableView  *)tableView cellForRowAtIndexPath:( NSIndexPath  *)indexPath{
   
  //当storyboard  中或者xib中的tableView 上拖拽了一个模板单元格,可以直接使用这个方法来获取一个单元格对象
   
  UITableViewCell   *cell = [tableView  dequeueReusableCellWithIdentifier : @"detailviewcontroller_listcell"  forIndexPath :indexPath];
   
  //到底是使用section还是row  注意一下
    cell.
textLabel . text  =  self . charList [indexPath. row ];
   
  return  cell;
}




//用来指定indexPath是否可以进行编辑
-(
BOOL )tableView:( UITableView  *)tableView canEditRowAtIndexPath:( NSIndexPath  *)indexPath{
   
  //指定第一行不可以编辑
   
  return  !(indexPath. row  ==  0 );
   
}


//点击处于编辑状态的删除操作,触发这个方法
-(
void )tableView:( UITableView  *)tableView commitEditingStyle:( UITableViewCellEditingStyle )editingStyle forRowAtIndexPath:( NSIndexPath  *)indexPath{
   
  switch  (editingStyle) {
           
  //默认都是删除动作
       
  case  UITableViewCellEditingStyleDelete :{
           
  //下面四行是删除的动作
           
           
           
           
  //开始更新表格视图
            [
self . tableView  beginUpdates ];
           
  //编辑数据源
            [
self . charList  removeObjectAtIndex :indexPath. row ];
           
           
  //指定视图的编辑状态(删除)
            [
self . tableView  deleteRowsAtIndexPaths : @[ indexPath ]  withRowAnimation : 0 ];
           
           
  //结束更新视图
            [
self . tableView  endUpdates ];
           
  break ;
        }
       
  case  UITableViewCellEditingStyleInsert :
        {
           
           
           
  //开始更新表格视图
            [
self . tableView  beginUpdates ];
           
  //编辑数据源
            [
self . charList    insertObject : @(0) . description  atIndex : 0  ];
           
           
  //zhi
           
  NSIndexPath  *zIndexPath  =  [ NSIndexPath    indexPathForItem : 0  inSection : 0 ];
           
  //指定视图的编辑状态(添加)
            [
self . tableView  insertRowsAtIndexPaths : @[ zIndexPath ]  withRowAnimation : 100 ];
           
           
  //结束更新视图
            [
self . tableView  endUpdates ];
           
  break ;

        }
         
  default :
           
  break ;
    }
}


//指定第二行添加
-(
UITableViewCellEditingStyle )tableView:( UITableView  *)tableView editingStyleForRowAtIndexPath:( NSIndexPath  *)indexPath{
   
  return   indexPath. row  ==  1 ?
   
  UITableViewCellEditingStyleInsert :
   
  UITableViewCellEditingStyleDelete ;
}


//----------------------------------------------------------------------------------------------------------------------------------
//指定那些单元格可以移动
-(
BOOL )tableView:( UITableView  *)tableView canMoveRowAtIndexPath:( NSIndexPath  *)indexPath{
//    每一行都可以移动
   
  return  YES ;
//    return !(indexPath.row == 0);
   
}
//将单元格移动到对应的单元格
- (
void )tableView:( UITableView  *)tableView moveRowAtIndexPath:( NSIndexPath  *)sourceIndexPath toIndexPath:( NSIndexPath  *)destinationIndexPath{
  
  //交换
    [
self . charList  exchangeObjectAtIndex :sourceIndexPath. row  withObjectAtIndex :destinationIndexPath. row ];
   
}

//可以修该默认的目标
-(
NSIndexPath  *)tableView:( UITableView  *)tableView targetIndexPathForMoveFromRowAtIndexPath:( NSIndexPath  *)sourceIndexPath toProposedIndexPath:( NSIndexPath  *)proposedDestinationIndexPath{
   
   
  return  proposedDestinationIndexPath. row  ==  0 ? [ NSIndexPath  indexPathForRow : 1  inSection : 0 ]:proposedDestinationIndexPath;
   
}

你可能感兴趣的:(ios-开发)