【问题标题】:swipe to delete option breaking the tableviewcell functionality滑动以删除破坏 tableviewcell 功能的选项
【发布时间】:2014-03-26 12:29:32
【问题描述】:

所以我一直在创建一个 tableviewcontroller 来处理我的 tableview 及其 tableviewcells.. 这是代码--

ItemsViewController.h

   #import <UIKit/UIKit.h>
   #import "DetailViewController.h"

    @interface ItemsViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>

    -(IBAction)addNewItem:(id)sender;

    @end

ItemsViewController.m

    @implementation ItemsViewController

   -(id) init{
      self= [super initWithStyle:UITableViewStyleGrouped];
         if (self) {
            UINavigationItem *n= [self navigationItem];
              [n setTitle:@"Homepwner"];


             for (int i=0; i<5; i++) {
               [[BNRItemStore sharedStore] createItem];
             }

        }
        return self;
     }
    - (id)initWithStyle:(UITableViewStyle)style
     {
           return [self init];

      }
   - (void)viewDidLoad
      {
         NSLog(@"ItemsView loaded");
         [super viewDidLoad];

           UIBarButtonItem *bbi= [[UIBarButtonItem alloc]    initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem:)];//Target-Action pair..
         self.navigationItem.rightBarButtonItem= bbi;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.

        self.navigationItem.leftBarButtonItem = self.editButtonItem;
  }
  -(void)viewWillAppear:(BOOL)animated{
       NSLog(@"ItemsView appearing");
       [super viewWillAppear:animated];
       [[self tableView] reloadData];
   }

   -(IBAction)addNewItem:(id)sender{
       BNRItem *newItem= [[BNRItemStore sharedStore] createItem];
       NSLog(@"%d",[[[BNRItemStore sharedStore] allItems] count]);
       int newRow = [[[BNRItemStore sharedStore] allItems] indexOfObject:newItem];
       NSIndexPath *ip= [NSIndexPath indexPathForRow:newRow inSection:0];
       [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationTop];
    }

   //Row Selection
   -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
         if(indexPath.row<[[BNRItemStore sharedStore] allItems].count){
            NSLog(@"Row# %d selected",indexPath.row);
            DetailViewController *detailViewController= [[DetailViewController alloc] init];
             NSArray *items= [[BNRItemStore sharedStore] allItems];

            BNRItem *item= [items objectAtIndex:indexPath.row];
           detailViewController.item=item;//BNRItem at the selected indexPath.

            //Push it onto the top of the navigation controller's stack
           [[self navigationController] pushViewController:detailViewController animated:YES];
       }
       else{
          return;
     }
   }

   #pragma mark - Table view data source

     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
       {
            NSLog(@"Test1");
           return 1;
        }

     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
       {
          //#warning Incomplete method implementation.
            // Return the number of rows in the section.
             NSLog(@"Current section- %d",section);

          return [[[BNRItemStore sharedStore] allItems] count]+1 ;//no. of rows (5)+'No more items' row
        }

       - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
         {
             NSLog(@"hello");
           //    NSLog(@"%@",headerView);
           static NSString *CellIdentifier = @"Cell";
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

          // Configure the cell...
           if (!cell) {
              cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
           }
            if (indexPath.row<[[BNRItemStore sharedStore] allItems].count) {
              [[cell textLabel] setText:[[[[BNRItemStore sharedStore] allItems] objectAt  Index:indexPath.row] description]];
           }
           else if (indexPath.row== [[BNRItemStore sharedStore] allItems].count)
            {
                [[cell textLabel] setText:@"No more items"];
            }
            return cell;
         }


           // Override to support conditional editing of the table view.
     - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
       {
           // Return NO if you do not want the specified item to be editable.
             if (self.editing) {
                 NSLog(@"Editing is on");
                 self.navigationItem.rightBarButtonItem.enabled=NO;
                 if (indexPath.row==[[BNRItemStore sharedStore] allItems].count) {
                     [tableView cellForRowAtIndexPath:indexPath].hidden=YES;
                     return NO;// Do not make 'No more items' editable.
                }
       }
       else{//When the user is out of editing mode
           NSLog(@"editing done");
           [self.tableView cellForRowAtIndexPath:indexPath].hidden=NO;
           self.navigationItem.rightBarButtonItem.enabled=YES;
       }

        return YES;
    }



      // Override to support editing the table view.
        - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
     {
         NSLog(@"%@",NSStringFromSelector(_cmd));
           if (editingStyle == UITableViewCellEditingStyleDelete) {
              NSLog(@"Deletion is on");
            // Delete the row from the data source
              NSArray *items= [[BNRItemStore sharedStore] allItems];
              BNRItem *p= [items objectAtIndex:[indexPath row]];
              [[BNRItemStore sharedStore] removeItem:p];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
       }
          else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
       }
    }



        // Override to support rearranging the table view.
      - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
        {
          [[BNRItemStore sharedStore] moveItemAtIndex:fromIndexPath.row toIndex:toIndexPath.row];
        }

     -(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath{
     if (proposedIndexPath.row==[[BNRItemStore sharedStore] allItems].count) {// 'No more items' row
         return [NSIndexPath indexPathForRow:proposedIndexPath.row-1 inSection:0];
      }
         return proposedIndexPath;
    }
  @end

当编辑模式打开时,即当我点击导航栏上的编辑按钮时,添加按钮变灰并且“没有更多项目”行被隐藏。退出编辑模式后(通过点击“完成”按钮),添加按钮变为可选状态,并且“没有更多项目”行取消隐藏。当我通过首先进入编辑模式然后退出编辑模式执行编辑时,这一切都很好。但是当我不使用上述方式时,我使用 滑动删除 功能,视图并没有实现上述功能(即 Ungraying/Re-enable 添加按钮一旦我使用滑动删除的东西完成删除。当我刷过一个单元格(以便在右侧调出删除按钮单元格并在此过程中将“添加”按钮变灰)并选择不按“删除”按钮,只需重新点击单元格(这样删除按钮就会消失) 添加按钮不会自行更新以启用..该怎么办。请先仔细检查我的代码并给出相应的建议..

【问题讨论】:

    标签: ios objective-c uitableview cocoa-touch uikit


    【解决方案1】:

    使用这个 tableView 委托方法

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    // Return NO if you do not want the specified item to be editable.
    return YES;
    }
    
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    return UITableViewCellEditingStyleDelete;
    }
    
    
     - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath 
     {
     return NO;
     }
    

    请使用Apple Library- UItableView

    【讨论】:

    • 我可以看到自己使用你建议的第三种方法。这是一个方便的功能,不会使“没有更多项目”行变得不合适,而所有其他与数据相关的单元格都处于编辑模式。尽管如此,它不是解决我的问题的方法。问题是滑动删除功能。当我在一个单元格上滑动时,添加按钮变灰并出现删除按钮。到这里为止,一切都很好。我想要的是按下删除按钮的那一刻,添加按钮应该重新启用。你提到的第一种方法已经在我的代码中实现了。请仔细检查我的代码。
    • 当我在一个单元格上执行滑动操作并出现删除按钮时,我选择通过重新点击单元格来关闭它,这样按钮就会消失并且单元格退出编辑模式。但是“添加”按钮没有重新启用。但是,当我在单元格处于编辑模式时按下“完成”按钮时,它会正常工作,因为“添加”按钮被重新启用。我已经在顶部的代码中包含了何时使“添加”按钮变灰以及何时重新启用它的代码。它工作正常,除非我使用“滑动删除”功能。帮我解决这个问题
    猜你喜欢
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2013-08-25
    • 2015-07-06
    • 1970-01-01
    相关资源
    最近更新 更多