English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Introduction
Every iOS developer knows that the swipe-to-delete feature of UITableView is very cool. Sometimes, the functionality required when swiping is not just deleting a single item; sometimes there may be other features like pinning, and in such cases, we need to customize the swipe action ourselves.
Example Code
-(NSArray<UITableViewRowAction*>*tableView: (UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Unfavorite" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSLog(@"Unfavorite click event"); }]; UITableViewRowAction *rowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Top" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSLog(@"Top button click event"); }]; rowAction.backgroundColor = RGB(215, 59, 16); NSArray *arr = @[rowAction,rowAction2]; return arr; }
Summary
We can use UITableViewRowAction to create objects. The following code block is the method to be executed after clicking. After creating the object, add it to the array. In this way, we can customize it arbitrarily, and we can also choose the color ourselves, which is very convenient. That's all for this article. I hope it can bring some help to everyone's learning or work. If you have any questions, you can leave a message for communication.