-
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){
}];
}