允许转载,但附上点击打开链接
库所在: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"];