We all find solutions for our problems but the difference is made by the time consumed!!!
Displaying icons on cell swipe is an easy task with this tutorial. Displaying icons on cell swipe is something we always come across while fulfilling clients basic demands. With some easy steps we can do it.
So, let's Start. . .
2. Drag and drop these files from the downloaded folder
- MGSwipeButton.h
- MGSwipeButton.m
- MGSwipeTableCell.h
- MGSwipeTableCell.m
- TestData.h
- TestData.m
3. Open the .h file of the custom cell of your table view.
4. Import #import "MGSwipeTableCell.h" and change the class of the cell to "MGSwipeTableCell".
5. We are almost done.We need to configure the icons on cell swipe and we are ready to RUN the application.
Open the .h file of your table view and Import
#import "MGSwipeTableCell.h"
and also the delegate method MGSwipeTableCellDelegate.
Open the .m file of your table view and add this method configuring
the number of buttons you seek and the image you want to set.
-(NSArray *) createRightButtons: (int) number
{
NSMutableArray * result = [NSMutableArray array];
for (int i = 0; i < number; ++i)
{
MGSwipeButton * button = [MGSwipeButton buttonWithTitle:nil icon:[UIImage imageNamed:@"trashImg"] backgroundColor:[UIColor whiteColor] callback:^BOOL(MGSwipeTableCell * sender)
{
NSLog(@"Convenience callback received (right).");
BOOL autoHide = i != 0;
return autoHide; }];
[result addObject:button];
}
return result;
}
6. Add these delegate methods that will be called when the button is clicked . So add them too.
#if TEST_USE_MG_DELEGATE
-(NSArray*) swipeTableCell:(MGSwipeTableCell*) cell swipeButtonsForDirection:(MGSwipeDirection)direction
swipeSettings:(MGSwipeSettings*) swipeSettings expansionSettings:(MGSwipeExpansionSettings*) expansionSettings;
{
TestData * data = [tests objectAtIndex:[_tableView indexPathForCell:cell].row];
swipeSettings.transition = data.transition;
if (direction == MGSwipeDirectionLeftToRight) {
expansionSettings.buttonIndex = data.leftExpandableIndex;
expansionSettings.fillOnTrigger = NO;
return [self createLeftButtons:data.leftButtonsCount];
}
else {
expansionSettings.buttonIndex = data.rightExpandableIndex;
expansionSettings.fillOnTrigger = YES;
return [self createRightButtons:data.rightButtonsCount];
}
}
#endif
-(BOOL) swipeTableCell:(MGSwipeTableCell*) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion
{
NSLog(@"Delegate: button tapped, %@ position, index %d, from Expansion: %@",
direction == MGSwipeDirectionLeftToRight ? @"left" : @"right", (int)index, fromExpansion ? @"YES" : @"NO");
if (direction == MGSwipeDirectionRightToLeft && index == 0) {
//delete button
NSIndexPath * path = [autolayoutTableView indexPathForCell:cell];
return NO; //Don't autohide to improve delete expansion animation
}
return YES;
}
7. Add this statement to cellForRowAtIndexPath method:
cell.rightButtons = [self createRightButtons:1];
You need to pass the count of the buttons you want to appear on cell swipe. We are all set to RUN the application.
Apart from this, these methods can also be used to configure the buttons:
+(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color;
+(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color padding:(NSInteger) padding;
+(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color insets:(UIEdgeInsets) insets;
+(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color callback:(MGSwipeButtonCallback) callback;
+(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color padding:(NSInteger) padding callback:(MGSwipeButtonCallback) callback;
+(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color insets:(UIEdgeInsets) insets callback:(MGSwipeButtonCallback) callback;
+(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color;
+(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color padding:(NSInteger) padding;
+(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color insets:(UIEdgeInsets) insets;
+(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color callback:(MGSwipeButtonCallback) callback;
+(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color padding:(NSInteger) padding callback:(MGSwipeButtonCallback) callback;
+(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color insets:(UIEdgeInsets) insets callback:(MGSwipeButtonCallback) callback;
Hope you will not find any difficulty implementing Buttons on TableViewCell Swipe.
Feel free to post your suggestions and your contribution to improving this tutorial will be highly appreciated .
Thanks for visiting and do help your friends to save their time.