cellForRowAtIndexPath不会触发。我错过了什么?

时间:2013-08-04 07:38:22

标签: objective-c uitableview

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

enter image description here

但我明白了:

enter image description here

我有一个带有视图表的视图cotroller

这是界面:

@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 个答案:

答案 0 :(得分:1)

在viewDidLoad中,您必须设置委托并调用[self.tblCredentials reloadData]以使表视图实际“加载其数据”

答案 1 :(得分:1)

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

答案 2 :(得分:0)

您正在设置表视图的委托,而不是数据源,这是行数等来自的位置。

您还在周期中稍稍设置了委托。由于这是在xib中,为什么不在xib中而不是在代码中设置委托和数据源?如果声明视图控制器符合标题中的委托和数据源属性,则可以在IB中建立连接。如果你坚持在代码中设置它,它应该在viewDidLoad中。

答案 3 :(得分: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];
}