10.12UITableView(cell)

weixin_34054866發表於2016-10-23

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都可以單獨點選,點亮或關閉小紅塊

2455916-fb515d32a87222e2.png
Paste_Image.png

相關文章