CommonTableCollectView:使用例子,增删cell,以及label解析html

允许转载,但附上点击打开链接

库所在:https://github.com/harde1/CommonTableCollectView





把上面那样的界面,用CommonTableCollectView库做出来

首先是xib的布局方案:

遵守一个条件,布局不能出现歧义,上下支撑高度的条件只有一个




controller的代码如下:


#import "ViewController.h"
#import "CommonTableView.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet CommonTableView *tv_test;
@property(nonatomic,strong)UIButton * btn_delect;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(delete:)];
    
    
    
    for (int i=0; i<2; i++) {
        [self.tv_test addNibWithEntity:[@{@"html":@"<html><body>\"这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字:<a href=\'http://www.baidu.com\'>http://www.baidu.com\"</body></html>",@"isDelete":@(NO),@"isSelected":@(NO)}mutableCopy] andCellName:@"Cell_test"];
    }
    
    
    
}

-(UIButton *)btn_delect{
    if (!_btn_delect) {
        _btn_delect = [UIButton buttonWithType:UIButtonTypeSystem];
        _btn_delect.frame = CGRectMake(0, 0, self.view.frame.size.width, 60);
        
        [_btn_delect addTarget:self action:@selector(clickDelect:) forControlEvents:UIControlEventTouchUpInside];
        [_btn_delect setTitle:@"删除" forState:UIControlStateNormal];
        _btn_delect.backgroundColor = [UIColor redColor];
    }
    
    return _btn_delect;
    
}

-(void)delete:(id)sender{
    _isDelete = !_isDelete;
    for (NSMutableDictionary * dict in _tv_test.arr_dataSource[0]) {
        dict[@"isDelete"]= @(_isDelete);
    }
    [_tv_test reloadData];
    
    
    if (_isDelete) {
        
        _tv_test.tableFooterView = self.btn_delect;
    }else{
        [self.btn_delect removeFromSuperview];
        _tv_test.tableFooterView = nil;
        
    }
    
}

-(void)clickDelect:(UIButton *)btn{
    
    NSArray * arr_result = @[];
    @try {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isSelected = YES"];
        arr_result = [_tv_test.arr_dataSource[0] filteredArrayUsingPredicate:predicate];
        
    }
    @catch (NSException *exception) {
        NSLog(@"%@",exception);
    }
    @finally {
        NSLog(@"拿到的要删除的家伙:%@",arr_result);
    }
    
    NSMutableArray * arr = [@[]mutableCopy];
    for (NSMutableDictionary * dict in arr_result) {
        NSUInteger row = [_tv_test.arr_dataSource[0] indexOfObject:dict];
        
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        
        [arr addObject:indexPath];
    }
    
    for (NSIndexPath * indexPath in arr) {
        [_tv_test removeIndexPath:indexPath withRowAnimation:UITableViewRowAnimationLeft];
    }
    
}


@end




xib的代码如下:


#import "Cell_test.h"

@implementation Cell_test

-(void)setParams:(id)params{
    [super setParams:params];

    if (params) {
        
        NSString * htmlString = params[@"html"];
        NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        self.lb_content.attributedText = attrStr;
        
       
        
        self.tvf_content.editable = NO;
        
        
        

       
        
        
        
        self.tvf_content.attributedText = attrStr;
        
        
        self.iv_center.layer.cornerRadius = 30;
        self.iv_center.layer.masksToBounds = YES;
        
        self.iv_center.backgroundColor = [UIColor redColor];
        
        
        
        self.btn_select.layer.cornerRadius = 15;
        self.btn_select.layer.masksToBounds = YES;
        
        [self.btn_select addTarget:self action:@selector(clickSelect:) forControlEvents:UIControlEventTouchUpInside];
        
         if ([params[@"isSelected"] boolValue]) {
        
        self.btn_select.backgroundColor = [UIColor redColor];
        
         }else{
         self.btn_select.backgroundColor = [UIColor whiteColor];
         
         }
        if ([params[@"isDelete"] boolValue]) {
            self.cons_leading.constant = 100;
            
            
        }else{
         self.cons_leading.constant = 10;
            for (NSMutableDictionary * dict in self.tableView.arr_dataSource[0]) {
                dict[@"isSelected"] = @(NO);
            }
            
        }
        
        
    }
    
}

-(void)clickSelect:(UIButton *)btn{


    self.params[@"isSelected"] = @(![self.params[@"isSelected"] boolValue]);
    
    if ([self.params[@"isSelected"] boolValue]) {
        self.btn_select.backgroundColor = [UIColor redColor];
    }else{
    self.btn_select.backgroundColor = [UIColor whiteColor];
    }
    
    
}


@end



核心就是几句:


  [self.tv_test addNibWithEntity:[@{@"html":@"<html><body>\"这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字这是一堆文字:<a href=\'http://www.baidu.com\'>http://www.baidu.com\"</body></html>",@"isDelete":@(NO),@"isSelected":@(NO)}mutableCopy] andCellName:@"Cell_test"];



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值