Ios UITableViewCell自定义编辑内容

Ios UITableViewCell自定义编辑内容,ios,uitableview,Ios,Uitableview,我希望我的单元格在编辑时不仅有“删除”按钮,还有其他按钮。那么,实际的方法是什么呢?我已尝试为editingAccessoryView属性创建自己的UIView对象,但问题是该视图仅在直接编辑时显示(即单击导航栏中的“编辑”按钮),而完全忽略对单元格的滑动。另外,尝试了tableView:editActionsForRowAtIndexPath:方法,但没有成功,至少我没有找到如何使用其函数获得所需内容。提前谢谢你 您可以尝试此代码 -(NSArray *)tableView:(UITableV

我希望我的单元格在编辑时不仅有“删除”按钮,还有其他按钮。那么,实际的方法是什么呢?我已尝试为
editingAccessoryView
属性创建自己的
UIView
对象,但问题是该视图仅在直接编辑时显示(即单击导航栏中的“编辑”按钮),而完全忽略对单元格的滑动。另外,尝试了
tableView:editActionsForRowAtIndexPath:
方法,但没有成功,至少我没有找到如何使用其函数获得所需内容。提前谢谢你

您可以尝试此代码

-(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...
    }

您也可以使用此代码

.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 *cellIdentifair=@"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];
    }

斯威夫特也能用

    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]
    }

对于iOS 11,您也应该使用

-(UISwipeActionsConfiguration*)tableView:(UITableView*)tableView LeadingSwipeActionsConfiguration for RowAtIndexPath:(NSIndexPath*)indexPath
-用于右击

-(UISwipeActionsConfiguration*)表视图:(UITableView*)表视图跟踪SwipeActionsConfiguration for RowatineXpath:(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;
}

欢迎并请右键单击此问题。顺便问一下,您是否可以说,只有当用户在单元格中滑动而不是单击“编辑”按钮时,才显示这些操作的正确方式是什么?@Majotron注意,tableView:EditActions ForrowatineXPath:仅在iOS 8.0及更高版本中可用。如果您必须支持iOS7或更早版本,那么这将不起作用!在iOS 7上,您可以依赖第三方解决方案,也可以实现2个未记录的iOS 7委托-请参阅。不过,后者只允许您添加一个自定义按钮。在我刷卡时存在手势混合问题。它被调用为tableview单元格中的其他按钮单击。请帮助我上面的代码片段很混乱(您使用旧的ObjC语法混合了集合视图委托和表视图委托),并且也没有解决原始问题。