iOS 8之前使用如下方式自定義UITableView左劃后顯示的文字,不過(guò)該樣式太單一了,而且只能顯示一個(gè):
- (nullable NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
iOS8之后,只需要一個(gè)tableView代理方
tableView:editActionsForRowAtIndexPath:和一個(gè)類(lèi)UITableViewRowAction就可以了。 rowAction可以設(shè)置style、title、backgroundColor、backgroundEffect,在block中實(shí)現(xiàn)點(diǎn)擊事件
注意 : title 可以設(shè)置,但是title的文字顏色沒(méi)辦法設(shè)置。
其中不修改backgroundColor時(shí),backgroundColor的顏色是由style決定的.
- UITableViewRowActionStyleDestructive時(shí)是紅色的刪除樣式,
- UITableViewRowActionStyleNormal時(shí)是灰色樣式,類(lèi)似于微信好友列表左劃后的“備注”。
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
當(dāng)實(shí)現(xiàn)該代理方法后,以下的這個(gè)代理方法就不執(zhí)行了:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
//設(shè)置刪除按鈕
UITableViewRowAction *deleteRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaul ttitle:@"刪除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
[self.arrremoveObjectAtIndex:indexPath.row];
[self.tableViewdeleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}];
//設(shè)置收藏按鈕
UITableViewRowAction *collectRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormal title:@"收藏"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"收藏"message:@"收藏成功"delegate:selfcancelButtonTitle:@"確定"otherButtonTitles:nil,nil];
[alertView show];
}];
//設(shè)置置頂按鈕
UITableViewRowAction *topRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaul ttitle:@"置頂"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
[self.arrinsertObject:self.arr[indexPath.row]atIndex:0];
[self.arrremoveObjectAtIndex:indexPath.row +1];
NSIndexSet *set = [NSIndexSetindexSetWithIndex:0];
[tableView reloadSections:setwithRowAnimation:UITableViewRowAnimationTop];
/**
* tableView 刷新時(shí)如果確定哪個(gè)row 或者section就刷新對(duì)應(yīng)的,不要走reloadData
*/
}];
topRowAction.backgroundColor = [UIColorblueColor];
collectRowAction.backgroundEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleDark];
return @[deleteRowAction,collectRowAction,topRowAction];
}
參考 :https://blog.csdn.net/mlcldh/article/details/54947302
https://blog.csdn.net/textfielddelegate/article/details/50599151
https://www.cnblogs.com/zj901203/p/4606804.html
https://blog.csdn.net/zhanglizhi111/article/details/52922915