2014-10-10 79 views
0

我希望我的單元格不僅在編輯時有刪除按鈕,還有其他人。那麼,這是一個實際的方法呢?我已經嘗試爲editingAccessoryView屬性製作自己的UIView對象,但問題在於該視圖僅在直接編輯時出現(即單擊導航欄中的「編輯」按鈕),並完全忽略單元格上的滑動。另外tableView:editActionsForRowAtIndexPath:方法被嘗試,但沒有奏效,至少我沒有設法弄清楚如何得到我想要的功能。先謝謝你!UITableViewCell自定義編輯內容

回答

4

你試試這個代碼

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) 
    { 
     NSLog(@"Action to perform with Button 1"); 
    }]; 
    button.backgroundColor = [UIColor greenColor]; //arbitrary color 
    UITableViewRowAction *button2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) 
            { 
             NSLog(@"Action to perform with Button2!"); 
            }]; 
    button2.backgroundColor = [UIColor blueColor]; //arbitrary color 

    return @[button, button2]; //array with all the buttons you want. 1,2,3, etc... 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
// you need to implement this method too or nothing will work: 

} 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     return YES; //tableview must be editable or nothing will work... 
    } 
+0

非常感謝!它工作正常! – Majotron 2014-10-10 12:41:45

+0

歡迎和plz右標記點擊這個問題 – 2014-10-10 12:44:14

+0

順便說一句,你可以請說什麼是正確的方式來顯示這些行動只有當用戶滑過單元格,但沒有點擊編輯按鈕? – Majotron 2014-10-10 12:50:46

1

您CanTry也驗證碼

.H File 
{ 
    NSMutableArray *dataArray; 

} 


@property (weak, nonatomic) IBOutlet UITableView *atableView; 

@property(nonatomic,strong) NSMutableArray *dataArray; 

**`.M file`** 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.dataArray = [[NSMutableArray alloc] initWithObjects:@"Saurabh Sharma",@"Deepesh Jain",@"Ashish Sharma",@"Chandan",@"kanhaiya",@"Suchitra Bohra",@"Neha",@"Ghanshyam",nil]; 
    self.title = @"Move Rows"; 


    UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(editButton:)]; 

    [self.navigationItem setRightBarButtonItem:editButton]; 



    // Do any additional setup after loading the view from its nib. 
} 
    -(void)editButton:(UIBarButtonItem *)button 
    { 
     { 
      if(self.editing) 
      { 
       [super setEditing:NO animated:NO]; 
       [atableView setEditing:NO animated:NO]; 
       [atableView reloadData]; 
       [self.navigationItem.rightBarButtonItem setTitle:@"Edit"]; 
       [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain]; 
      } 
      else 
      { 
       [super setEditing:YES animated:YES]; 
       [atableView setEditing:YES animated:YES]; 
       [atableView reloadData]; 
       [self.navigationItem.rightBarButtonItem setTitle:@"Done"]; 
       [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; 

    } 

     } 

    } 

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     return 1; 

    } 

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return [dataArray count]; 
    } 

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

     static NSString *[email protected]"cellIdentifair"; 
     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifair]; 

     if(cell==nil) 
     { 
      cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifair]; 
     } 


     cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row]; 

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     return cell; 

    } 

    #pragma delete row in table view 
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { 
     return 10; // This is the minimum inter item spacing, can be more 
    } 
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     return UITableViewCellEditingStyleDelete; 
    } 

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     return TRUE; 
    } 

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     [tableView beginUpdates]; 
     [dataArray removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop]; 
     [tableView endUpdates]; 
     NSLog(@"Methods called when row is deleted"); 
    } 

    #pragma Move Table View Delegte 


    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     return YES; 
    } 
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
    { 
     NSString *item = [self.dataArray objectAtIndex:fromIndexPath.row]; 
     [self.dataArray removeObject:item]; 
     [self.dataArray insertObject:item atIndex:toIndexPath.row]; 
    } 
+0

歡迎和Thankuuu – 2014-10-10 13:12:25

+0

歡迎和Thankuuu – 2014-10-10 13:15:52

+0

上面的代碼片段很混亂(你正在混合集合視圖委託和表視圖委託,使用舊的ObjC語法),並沒有解決原始問題。 – 2014-11-27 10:46:30

1

作品在斯威夫特也是如此。

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { 
     var completionButton = UITableViewRowAction(style: .Normal, 
                title: "Complete", 
                handler: {(action, index) in 
                 /* Completion Code */ 
                 println("Complete")}) 
     completionButton.backgroundColor = .lightGrayColor() 
     return [completionButton] 
    } 
0

也適用於iOS 11,你應該使用

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath - 右輕掃

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath - 爲向左掃

對於前:

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UIContextualAction* action = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"Title" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { 
     // Do something 
     completionHandler(YES); 
    } 

    UISwipeActionsConfiguration * actionsConfiguration = [UISwipeActionsConfiguration configurationWithActions:@[action]]; 
    return actionsConfiguration; 
}