10.12UITableView(cell)

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,207评论 4 61
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 5,199评论 0 1
  • 一、不种格式图片的使用 1.JPEG格式—照片和复杂图像使用 适合连续色调图像,例如照片 包含颜色丰富多达1600...
    April_17阅读 3,142评论 0 0
  • 记得之前看过一篇文章说 “写作并不难 在你有想法有思维的时候拿起笔写就OK” 对啊 你管写出来的文字通不通顺 流不...
    素靥阅读 1,131评论 0 0
  • 印象中我小时候没怎么发过烧,以致于贤哥,我总也以为不会发烧的,更骄傲地对朋友晨霞说:“我家宝宝都不会发烧的,一岁了...
    燕纪事阅读 3,043评论 0 0