#import "ViewController.h"
#import "ClickedTableViewCell.h"
#import "Masonry.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
{
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显示和隐藏点
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.viewController.mp.p1 {margin: 0.0px 0.0px 0.0px 0.0px; ...
- button分次点击效果不同(点击button隐藏一个视图,再次点击该视图显示,如此循环) 实现循环改变视图的隐藏...