使用 StoryBoard 制作一个能够删除cell的TableView

 

本篇博客方便自己检索使用。资源链接

 

下面是制作效果图,点击删除按钮,就能将该cell删除:

 

 

 

下面是主要的代码:

#define KSUPER_TAG 20000
#define KDEFAU_TAG 10000

#import "WholeViewController.h"
#import "HJ_CELL_3.h"
#import "HJ_CELL_4.h"
#import "NULL_CELL.h"
#import "HRAdView.h"
#import "FrameMacro.h"
#import "ColorMacro.h"
#import "FontMacro.h"

@interface WholeViewController ()
{
    NSMutableArray *_dataSource;
    UIView         * _tabHead_View ;
    
}
@end

@implementation WholeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"全部"];
  
    self.tableView.backgroundColor = K_SET_COLOR_VALUE(K_ROOT_BGC);
    
    // 假设有表头数据
    [self createTableHeader];
    
    // 设置无选择线
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    // 初始化数据源
    _dataSource = [NSMutableArray arrayWithObjects:@"3",@"3",@"2",@"3",@"2",@"3",@"2",@"3",@"2",@"3",@"2",@"3",@"2",@"3",@"2",@"3",@"2",@"0", nil];

}

// 创建表头
- (void)createTableHeader{
    
    if (!_tabHead_View) {
        
        // 表头部分的View
        _tabHead_View = [[UIView alloc] initWithFrame:CGRectMake(50,50, K_WIDTH, 62)];
        
        _tabHead_View.backgroundColor = K_SET_COLOR_VALUE(K_ROOT_BGC);
        
        // 初始化广告控件
        HRAdView *hdview = [[HRAdView alloc] initWithTitles:@[@"你有一笔支付订单30分钟后关闭",@"你有一笔支付订单60分钟后关闭",@"你有一笔支付订单90分钟后关闭"]];
        hdview.time = 2;
        [hdview beginScroll];
        hdview.backgroundColor = [UIColor whiteColor];
        hdview.isHaveTouchEvent = YES;
        hdview.clickAdBlock = ^(NSUInteger index){
            
            NSLog(@"下标是 %ld",(unsigned long)index);
        };
        hdview.frame = CGRectMake(0, 12, K_WIDTH, 49);
        [_tabHead_View addSubview:hdview];
        
        // 分割线 1
        UIView *line_1 = [[UIView alloc] initWithFrame:CGRectMake(0, 11, K_WIDTH, 1)];
        [line_1 setBackgroundColor:K_SET_COLOR_VALUE(@"#dbdbdb")];
        [_tabHead_View addSubview:line_1];
        
        // 分割线 2
        UIView *line_2 = [[UIView alloc] initWithFrame:CGRectMake(0, 61, K_WIDTH, 1)];
        [line_2 setBackgroundColor:K_SET_COLOR_VALUE(@"#dbdbdb")];
        [_tabHead_View addSubview:line_2];
        
    }
    
    self.tableView.tableHeaderView = _tabHead_View;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if (self.isViewLoaded&&self.view.window) {
        self.view = nil;
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [_dataSource count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = nil;
    
    NSString *mask = _dataSource[indexPath.row];
    
    
    if ([mask isEqualToString:@"3"]) {
        
        cell = (HJ_CELL_3*)[tableView dequeueReusableCellWithIdentifier:@"HJ_CELL_3" forIndexPath:indexPath];
        
        if (nil == cell) {
            
            cell = (HJ_CELL_3*)[[[NSBundle mainBundle] loadNibNamed:@"HJ_CELL_3" owner:self options:nil] lastObject];
        }
        
        ((HJ_CELL_3*)cell).block = ^(NSInteger statusNum){
        
            if ( 1 == statusNum) {
                
                // 执行删除任务
                [self tableView:tableView commitEditingStyle:UITableViewCellEditingStyleDelete forRowAtIndexPath:indexPath];
            }
        };
    }else
    
    if ([mask isEqualToString:@"2"]) {
        
        cell = (HJ_CELL_4*)[tableView dequeueReusableCellWithIdentifier:@"HJ_CELL_4" forIndexPath:indexPath];
        
        if (nil == cell) {
            
            cell = (HJ_CELL_4*)[[[NSBundle mainBundle] loadNibNamed:@"HJ_CELL_4" owner:self options:nil] lastObject];
        }
        
        ((HJ_CELL_4*)cell).block = ^(NSInteger statusNum){
            
            if ( 1 == statusNum) {
                
                // 执行删除任务
                [self tableView:tableView commitEditingStyle:UITableViewCellEditingStyleDelete forRowAtIndexPath:indexPath];
            }
        };

    }else {
        
        cell = (NULL_CELL*)[tableView dequeueReusableCellWithIdentifier:@"NULL_CELL" forIndexPath:indexPath];
        
        if (nil == cell) {
            
            cell = (NULL_CELL*)[[[NSBundle mainBundle] loadNibNamed:@"NULL_CELL" owner:self options:nil] lastObject];
        }
    }
    

    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *mask = _dataSource[indexPath.row];
    
    if ([mask isEqualToString:@"3"]) {
        
        return 105;
    }else
    
    if ([mask isEqualToString:@"2"]) {
        
        return 80;
    }else {
        
        return 20;
    }
    
    return 105;
    
}

// 删除某些行的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        
        [_dataSource removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    
        [self.tableView reloadData];
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

 

posted @ 2016-12-07 02:46  ywda  阅读(311)  评论(0编辑  收藏  举报