tableView标记所选行和点击button显示和隐藏点

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

@interface ViewController ()
{
    UITableView *_tableView;
    NSMutableArray *_tag;
    NSMutableArray *_leftTag;
    NSInteger _lastIndex;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _lastIndex = 0;
    
    _tag = [NSMutableArray array];
    for (int i = 0; i<200; i++) {
        [_tag addObject:@0];
    }
    _leftTag = [NSMutableArray array];
    for (int i = 0; i<200; i++) {
        [_leftTag addObject:@0];
    }
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 70;
    [self.view addSubview:_tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        UIView *backView = [UIView new];
        backView.backgroundColor = [UIColor greenColor];
        backView.layer.cornerRadius = 10;
        [cell.contentView addSubview:backView];
        
        UILabel *numLabel = [UILabel new];
        numLabel.font = [UIFont systemFontOfSize:25];
        [cell.contentView addSubview:numLabel];
        numLabel.tag = 1001;
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.backgroundColor = [UIColor colorWithWhite:0.89 alpha:0.89];
        button.layer.cornerRadius = 15;
        button.titleLabel.font = [UIFont systemFontOfSize:18];
        [button setTintColor:[UIColor blackColor]];
        [button setTitle:@"点击" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(didClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];
        
        [backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@10);
            make.left.equalTo(@20);
            make.right.equalTo(@(-20));
            make.height.equalTo(@50);
        }];
        
        [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backView).offset(20);
            make.centerY.equalTo(backView);
        }];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(backView);
            make.right.equalTo(backView).offset(-20);
            make.size.mas_equalTo(CGSizeMake(60, 30));
        }];
        
        UIView *pointView = [UIView new];
        pointView.backgroundColor = [UIColor redColor];
        pointView.layer.cornerRadius = 4;
        [cell.contentView addSubview:pointView];
        [pointView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(button.mas_right).offset(6);
            make.centerY.equalTo(button);
            make.size.mas_equalTo(CGSizeMake(8, 8));
        }];
        [pointView setHidden:YES];
        pointView.tag = 1002;
        
        UIImageView *selectImageView = [UIImageView new];
        selectImageView.image = [UIImage imageNamed:@"arrow-right.png"];
        [cell.contentView addSubview:selectImageView];
        [selectImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(backView).offset(15);
            make.right.equalTo(backView.mas_left);
            make.left.equalTo(@0);
            make.height.equalTo(@20);
        }];
        selectImageView.hidden = YES;
        selectImageView.tag = 1003;
        
    }
    
    UILabel *numLabel = (UILabel*)[cell.contentView viewWithTag:1001];
    numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    //点击行显示小圆点,再次点击消失
    UIView *pointView = [cell.contentView viewWithTag:1002];
    if ([_tag[indexPath.row] isEqual:@0]) {
        pointView.hidden = YES;
    }
    else {
        pointView.hidden = NO;
    }
    //标记选中行
    UIImageView *selectImageView = [cell.contentView viewWithTag:1003];
    if ([_leftTag[indexPath.row] isEqual:@0]) {
        selectImageView.hidden = YES;
    }
    else {
        selectImageView.hidden = NO;
    }
    
    return cell;
}

- (void)didClicked:(UIButton*)sender {
    //button在tableView上零点的坐标
    CGPoint point = [sender convertPoint:CGPointZero toView:_tableView];
    //根据坐标算出在tableView上的位置
    NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:point];
    
    if ([_tag[indexPath.row]  isEqual: @0]) {
        _tag[indexPath.row] = @1;
    }
    else {
        _tag[indexPath.row] = @0;
    }
    [_tableView reloadData];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    _leftTag[_lastIndex] = @0;
    _leftTag[indexPath.row] = @1;
    _lastIndex = indexPath.row;
    [tableView reloadData];
}

@end

你可能感兴趣的:(tableView标记所选行和点击button显示和隐藏点)