仿微信UITableViewCell 左滑刪除、確認刪除


添加基本方法

?- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
??? return UITableViewCellEditingStyleDelete;
}
-? (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
??? return YES;
}

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

{
? ? for (UIView *subview in tableView.subviews) {
?????? if ([NSStringFromClass([subview class]) isEqualToString:@"UISwipeActionPullView"]) {
????????? if ([NSStringFromClass([subview.subviews[0] class]) isEqualToString:@"UISwipeActionStandardButton"]) {
???????????????? subview.backgroundColor = [UIColor whiteColor];
???????????????? UIButton *deleteBtn = subview.subviews[0];
???????????????? [deleteBtn setWidth:75];
????????????????? UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: deleteBtn.bounds byRoundingCorners:
????????????????? UIRectCornerBottomRight | UIRectCornerTopRightcornerRadii:CGSizeMake(8,8)];
???????????????? CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
???????????????? maskLayer.frame = deleteBtn.bounds;
???????????????? maskLayer.path = maskPath.CGPath;
??????????????? deleteBtn.layer.mask = maskLayer;? ? ? ? ? ? ??
??????????????? deleteBtn.backgroundColor = HexRGB(0xF3755C);
?????????????? [deleteBtn setTitle:@"刪除" forState:UIControlStateNormal];
??????????????? }
?????????? }
???????? }
???? }

此方法是ios 11 之后用到的?? TARGETS -> Deployment info? -> Deployment Target? 設置為11.0



- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
{
? ? MJWeakSelf;
???? UIContextualAction *action = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"刪除" handler:^(UIContextualAction * _Nonnull action,?? __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
???????? if ([NSStringFromClass([sourceView.superview class]) isEqualToString:@"UISwipeActionStandardButton"]) {
?????????????? UIButton *tmpBtn = (UIButton *)sourceView.superview;
?????????????? if ([[tmpBtn currentTitle] isEqualToString:@"確認刪除"]) {
?????????????????? DeviceRealmModel *model = [weakSelf.tableViewArr objectAtIndex:indexPath.section];?????????????????
????????????????? //刪除操作
??????????????? else{
??????????????????? UIImage *img = [self imageWithColor:HexRGB(0xF3755C)];
??????????????????? [tmpBtn setBackgroundImage:img forState:UIControlStateNormal];
??????????????????? [tmpBtn setTitle:@"確認刪除" forState:UIControlStateNormal];
??????????????? }
?????????? }
????? }];
?? // 此處可以添加多個UIContextualAction 放入下面的configuration 數(shù)組中
?????? UISwipeActionsConfiguration *configuration = [UISwipeActionsConfiguration configurationWithActions:@[action]];
????? configuration.performsFirstActionWithFullSwipe = NO;
?????? return configuration;
????? }

//? 顏色轉(zhuǎn)換為背景圖片

- (UIImage *)imageWithColor:(UIColor *)color {
????? CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
???? UIGraphicsBeginImageContext(rect.size);
??? CGContextRef context = UIGraphicsGetCurrentContext();
??? CGContextSetFillColorWithColor(context, [color CGColor]);
??? CGContextFillRect(context, rect);
?? UIImage *image = UIGraphicsGetImageFromCurrentImageContext();??????????????????????????
?? UIGraphicsEndImageContext();???
??? return image;
}

不要問我為什么不用設置背景色來設置顏色,此時 操作完成? 主意你的xocde

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容