UITableViewCell上左滑删除

本文介绍了一个iOS应用中如何使用Table View展示从plist文件加载的数据,并实现了数据模型与单元格的绑定及删除功能。文章详细讲解了如何将plist文件中的字典数组转换为模型数组,以及如何通过自定义的单元格类来展示数据。
//
//  TableViewController.m


#import "TableViewController.h"
#import "tgModel.h"
#import "tgCell.h"

@interface TableViewController ()
/** 所有团购数据 */
@property(nonatomic,strong) NSMutableArray *tgs;
@end

@implementation TableViewController

- (NSMutableArray *)tgs
{
    if (_tgs == nil) {
        // 加载plist文件中的字典数组
        NSString *path = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 字典数组 -> 模型数组
        NSMutableArray *arrayM = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            tgModel *model = [tgModel tgWithDict:dict];
            [arrayM addObject:model];
        }
        
        _tgs = arrayM;
    }
    return _tgs;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    //注册xib
//    UINib *nib = [UINib nibWithNibName:NSStringFromClass([tgCell class]) bundle:nil];
//    [self.tableView registerNib:nib forCellReuseIdentifier:@"cell"];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tgs.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 创建cell的过程封装到cell类中
    tgCell *cell = [tgCell cellWithTableView:tableView];
    
    //传递模型数据
    cell.model = self.tgs[indexPath.row];
    
    
    return cell;
}

#pragma mark - 代理方法
/**
 *  只要实现这个方法,左滑cell就会出现删除按钮的功能就有了
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //删除模型
        [self.tgs removeObjectAtIndex:indexPath.row];
        
        //刷新表格
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
@end

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值