昨天加班到11点,因为自己的代码写的太烂了,老大很生气,后果很严重,因为tableViewcell重用机制,搞的我半天都是晕乎乎的。不过最后好在明白了点,把昨天的代码整理一下吧。
cell重用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCellc *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
switch (indexPath.row) {
case 0:
_cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
lableCardNo = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width * 0.18, 44)];
fieldCardNo = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width * 0.18 + 10, 0, self.view.frame.size.width * 0.5, 44)];
arrowBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width * 0.72, 0, 40, 44)];
_scanBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width * 0.85, 0, 44, 44)];
[_cell.contentView addSubview:_scanBtn];
[_cell.contentView addSubview:arrowBtn];
[_cell.contentView addSubview:lableCardNo];
[_cell.contentView addSubview:fieldCardNo];
break;
case 1:
_cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
_label2 = [[UILabel alloc] initWithFrame:CGRectMake(190, 0, 130, _cell.frame.size.height)];
[_cell.contentView addSubview:_label2];
break;
case 2:
_cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
_label3= [[UILabel alloc] initWithFrame:CGRectMake(190, 0, 130, _cell.frame.size.height)];
[_cell.contentView addSubview:_label3];
break;
}
}
switch (indexPath.row) {
case 0:
lableCardNo.text = @"卡号";
lableCardNo.font = [UIFont systemFontOfSize:14];
lableCardNo.textAlignment = NSTextAlignmentRight;
fieldCardNo.placeholder = @"请输入卡号";
fieldCardNo.delegate = self;
// 设置键盘样式
fieldCardNo.keyboardType = UIKeyboardTypePhonePad;
fieldCardNo.font = [UIFont systemFontOfSize:14];
[fieldCardNo addTarget : self action : @selector (textFieldClick:) forControlEvents : UIControlEventEditingDidBegin ];
[arrowBtn setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
[arrowBtn setImage:[UIImage imageNamed:@"arrow_up"] forState:UIControlStateSelected];
[arrowBtn setImageEdgeInsets:UIEdgeInsetsMake(15, 12, 15, 12)];
// [arrowBtn setBackgroundColor:[UIColor redColor]];
[arrowBtn addTarget:self action:@selector(arrowBtnClick) forControlEvents:UIControlEventTouchUpInside];
[_scanBtn setBackgroundImage:[UIImage imageNamed:@"saoMiao"] forState:UIControlStateNormal];
// [scanBtn setBackgroundColor:[UIColor blueColor]];
[_scanBtn addTarget:self action:@selector(scanCardBtnClick) forControlEvents:UIControlEventTouchUpInside];
break;
case 1:
_label2.text = @"4444";
break;
case 2:
_label3.text = @"5555";
break;
}
cell重用机制,其实明白其中的道理,每次tableView滑动的时候,都会执行上面这个方法,如果不做判断,就会一直创建新的cell对象,多滑动几次就会一直创建新的cell,导致程序卡死。cell创建只能创建一次,看着自己写的垃圾代码,心里还是很过意不去,毕竟自己的能力还是有欠缺,就怕公司或者领导不给自己改正的机会,还好有个好领导,能给这个机会。
代码规范性
比如一个button的初始化等,如果有完全一样的,可以抽取出来相同的代码,重新定义一个方法,传入需要的参数,返回需要的控件即可。
例如没有重构的代码
// 箭头点击事件
- (void)arrowBtnClick
{
lableCVV2.hidden = YES;
fieldCVV2.hidden = YES;
self.nTopCellHieght = 88;
[self.tableView reloadData];
// 状态取反
arrowBtn.selected = !arrowBtn.isSelected;
// i CardsArray数组长度 根据数组长度 设置弹出cardListView的大小
NSUInteger i = cards.count;
cardListView = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * 0.2, 45,self.view.frame.size.width * 0.6, 44 * i)] ;
cardListView.backgroundColor = [UIColor whiteColor];
cardListView.layer.borderWidth = 1;
cardListView.layer.borderColor = [[UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:239.0/255.0 alpha:1.0]CGColor];
UIButton * cardItem1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, cardListView.frame.size.width, 44)];
[cardListView addSubview:cardItem1];
[cardItem1 setTitle:cards[0] forState:UIControlStateNormal];
[cardItem1 setBackgroundImage:[UIImage imageNamed:@"button_red"] forState:UIControlStateHighlighted];
[cardItem1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
cardItem1.titleLabel.font = [UIFont systemFontOfSize:14];
[cardItem1 addTarget:self action:@selector(cardItemClick:) forControlEvents:UIControlEventTouchUpInside];
UIView *Line1 = [[UIView alloc]initWithFrame:CGRectMake(0, cardItem1.frame.size.height, cardListView.frame.size.width, 1)];
Line1.backgroundColor = [UIColor colorWithRed:239/255.0 green:239 blue:239 alpha:1.0];
[cardListView addSubview:Line1];
UIButton * cardItem2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 44, cardListView.frame.size.width, 44)];
[cardItem2 setTitle:cards[1] forState:UIControlStateNormal];
[cardListView addSubview:cardItem2];
[cardItem2 setBackgroundImage:[UIImage imageNamed:@"button_red"] forState:UIControlStateHighlighted];
[cardItem2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
cardItem2.titleLabel.font = [UIFont systemFontOfSize:14];
[cardItem2 addTarget:self action:@selector(cardItemClick:) forControlEvents:UIControlEventTouchUpInside];
UIView *Line2 = [[UIView alloc]initWithFrame:CGRectMake(0, cardItem2.frame.size.height, cardListView.frame.size.width, 1)];
Line1.backgroundColor = [UIColor colorWithRed:239 green:239 blue:239 alpha:1.0];
[cardListView addSubview:Line2];
UIButton * cardItem3 = [[UIButton alloc]initWithFrame:CGRectMake(0, 88, cardListView.frame.size.width, 44)];
[cardItem3 setTitle:cards[2] forState:UIControlStateNormal];
[cardListView addSubview:cardItem3];
[cardItem3 setBackgroundImage:[UIImage imageNamed:@"button_red"] forState:UIControlStateHighlighted];
[cardItem3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
cardItem3.titleLabel.font = [UIFont systemFontOfSize:14];
[cardItem3 addTarget:self action:@selector(cardItemClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cardListView];
}
可以看到每一行代码都是垃圾的不行的代码,每一次button的创建都是相同的内容。于是我重新写了一个方法,相对来说简单了一点,但是毕竟刚开始,代码还是不尽如人意。
- (void)arrowBtnClick
{
lableCVV2.hidden = YES;
fieldCVV2.hidden = YES;
self.nTopCellHieght = 88;
[self.tableView reloadData];
// 状态取反
arrowBtn.selected = !arrowBtn.isSelected;
// i CardsArray数组长度 根据数组长度 设置弹出cardListView的大小
NSUInteger i = cards.count;
cardListView = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * 0.2, 45,self.view.frame.size.width * 0.6, 44 * i)] ;
cardListView.backgroundColor = [UIColor whiteColor];
cardListView.layer.borderWidth = 1;
cardListView.layer.borderColor = [[UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:239.0/255.0 alpha:1.0]CGColor];
for (int j=0; j<i; j++) {
[cardListView addSubview:[self cardItem:j]];
}
[self.view addSubview:cardListView];
}
- (UIView *)cardItem:(int) j
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 44 * j, cardListView.frame.size.width, 44)];
UIButton * cardItem = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, cardListView.frame.size.width, 44)];
[view addSubview:cardItem];
[cardItem setTitle:cards[j] forState:UIControlStateNormal];
[cardItem setBackgroundImage:[UIImage imageNamed:@"button_red"] forState:UIControlStateHighlighted];
[cardItem setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
cardItem.titleLabel.font = [UIFont systemFontOfSize:14];
[cardItem addTarget:self action:@selector(cardItemClick:) forControlEvents:UIControlEventTouchUpInside];
UIView *Line = [[UIView alloc]initWithFrame:CGRectMake(0, cardItem.frame.size.height, cardListView.frame.size.width, 1)];
Line.backgroundColor = [UIColor colorWithRed:239/255.0 green:239 blue:239 alpha:1.0];
[view addSubview:Line];
return view;
}
小结
总是一天看起来很忙碌的敲代码,但是代码风格,真的是太烂了,被老大骂的也是活该。但是还是要安慰自己,没有今天菜鸟的我,怎么会有以后大神的我。谁不是从小白一步步来的,脚踏实地,稳中求稳,以后的coding之路还很长,我才迈出了小小的第一步而已。
在这里还是非常感谢,在iOS开发群里认识的一位朋友sacrifice的朋友,和一位叫落明的大神。非常感谢!
另外cell自定义扩展的连接也贴上,以备以后复习吧。
http://blog.sina.cn/dpool/blog/s/blog_65cbfb2b0101cd60.html?vt=4