2019独角兽企业重金招聘Python工程师标准>>>
这篇文章只是新手看 做过开发的就可以关闭了
问题:为毛勾选不住 为毛 经常看到新手问这种问题
思路:放到模型 刷新数据 还是不会 所以写了个DEMO
VC:
//
// ViewController.m
// ChuHai
//
// Created by point on 16/9/12.
// Copyright © 2016年 dacai. All rights reserved.
//
#import "ViewController.h"
#import
#import "CHCell.h"
#import "CHmodel.h"
@interface ViewController ()
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *modelArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupTableView];
#warning crate model
//第一步 模拟模拟模拟数据 --- 不管你网络获取 本地SQL 转成模型
#warning mark
//第二步 注意观察 你滑动的时候背景色会改变 ---说明cell 已经产生了重用问题
#warning mark
//第三步 我们解决选中按钮的问题 注意看!!!!
CHmodel *model1 = [[CHmodel alloc]init];
model1.title = @"为";
model1.selectedBtn = YES;
CHmodel *model2 = [[CHmodel alloc]init];
model2.title = @"名";
model2.selectedBtn = NO;
CHmodel *model3 = [[CHmodel alloc]init];
model3.title = @"除";
model3.selectedBtn = YES;
CHmodel *model4 = [[CHmodel alloc]init];
model4.title = @"害";
model4.selectedBtn = NO;
CHmodel *model5 = [[CHmodel alloc]init];
model5.title = @"加油";
model5.selectedBtn = YES;
CHmodel *model6 = [[CHmodel alloc]init];
model6.title = @"为";
model6.selectedBtn = YES;
CHmodel *model7 = [[CHmodel alloc]init];
model7.title = @"名";
model7.selectedBtn = NO;
CHmodel *model8 = [[CHmodel alloc]init];
model8.title = @"除";
model8.selectedBtn = YES;
CHmodel *model9 = [[CHmodel alloc]init];
model9.title = @"害";
model9.selectedBtn = NO;
CHmodel *model10 = [[CHmodel alloc]init];
model10.title = @"加油";
model10.selectedBtn = YES;
[self.modelArr addObject:model1];
[self.modelArr addObject:model2];
[self.modelArr addObject:model3];
[self.modelArr addObject:model4];
[self.modelArr addObject:model5];
[self.modelArr addObject:model6];
[self.modelArr addObject:model7];
[self.modelArr addObject:model8];
[self.modelArr addObject:model9];
[self.modelArr addObject:model10];
}
- (void)setupTableView {
[self.view addSubview:self.tableView];
[self.tableView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.modelArr.count;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CHCell *cell = [tableView dequeueReusableCellWithIdentifier:kCHCellIdentifier forIndexPath:indexPath];
cell.delegate = self;
[cell configModel:self.modelArr[indexPath.row] row:indexPath.row];
return cell;
}
- (void)chCell:(CHCell *)carouselView didClick:(NSInteger)index {
self.modelArr[index].selectedBtn = !self.modelArr[index].selectedBtn;
[self.tableView reloadData];
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
[_tableView configureForAutoLayout];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.showsVerticalScrollIndicator = YES;
_tableView.alwaysBounceVertical = YES;
[_tableView registerClass:[CHCell class] forCellReuseIdentifier:kCHCellIdentifier];
}
return _tableView;
}
- (NSMutableArray *)modelArr{
if(!_modelArr){
_modelArr = [NSMutableArray array];
}
return _modelArr;
}
@end
cell.h:
//
// CHCell.h
// ChuHai
//
// Created by point on 16/9/12.
// Copyright © 2016年 dacai. All rights reserved.
//
#import
@class CHmodel;
@class CHCell;
static NSString *const kCHCellIdentifier = @"CHCelldentifier";
@protocol CHCellDelegate
@optional
- (void)chCell:(CHCell *)carouselView didClick:(NSInteger)index;
@end
@interface CHCell : UITableViewCell
- (void)configModel:(CHmodel *)model row:(NSInteger)row;
@property (nonatomic, weak) id delegate;
@end
cell.m:
//
// CHCell.m
// ChuHai
//
// Created by point on 16/9/12.
// Copyright © 2016年 dacai. All rights reserved.
//
#import "CHCell.h"
#import
#import "CHmodel.h"
#define RGBRandomColor [UIColor colorWithRed:arc4random_uniform(100)/100.0 green:arc4random_uniform(100)/100.0 blue:arc4random_uniform(100)/100.0 alpha:1.0]
@interface CHCell ()
/** 除害label */
@property(nonatomic,strong) UILabel * chuhaiLabel;
/** 除害点击btn */
@property(nonatomic,strong) UIButton * chuhaiBtn;
@end
@implementation CHCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = RGBRandomColor;
self.selectionStyle = UITableViewCellSelectionStyleNone;
//设置
[self.contentView addSubview:self.chuhaiBtn];
[self.contentView addSubview:self.chuhaiLabel];
//布局
[self.chuhaiLabel autoCenterInSuperview];
[self.chuhaiBtn autoSetDimensionsToSize:CGSizeMake(100, 100)];
[self.chuhaiBtn autoPinEdgeToSuperviewEdge:ALEdgeLeft];
[self.chuhaiBtn autoPinEdgeToSuperviewEdge:ALEdgeTop];
[self.chuhaiBtn setImage:[UIImage imageNamed:@"btn2"] forState:UIControlStateNormal];
[self.chuhaiBtn setImage:[UIImage imageNamed:@"btn1"] forState:UIControlStateSelected];
[self.chuhaiBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)clickBtn:(UIButton *)btn {
btn.selected = !btn.selected;
if ([_delegate respondsToSelector:@selector(chCell:didClick:)]){
[_delegate chCell:self didClick:btn.tag];
}
}
- (void)configModel:(CHmodel *)model row:(NSInteger)row {
self.chuhaiLabel.text = model.title;
self.chuhaiBtn.tag = row;
if(model.selectedBtn == YES){
self.chuhaiBtn.selected = YES;
}else{
self.chuhaiBtn.selected = NO;
}
}
- (UILabel *)chuhaiLabel {
if(!_chuhaiLabel){
_chuhaiLabel = [UILabel newAutoLayoutView];
}
return _chuhaiLabel;
}
- (UIButton *)chuhaiBtn {
if(!_chuhaiBtn){
_chuhaiBtn = [UIButton newAutoLayoutView];
}
return _chuhaiBtn;
}
@end
model:
//
// CHmodel.h
// ChuHai
//
// Created by point on 16/9/12.
// Copyright © 2016年 dacai. All rights reserved.
//
#import
@interface CHmodel : NSObject
/** label 文字*/
@property(nonatomic,copy) NSString * title;
/** btn 是否选中*/
@property(nonatomic,assign) BOOL selectedBtn;
@end
结束