您的位置:首页 > 移动开发 > IOS开发

【11/04】 iOS开发成长之路,【tabelView cell复用】

2015-11-04 19:00 357 查看

今天调试程序遇到这么一个警告!

Local declaration of ‘XXX’ hides instance variable

遇到这种原因,是因为本地变量跟函数参数变量同名。开来,还是要注意变量的命名啊!

tableViewcell 去掉横线

隐藏UITableViewCell的分隔线

[self.myTableView       setSeparatorStyle:UITableViewCellSeparatorStyleNone];


textfield 左边距

代理方法实现。要么直接改变textfield的x值

图片自适应大小

iconLeftView.contentMode =UIViewContentModeScaleAspectFit;


tabelView点击之后变灰色 去掉

在 cell alloc之后!!

cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[tableView deselectRowAtIndexPath:indexPath animated:YES];


今天写的类

老大说再改改就能用了。加班改代码中。。。

#import "DSNewRegistViewController.h"

#define kDefaultHeight 44  //默认cell高度
#define kBaseCellRect CGRectMake(0, 0, self.view.frame.size.width , kDefaultHeight) //默认cellrect
#define kTextMarginLeft self.view.width*0.2 // 文字左边距
#define kIconSize 30                        // 图标尺寸 30*30
#define kIconMarginLeft self.view.width*0.1  // 图标左边距
#define kTextWidth self.view.width*0.6   //  text的宽度
@interface DSNewRegistViewController (){
NSArray *leftIcon; //左边图标
NSArray *eyesIcon; //  眼睛图标
NSArray *labelArray; // 提示信息数组
}
@property(nonatomic,strong)UITableViewCell *nameCell;//昵称
@property(nonatomic,strong)UITableViewCell *phoneCell;//手机号
@property(nonatomic,strong)UITableViewCell *loginPswCell;//登录密码
@property(nonatom
4000
ic,strong)UITableViewCell *payPswCell;//支付
@property(nonatomic,strong)UITableViewCell *tipCell;// 提示信息
@property(nonatomic,strong)UITableViewCell *protocolCell;// 得仕协议
@property(nonatomic,strong)UITableViewCell *submitCell;// 提交按钮
@property(assign,nonatomic)NSInteger rowIndex;
@property(nonatomic,strong)UITableViewCell *cell;
@end

@implementation DSNewRegistViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"注册";

self.rowIndex = 7;

//图标数组
leftIcon =@[@"regist_icon01",@"regist_icon02",@"regist_icon04",@"regist_icon05",@"regist_icon03"];//左边图标
eyesIcon =@[@"regist_icon06",@"regist_icon07"];// 眼睛图标
// 提示信息数组
labelArray = @[@"设置一个昵称吧",@"手机号",@"设置登录密码",@"登录密码和支付密码均为6-20位,数字,字母及组合",@"设置支付密码"];

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.rowIndex;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//tableView空白样式
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

NSInteger row = indexPath.row;

cell = [self Cell:row];

return cell;

}
//
-(UITableViewCell *)Cell :(NSInteger)row{
switch (row) {
case 0:
{
if (_nameCell == nil) {

_nameCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"nameCell"];
//
_nameCell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *baseView= [[UIView alloc]initWithFrame:kBaseCellRect];
[self loadCellView:baseView withRow:row];
[_nameCell.contentView addSubview:baseView];
}

return _nameCell;
}
break;

case 1:
{
if (_phoneCell == nil) {

_phoneCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"phoneCell"];
_phoneCell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *baseView= [[UIView alloc]initWithFrame:kBaseCellRect];
[self loadCellView:baseView withRow:row];
[_phoneCell.contentView addSubview:baseView];
}

return _phoneCell;
}
break;
case 2:
{
if (_loginPswCell == nil) {

_loginPswCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"loginPswCell"];
_loginPswCell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *baseView= [[UIView alloc]initWithFrame:kBaseCellRect];
[self loadCellView:baseView withRow:row];
[_loginPswCell.contentView addSubview:baseView];
}

return _loginPswCell;
}
break;
case 3:
{
if (_payPswCell == nil) {

_payPswCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"payPswCell"];
_payPswCell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *baseView= [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , 50)];
baseView.backgroundColor = DSColor(239, 239, 239);
[self loadCellView:baseView withRow:row];
[_payPswCell.contentView addSubview:baseView];
}

return _payPswCell;
}
break;

case 4:
{
if (_tipCell == nil) {

_tipCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tipCell"];
_tipCell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *baseView= [[UIView alloc]initWithFrame:kBaseCellRect];
[self loadCellView:baseView withRow:row];
[_tipCell.contentView addSubview:baseView];
}

return _tipCell;
}
break;

case 5:
{
if (_protocolCell == nil) {

_protocolCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"protocolCell"];
_protocolCell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *baseView= [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , 60)];
baseView.backgroundColor = DSColor(239, 239, 239);
[self loadCellView:baseView withRow:row];
[_protocolCell.contentView addSubview:baseView];
}

return _protocolCell;
}
break;

case 6:
{
if (_submitCell == nil) {

_submitCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"submitCell"];
_submitCell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *baseView= [[UIView alloc]initWithFrame:kBaseCellRect];
baseView.backgroundColor = DSColor(239, 239, 239);
[self loadCellView:baseView withRow:row];
[_submitCell.contentView addSubview:baseView];
}

return _submitCell;
}
break;

}
return nil;
}

// 初始化cell的布局
- (void)loadCellView:(UIView *)view withRow:(NSInteger)row
{
switch (row) {
case 0:
{
UIImageView *iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:leftIcon[row]]];
iconView.frame = CGRectMake(kIconMarginLeft, 5 , kIconSize, kIconSize);
iconView.contentMode =UIViewContentModeScaleAspectFit;

UITextField *field=[[UITextField alloc] initWithFrame:CGRectMake(kIconMarginLeft+2*kIconSize, 0, view.width - kIconMarginLeft -2*kIconSize, kDefaultHeight)];
field.textAlignment=NSTextAlignmentLeft;
field.font=[UIFont systemFontOfSize:14];
field.placeholder = labelArray[row];
field.delegate = self;
[self drawLine:view withFrame:CGRectMake(0,  kDefaultHeight -1, self.view.width, 1)];
[view addSubview:iconView];
[view addSubview:field];

break;
}
case 1:
{
UIImageView *iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:leftIcon[row]]];
iconView.frame = CGRectMake(kIconMarginLeft, 5 , kIconSize, kIconSize);
iconView.contentMode =UIViewContentModeScaleAspectFit;

UITextField *field=[[UITextField alloc] initWithFrame:CGRectMake(kIconMarginLeft+2*kIconSize, 0, view.width - kIconMarginLeft -2*kIconSize, kDefaultHeight)];
field.textAlignment=NSTextAlignmentLeft;
field.font=[UIFont systemFontOfSize:14];
field.keyboardType = UIKeyboardTypePhonePad ;
field.placeholder = labelArray[row];
field.delegate = self;
[self drawLine:view withFrame:CGRectMake(0,  kDefaultHeight -1, self.view.width, 1)];
[view addSubview:iconView];
[view addSubview:field];
break;

}
case 2:
{
UIImageView *iconLeftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:leftIcon[row]]];
iconLeftView.frame = CGRectMake(kIconMarginLeft, 5 , kIconSize, kIconSize);
UITextField *field=[[UITextField alloc] initWithFrame:CGRectMake(kIconMarginLeft+2*kIconSize, 0, view.width - 2*(kIconMarginLeft +1.5*kIconSize ) , kDefaultHeight)];
field.textAlignment=NSTextAlignmentLeft;
field.font=[UIFont systemFontOfSize:14];
field.keyboardType = UIKeyboardTypePhonePad ;
field.placeholder = labelArray[row];
field.delegate = self;
UIButton* iconRightBtn= [[UIButton alloc]initWithFrame:CGRectMake(field.x+field.width , 5 , kIconSize, kIconSize)];
[iconRightBtn setImage:[UIImage imageNamed:eyesIcon[0]] forState:UIControlStateNormal];
[iconRightBtn setImage:[UIImage imageNamed:eyesIcon[0]] forState:UIControlStateHighlighted];
[iconRightBtn setImage:[UIImage imageNamed:eyesIcon[1]] forState:UIControlStateSelected];
iconRightBtn.contentMode =UIViewContentModeScaleAspectFit;
[iconRightBtn addTarget:self action:@selector(agreeClick:) forControlEvents:UIControlEventTouchUpInside];
[self drawLine:view withFrame:CGRectMake(0,  kDefaultHeight -1, self.view.width, 1)];

[view addSubview:iconLeftView];
[view addSubview:iconRightBtn];
[view addSubview:field];
break;

}
case 3:
{
UIImageView *tipIconView = [[UIImageView alloc]init];
tipIconView.contentMode =UIViewContentModeScaleAspectFit;
tipIconView.frame = CGRectMake(kIconMarginLeft, 5 , kIconSize, kIconSize);
tipIconView.image = [UIImage imageNamed:leftIcon[row]];

UILabel *tipLabel=[[UILabel alloc] initWithFrame:CGRectMake(kIconMarginLeft+kIconSize, 0, view.width - kIconMarginLeft -2*kIconSize, kDefaultHeight)];
tipLabel.text= labelArray[row];
tipLabel.textColor = [UIColor redColor];
tipLabel.numberOfLines = 0;
tipLabel.font =[UIFont systemFontOfSize:13];
[self drawLine:view withFrame:CGRectMake(0,  50 -1, self.view.width, 1)];
[view addSubview:tipIconView];
[view addSubview:tipLabel];

break;

}
case 4:
{
UIImageView *iconLeftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:leftIcon[row]]];
iconLeftView.frame = CGRectMake(kIconMarginLeft, 5 , kIconSize, kIconSize);
UITextField *field=[[UITextField alloc] initWithFrame:CGRectMake(kIconMarginLeft+2*kIconSize, 0, view.width - 2*(kIconMarginLeft +1.5*kIconSize ) , kDefaultHeight)];
field.textAlignment=NSTextAlignmentLeft;
field.font=[UIFont systemFontOfSize:14];
field.keyboardType = UIKeyboardTypePhonePad ;
field.placeholder = labelArray[row];
field.delegate = self;
UIButton* iconRightBtn= [[UIButton alloc]initWithFrame:CGRectMake(field.x+field.width , 5 , kIconSize, kIconSize)];
[iconRightBtn setImage:[UIImage imageNamed:eyesIcon[0]] forState:UIControlStateNormal];
[iconRightBtn setImage:[UIImage imageNamed:eyesIcon[0]] forState:UIControlStateHighlighted];
[iconRightBtn setImage:[UIImage imageNamed:eyesIcon[1]] forState:UIControlStateSelected];
iconRightBtn.contentMode =UIViewContentModeScaleAspectFit;
[iconRightBtn addTarget:self action:@selector(agreeClick:) forControlEvents:UIControlEventTouchUpInside];
[self drawLine:view withFrame:CGRectMake(0,  kDefaultHeight -1, self.view.width, 1)];

[view addSubview:iconLeftView];
[view addSubview:iconRightBtn];
[view addSubview:field];
break;

}
case 5:
{

UIButton* iconView = [[UIButton alloc]init];
iconView.frame = CGRectMake(kIconMarginLeft, 5 , kIconSize, kIconSize);
[iconView setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
[iconVie
ac6b
w setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
[iconView addTarget:self action:@selector(agreeClick:) forControlEvents:UIControlEventTouchUpInside];
//            [iconView setImageEdgeInsets:UIEdgeInsetsMake(10, 5,10 ,5)];

UILabel* agreeLabel=[[UILabel alloc] initWithFrame:CGRectMake(kIconMarginLeft+kIconSize, 0, view.width - kIconMarginLeft +kIconSize , kDefaultHeight)];

agreeLabel.text= @"我已阅读并同意《得仕钱包服务协议》";
agreeLabel.textColor = [UIColor blackColor];
agreeLabel.font =[UIFont systemFontOfSize:13];

[view addSubview:iconView];
[view addSubview:agreeLabel];

break;

}
case 6:
{
UIButton * submitBtn=[self LoadDefaultButtonInView:self.view withFrame:CGRectMake(kTextMarginLeft, 0, kTextWidth, 30) title:@"提交"];
[submitBtn addTarget:self action:@selector(submitBtnClick) forControlEvents:UIControlEventTouchUpInside];
[submitBtn.layer setMasksToBounds:YES];
[submitBtn.layer setCornerRadius:5.0];

[view addSubview:submitBtn];

break;

}
}

}

// 画线
- (void)drawLine:(UIView *)view withFrame:(CGRect)frame
{
UIView *line=[[UIView alloc] initWithFrame:frame];
line.backgroundColor=[UIColor colorWithWhite:0.85 alpha:1.0];
[view addSubview:line];
}

- (void)agreeClick:(UIButton *)btn
{

btn.selected = !btn.selected;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==3)
{
return 50;
}
else if(indexPath.row==5)
{
return 60;
}
return 44;

}

#pragma mark - 提交按钮点击事件
- (void)submitBtnClick
{

NSLog(@"Clicked on this button");

//    DSRegistNextViewController *tViewCtr=[[DSRegistNextViewController alloc]init];
//    [self.navigationController pushViewController:tViewCtr animated:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: