One of the new iOS 11 features is the addition of the UITableViewCell swipeable action targets. While this new feature is a great help for content creators, it can also be a bit of a hassle. For example, you might be creating a tutorial about how to get your app’s scroll view to scroll automatically, but you don’t want to affect the scroll view’s content view or content view controller.
Today I’m going to show you an alternative way to do a UITableViewCell swipe up to trigger some actions. The swipe up gesture is used by some apps to trigger actions like copying something, sending an email, or starting a new screen, but I wanted to do this in a way that is more practical and less complicated.
As I always tell my students, once you understand the basics of programming, you’ve pretty much got the whole thing down. You just have to learn how to apply them.
MCSwipeTableViewCell
I’m trying to show how to implement a UITableViewCell, similar to what we see in the very well made Mailbox app for iOS.
Overview
Output mode
The MCSwipeTableViewCellModeExit is the original behavior we can see in the mailbox application. Move the mouse over the cell to make it disappear. At ease in destructive modes.
Switch mode
MCSwipeTableViewCellModeSwitch is a new behavior I am introducing. The cell returns after selecting a state, so you can save the cell. It is convenient to quickly change options.
API reference
See the MCSwipeTableViewCell.h header file for a complete listing of the class’s properties.
Use
– (UITableViewCell *)tableView :(UITableView *)tableView cellForRowAtIndexPath :(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @Cell ;
MCSwipeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier] ;
if (!cell) {
cell = [[MCSwipeTableViewCell allocate] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
// Remove the insertion of the separator from iOS 7.
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
cell.separatorInset = UIEdgeInsetsZero;
}
// Set the background color of the cell.
cell.contentView.backgroundColor = [UIColor whiteColor];
}
// Configure views and colors.
UIView *checkView = [self viewWithImageName:@check];
UIColor *greenColor = [UIColor colorWithRed:85.0 / 255.0 green:213.0 / 255.0 blue:80.0 / 255.0 alpha:1.0] ;
UIView *crossView = [self viewWithImageName:@cross];
UIColor *redColor = [UIColor colorWithRed:232.0 / 255.0 green:61.0 / 255.0 blue:14.0 / 255.0 alpha:1.0] ;
UIView *clockView = [self viewWithImageName:@clock];
UIColor *yellowColor = [UIColor colorWithRed:254.0 / 255.0 green:217.0 / 255.0 blue:56.0 / 255.0 alpha:1.0] ;
UIView *listView = [self viewWithImageName:@list];
UIColor *brownColor = [UIColor colorWithRed:206.0 / 255.0 green:149.0 / 255.0 blue:98.0 / 255.0 alpha:1.0] ;
// Set the default color of the resting state as the background color of the TableView.
[cell setDefaultColor:self.tableView.backgroundView.backgroundColor];
[cell.detailTextLabel setText:@Swipe to switch] ;
// Add state-based gestures.
[cell setSwipeGestureWithView:checkView color:greenColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState1 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState, MCSwipeTableViewCellMode mode) {
NSLog(@Did swipe checkmark cell);
}]
[cell setSwipeGestureWithView:crossView color:redColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState2 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) {
NSLog(@Did swipe Cross cell);
}]
[cell setSwipeGestureWithView:clockView color:yellowColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState3 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) {
NSLog(@Did swipe clock cell);
}]
[cell setSwipeGestureWithView:listView color:brownColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState4 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) {
NSLog(@Did swipe List cell);
}]
return the cell;
}
Delegate
MCSwipeTableViewCell has a number of delegated methods for tracking user behavior. See the header file for all methods of MCSwipeTableViewCellDelegate.
@Interface MCTableViewController ()
#pragma mark – MCSwipeTableViewCellDelegate
// Called when the user starts swiping a cell.
– (void)swipeTableViewCellDidStartSwiping :(MCSwipeTableViewCell *)cell ;
// Called when the user has finished swiping a cell.
– (void)swipeTableViewCellDidEndSwiping :(MCSwipeTableViewCell *)cell ;
// Called when the cursor passes.
– (void)swipeTableViewCell :(MCSwipeTableViewCell *)cell didSwipeWithPercentage :(CGFloat)percentage ;
Cell abnormality
In MCSwipeTableViewCellModeExit, you may want to delete a cell with a nice fade-in animation. The following rules will give you an idea of how to proceed:
cell setSwipeGestureWithView:crossView color:redColor mode:MCSwipeTableViewCellModeExit state:MCSwipeTableViewCellState2 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) {
NSLog(@Deed swipe over cell 😉
// Code to remove a cell…
}];
You can also ask for confirmation before deleting a cell:
__weak MCTableViewController *weakSelf = self ;
cell setSwipeGestureWithView:crossView color:redColor mode:MCSwipeTableViewCellModeExit state:MCSwipeTableViewCellState1 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) {
NSLog(@Deed swipe over cell 😉
__strong MCTableViewController *strongSelf = weakSelf ;
strongSelf.cellToDelete = cell ;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@Delete ?
Message:@Are you sure you want to delete the cell?
delegate:self
cancelButtonTitle:@No
otherButtonTitles:@Yes, nil];
[alertView show];
}]
Then edit the UIAlertView action:
#pragma mark – UIAlertViewDelegate
– (void)alertView :(UIAlertView *)alertView clickedButtonAtIndex :(NSInteger)buttonIndex {
// No
if (buttonIndex == 0) {
[_cellToDelete swipeToOriginWithCompletion:^{
NSLog(@Swiped back);
}];
_cellToDelete = nil;
}
// Yes
otherwise {
// Code to delete the cell….
}
}
There is also an example in the demo project, I recommend you take a look at that.
Percentage change in travel
If the default trigger limits do not meet your needs, you can change them with the properties firstTrigger (default: 25%) and secondTrigger (default: 75%).
cell.firstTrigger = 0.1 ;
cell.secondTrigger = 0.5 ;
Reset cell position
You can return a cell to its original position when using MCSwipeTableViewCellModeExit with the -swipeToOriginWithCompletion: method:
[cell swipeToOriginWithCompletion:^{NSLog(@Cell swipe back !);
}] ;
Setting
CocoaPods
CocoaPods is a dependency manager for Cocoa projects.
$ gem install cocoapods
To integrate MCSwipeTableViewCell into your Xcode project with CocoaPods, specify it in your podfile:
pod MCSwipeTableViewCell, ~> 2.1.4.
Then run the following command:
$ pod install
Carthago
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate MCSwipeTableViewCell in your Xcode project with Carthage, specify it in your cartfile:
github alikaragoz/MCSwipeTableViewCell >= 2.1.4
Compatibility
This library does not support autopartitioning, so you must disable autopartitioning in the xib properties.
Requirements
Ali Karagoz
GitHub
https://github.com/alikaragoz/MCSwipeTableViewCellThe iOS API provides an easy way to have content change when the swipe action is performed. Unfortunately, this approach doesn’t work for all cases where the data source (e.g. from an NSFetchedResultsController) is not in the table view.. Read more about uicontextualaction accessibility identifier and let us know what you think.
Related Tags:
what is swipecellkitswipe button ios swiftuicontextualactionuicontextualaction custom viewuitableviewcell swipe custom button swiftuitableviewcell swipe to delete custom button with image swift,People also search for,Privacy settings,How Search works,uicontextualaction,uicontextualaction custom view,uicontextualaction image not showing,trailingswipeactionsconfigurationforrowat,uicontextualaction accessibility identifier,uicontextualaction icon,uicontextualaction handler,uicontextualaction image tint color