-1

我正在努力实现这一目标:

在此处输入图像描述

但我明白了:

在此处输入图像描述

我有一个视图控制器,上面有一个视图表

这是界面:

@interface LoginViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tblCredentials;

@end

这是实现:

@interface LoginViewController ()

@end

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

-(void)viewWillAppear:(BOOL)animated
{
    self.tblCredentials.delegate=self;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UITextField *textField = [[UITextField alloc] init];
    textField.enablesReturnKeyAutomatically = YES;
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;

    CGRect cellBounds = cell.bounds;
    CGFloat textFieldBorder = 10.f;
    CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );

    textField.frame = aRect;

    if(indexPath.row==0)
    {
        textField.placeholder = @"Username";
        textField.returnKeyType = UIReturnKeyNext;
        textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    }
    else
    {
        textField.placeholder = @"Password";
        textField.returnKeyType = UIReturnKeyDone;
        textField.secureTextEntry = YES;
    }

    [cell.contentView addSubview:textField];

    return cell;
}


@end

我在 cellForRowAtIndexPath 中放了一个断点,它并没有停在那里,所以这些文本字段不会被渲染。

我错过了什么?

PS:这是实现目标的坏方法吗?(这两个分组的文本字段)

LE:我正在使用没有 xib 文件的 stroyboard

4

4 回答 4

1

在 viewDidLoad 中,您必须设置委托和调用[self.tblCredentials reloadData]才能让表视图真正“加载其数据”

于 2013-08-04T07:43:36.897 回答
1

您需要创建一个自定义表格视图单元格。看看这个github 链接

于 2013-08-04T07:56:39.453 回答
0

您正在设置 table view 的委托,而不是datasource,这是行数等的来源。

您还在周期中将委托设置得有点晚。既然这是在 xib 中,为什么不在 xib 中而不是在代码中设置委托和数据源?如果您声明您的视图控制器符合标头中的委托和数据源属性,您将能够在 IB 中建立连接。如果非要在代码中设置,应该在viewDidLoad中。

于 2013-08-04T07:49:53.723 回答
0

在-viewDidLoad 中设置delegate 和dataSource 并放入[self.tblCredentials reloadData]-viewWillAppear:。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tblCredentials.delegate=self;
    self.tblCredentials.dataSource=self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated]; // BTW, it's better to call super's -viewWillAppear: here, according to apple's documentation.
    [self.tblCredentials reloadData];
}
于 2013-08-04T08:31:00.567 回答