iOS tableViewCell 自定義左滑刪除按鈕樣式

  • iOS11之前

for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
            for (UIButton *btn in subView.subviews) {
                if ([btn isKindOfClass:[UIButton class]]) {
/*在此處可以自定義刪除按鈕的樣式*/
                    btn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
                }
            }
        }
    }

將上邊??代碼加入到 自定義cell.m 文件 layoutSubviews 方法中?。?!之外還要設(shè)置tableview的代理方法

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return   UITableViewCellEditingStyleDelete;
}
//Cell可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//修改編輯按鈕文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"刪除";
}
//設(shè)置進入編輯狀態(tài)時,Cell不會縮進
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.arr removeObjectAtIndex:indexPath.row];
    [self.mainTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
  • 補充:(iOS 11 之后)

    • 只需要設(shè)置tableview一個代理方法可以更加方便的實現(xiàn)以上功能

    碼:

#pragma mark 測試
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
    //刪除
    if (@available(iOS 11.0, *)) {
        UIContextualAction *delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"刪除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
            [self.arr removeObjectAtIndex:indexPath.row];
            completionHandler (YES);
            [self.mainTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }];
//    delete.image = [UIImage imageNamed:@"delete"];//這里還可以設(shè)置圖片
    delete.backgroundColor = [UIColor grayColor];
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[delete]];
    return config;
    } else {
        return nil;
        // Fallback on earlier versions
    }
}
  • cell加載動畫

只要在代理方法中加入動畫代碼即可

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.x = - SCREEN_WIDTH;
    cell.alpha = 0.1;
    [UIView animateWithDuration:0.8 animations:^{
        cell.x = 0;
        cell.alpha = 1;
    }completion:^(BOOL finish){
    
    }];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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