当前位置:编程学习 > wap >>

iOS -UITableView / UITextField

这个效果之前一直以为是UI把图给p成这样子的效果的。后来才知道,代码也可以写的。。
 
(1)边框是设置的圆角:
 
    TableView.layer.cornerRadius=15;//圆角大小
(2)tableview的cell里面放的是UITextField
 
------
(1)让你的控制器类遵守
UITableViewDataSource,UITableViewDelegate协议,申明一个tableview
[objc] 
@interface LogInViewController ()<UITableViewDataSource,UITableViewDelegate>  
@property (strong,nonatomic)UITableView *loginTableView;  
  
  
@end  
 
(2)初始化tabelview
[objc] 
-(void)initTableView{  
    UITableView *TableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 400, 140) style:UITableViewStylePlain];//初始化tabview  
    TableView.center =CGPointMake(self.view.center.x, self.view.center.y-70);//tableview的中心位置  
    TableView.delegate = self;  
    TableView.dataSource=self;  
    TableView.scrollEnabled=NO;//tabview是否滑动  
    TableView.layer.cornerRadius=15;//圆角大小  
      
    _loginTableView = TableView;  
      
    [self.view addSubview:_loginTableView];  
}  
 
(3)设置tablview的delegate和datasource
[objc]  
//行高  
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    return 70;  
}  
  
//多少个section  
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
{  
    return 1;  
      
}  
  
//section里面有多少行  
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex  
{  
    return 2;  
}  
  
//cell的内容  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    static NSString *iden = @"loginCell";  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];  
    if (cell ==nil) {  
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];  
        cell.selectionStyle = UITableViewCellSelectionStyleNone;  
          
          
          
    }  
      
      
    if (indexPath.row == 0 ) {  
//        第一行cell里面放UITextField  
        UITextField *nameTF = [[UITextField alloc]initWithFrame:CGRectMake(15, cell.frame.size.height/2, cell.frame.size.width, cell.frame.size.height-10)];  
          
        nameTF.backgroundColor = [UIColor clearColor];  
        nameTF.placeholder = @"用户名";//当输入框没有内容时,水印提示  
          
        nameTF.clearButtonMode=YES;//设置显示一健清除按钮  
        nameTF.clearButtonMode = UITextFieldViewModeAlways;//  UITextFieldViewModeNever,  重不出现--UITextFieldViewModeWhileEditing, 编辑时出现--UITextFieldViewModeUnlessEditing,  除了编辑外都出现---UITextFieldViewModeAlways   一直出现  
          
        nameTF.clearsOnBeginEditing = YES;//再次编辑就清空  
  
  
          
        nameTF.font = [UIFont fontWithName:@"Arial" size:20.0f];  
        nameTF.textColor = [UIColor cyanColor];//设置字体颜色  
        [cell.contentView addSubview:nameTF];  
    }  
    if (indexPath.row == 1) {  
//        第二行cell里面放UITextField  
  
        UITextField *passTF = [[UITextField alloc]initWithFrame:CGRectMake(15, cell.frame.size.height/2, cell.frame.size.width, cell.frame.size.height-10)];  
          
        passTF.backgroundColor = [UIColor clearColor];  
        passTF.placeholder = @"用户密码";  
        passTF.clearButtonMode=YES;  
        passTF.secureTextEntry = YES;//每输入一个字符就变成点 用语密码输入  
  
  
        [cell.contentView addSubview:passTF];  
    }  
      
   return cell;  
      
      
}  
 
(4)调用tabelview
[objc] 
[self initTableView];  
补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,