美文网首页
10.12UITableView(cell)

10.12UITableView(cell)

作者: jayck | 来源:发表于2016-10-23 20:57 被阅读13次

UITableView
复用(重用)
button 点击判断事件

#import "ViewController.h"
#import "Masonry.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_datas;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //新建数组datas,随便给个值0,等下下面是使用
    _datas = [NSMutableArray new];
    for (NSInteger i = 0; i<200; i++) {
        
        [_datas addObject:@0];
    }
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(@0);
    }];

}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 200;
}
//设置cell属性
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //复用表示符为“cell”
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor blueColor];
        [cell.contentView addSubview:button];
        button.frame = CGRectMake(200, 10, 100, 30);
        [button setTitle:@"button" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        
        UIView *v = [UIView new];
        v.frame = CGRectMake(320, 10, 20, 20);
        v.backgroundColor = [UIColor colorWithRed:0.8 green:0.1 blue:0.1 alpha:0.5];
        [cell.contentView addSubview:v];
        v.tag =1002;
    }
    
    UIView *targetView = (UIView*)[cell.contentView viewWithTag:1002];
    //判断 如果数组行号和我们刚刚设置的就不显示,反之就显示
    if ([_datas[indexPath.row] isEqual:@0]) {
        
        targetView.hidden = YES;
    }else{
        
        targetView.hidden = NO;
    }
    
    //创建label并给Tag值
    UILabel *templabel = [cell.contentView viewWithTag:1001];
    if (!templabel) {
        
        UILabel *contentLabel = [UILabel new];
        [cell.contentView addSubview:contentLabel];
        contentLabel.frame = CGRectMake(20, 10, 100, 20);
        contentLabel.tag = 1001;
        
        static NSInteger count = 0;
        NSLog(@"%ld",count++);
    }
    //显示withTag值
    templabel = (UILabel*)[cell.contentView viewWithTag:1001];
    templabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //关闭cell的动画
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (void)buttonClicked:(UIButton*)sender{
    
    CGPoint point = [sender convertPoint:CGPointZero toView:_tableView];
    NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:point];
//    反向判断    按钮才能进到else,否则一直都只是执行if里面的
    if ([_datas[indexPath.row]isEqual:@0]) {
    _datas[indexPath.row] = @1;
    [_tableView reloadData];
    NSLog(@"---%@",indexPath);
    }else{
        _datas[indexPath.row] = @0;
        [_tableView reloadData];
    }
}

@end

每个button都可以单独点击,点亮或关闭小红块

Paste_Image.png

相关文章

网友评论

      本文标题:10.12UITableView(cell)

      本文链接:https://www.haomeiwen.com/subject/ahpkuttx.html