UITableView
復用(重用)
button 點擊判斷事件
#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
NSMutableArray *_datas;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//新建數(shù)組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];
//判斷 如果數(shù)組行號和我們剛剛設置的就不顯示趁啸,反之就顯示
if ([_datas[indexPath.row] isEqual:@0]) {
targetView.hidden = YES;
}else{
targetView.hidden = NO;
}
//創(chuàng)建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,否則一直都只是執(zhí)行if里面的
if ([_datas[indexPath.row]isEqual:@0]) {
_datas[indexPath.row] = @1;
[_tableView reloadData];
NSLog(@"---%@",indexPath);
}else{
_datas[indexPath.row] = @0;
[_tableView reloadData];
}
}
@end
每個button都可以單獨點擊素挽,點亮或關閉小紅塊