如何实现“双”UITextField?

时间:2010-03-03 21:37:57

标签: iphone cocoa-touch interface-builder

我正在尝试在这样的视图中生成UITextField。我不介意使用IB或以编程方式进行。

alt text http://echofon.com/img/twitter/iphone/setup.png

2 个答案:

答案 0 :(得分:5)

  1. 新文件(Cocoa Touch Class - > UIViewController子类) UITableViewController子类,使用XIB进行接口(同时检查)
  2. 打开Xib文件
  3. 更改属性检查器上的视图样式(plain-> group)(apple key + 1)
  4. 在.m文件中添加以下代码
  5. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 2;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    
        if(indexPath.row == 0) {
            cell.textLabel.text = @"Username";
            UITextField *field = [[[UITextField alloc] initWithFrame: CGRectMake(120, 10, 180, 30)] autorelease];
            [cell addSubview:field];
    
        }
        else if(indexPath.row == 1) {
            cell.textLabel.text = @"Password";
            UITextField *field = [[[UITextField alloc] initWithFrame: CGRectMake(120, 10, 180, 30)] autorelease];
            field.secureTextEntry = YES;
            [cell addSubview:field];
        }
    
        return cell;
    }
    
    - (NSString *)tableView: (UITableView *)tableView
    titleForHeaderInSection: (NSInteger)section 
    {
        return @"Account";
    }
    
    - (NSString *)tableView: (UITableView *)tableView
    titleForFooterInSection: (NSInteger)section 
    {
        return @"If you don't have a twitter account, go to twitter.com to sign up.";
    }
    

答案 1 :(得分:1)

您需要将UITextField对象放在表中以实现此效果。有关详细信息,请参阅UITableView:http://developer.apple.com/iphone/library/DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html