如何在UITableViewCell中添加左右全滑及对应功能(Objective-C)
Hey there! Let's break down your two UITableViewCell swipe gesture questions clearly, with practical Objective-C code examples tailored to your needs.
You've got two solid options here—using Apple's native swipe system (the easiest, most consistent with iOS design) or building a custom gesture recognizer for full control.
Option 1: Use Native UITableView Swipe Actions (Recommended)
This method lets users swipe left (from right to left on the cell) to reveal custom action buttons, and even support full-swipe to trigger the first action automatically.
iOS 11+ Code Example:
// Implement this UITableViewDelegate method - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath { // Create your left-swipe action (e.g., "Mark as Read") UIContextualAction *markReadAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"Mark Read" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { // Put your functionality logic here NSLog(@"Marked row %ld as read", indexPath.row); // Refresh the cell or update data as needed [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; // Always call the completion handler when done completionHandler(YES); }]; markReadAction.backgroundColor = [UIColor systemGreenColor]; // Configure the swipe behavior UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[markReadAction]]; // Allow full-swipe to trigger the first action immediately config.performsFirstActionWithFullSwipe = YES; return config; }
iOS 8-10 Compatibility Code:
// Enable edit mode for cells - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } // Define left-swipe actions - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *markReadAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Mark Read" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { // Your functionality here NSLog(@"Marked row %ld as read", indexPath.row); // Exit edit mode after action [tableView setEditing:NO animated:YES]; }]; markReadAction.backgroundColor = [UIColor greenColor]; return @[markReadAction]; }
Option 2: Custom Swipe Gesture Recognizer
If you need full control over the swipe behavior (no action buttons, just direct trigger on swipe), add a gesture recognizer directly to your cell:
// In your custom UITableViewCell's initialization method - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Add left-swipe gesture (direction = Left means swipe from right to left) UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeTriggered:)]; leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft; [self addGestureRecognizer:leftSwipe]; } return self; } // Handle the swipe trigger - (void)leftSwipeTriggered:(UISwipeGestureRecognizer *)gesture { // Use a delegate to pass the event to your view controller if ([self.delegate respondsToSelector:@selector(cellDidTriggerLeftSwipe:)]) { [self.delegate cellDidTriggerLeftSwipe:self]; } } // In your view controller's delegate method - (void)cellDidTriggerLeftSwipe:(CustomTableViewCell *)cell { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; NSLog(@"Left swipe triggered on row %ld", indexPath.row); // Execute your custom logic here }
Depending on how you implemented the left swipe, here's how to add the right swipe:
If You Used Native UITableView Swipe Actions
iOS 11+ supports both left and right swipe actions natively—just add the leading swipe configuration method:
// Implement this UITableViewDelegate method for right-swipe (swipe from left to right) - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath { UIContextualAction *shareAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"Share" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { // Your share functionality here NSLog(@"Sharing row %ld content", indexPath.row); completionHandler(YES); }]; shareAction.backgroundColor = [UIColor systemBlueColor]; UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[shareAction]]; config.performsFirstActionWithFullSwipe = YES; return config; }
The system automatically handles gesture conflicts between left and right swipes—no extra work needed!
If You Used a Custom Gesture Recognizer
Just add a second gesture recognizer for right swipes to your custom cell:
// In your custom UITableViewCell's initialization method - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Existing left-swipe gesture (keep this) UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeTriggered:)]; leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft; [self addGestureRecognizer:leftSwipe]; // Add right-swipe gesture (direction = Right means swipe from left to right) UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeTriggered:)]; rightSwipe.direction = UISwipeGestureRecognizerDirectionRight; [self addGestureRecognizer:rightSwipe]; } return self; } // Handle right-swipe trigger - (void)rightSwipeTriggered:(UISwipeGestureRecognizer *)gesture { if ([self.delegate respondsToSelector:@selector(cellDidTriggerRightSwipe:)]) { [self.delegate cellDidTriggerRightSwipe:self]; } } // In your view controller's delegate method - (void)cellDidTriggerRightSwipe:(CustomTableViewCell *)cell { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; NSLog(@"Right swipe triggered on row %ld", indexPath.row); // Execute your custom logic here }
Quick Note:
If you run into gesture conflicts with custom gestures, use requireGestureRecognizerToFail: to set priority (e.g., make the swipe gesture wait for any tap gestures to fail first).
内容的提问来源于stack exchange,提问作者vinoth

