2012-12-13 30 views
-1

好吧,大家好,这是问题所在。我加载一个UIView内的UITableView,它不是显示两个文本框:UIView里面的UITableView无法像UITextField一样按预期工作

.H:

#import <UIKit/UIKit.h> 

@interface LoginViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> { 

    UITableView *loginTableView; 
    UITextField *username; 
    UITextField *password; 

} 

@property UITableView *loginTableView; 
@property UITextField *username; 
@property UITextField *password; 

@end 

.M:

#import <QuartzCore/QuartzCore.h> 
#import "LoginViewController.h" 

@interface LoginViewController() 

@end 

@implementation LoginViewController 

@synthesize loginTableView, username, password; 

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    // Make rounded corners view 
    [self.view.layer setCornerRadius:4.0]; 
    [self.view.layer setMasksToBounds:YES]; 
    self.view.layer.opaque = NO; 
    self.view.backgroundColor = [UIColor whiteColor]; 

    // Add login table view to main view 
    loginTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; 
    loginTableView.delegate = self; 
    loginTableView.dataSource = self; 
    [self.view addSubview:loginTableView]; 

} 

- (void)didReceiveMemoryWarning { 

    [super didReceiveMemoryWarning]; 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    return 2; 

} 

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"Cell"]; 

    if(cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    } 
    if (indexPath.row == 0) { 
     username = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)]; 
     username.placeholder = @"Username"; 
     username.autocorrectionType = UITextAutocorrectionTypeNo; 
     [username setClearButtonMode:UITextFieldViewModeWhileEditing]; 
     cell.accessoryView = username; 
    } 
    if (indexPath.row == 1) { 
     password = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)]; 
     password.placeholder = @"Password"; 
     password.secureTextEntry = YES; 
     password.autocorrectionType = UITextAutocorrectionTypeNo; 
     [password setClearButtonMode:UITextFieldViewModeWhileEditing]; 
     cell.accessoryView = password; 
    } 

    username.delegate = self; 
    password.delegate = self; 

    [loginTableView addSubview:username]; 
    [loginTableView addSubview:password]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell; 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 

    return 1; 

} 

@end 

任何人都可以请告诉我,我什么做错了?

+0

它只是掩盖了文本字段?我没有看到你曾经设置过表格视图的大小。 – rdelmar

+0

我刚刚编辑了你所说的内容,但它还没有正常工作。 –

回答

1

它看起来就像是增加了文本框在tableview中,而不是实际的细胞

[loginTableView addSubview:username]; //you're saying to add it to the table 

另外,您要添加的username两次

编辑:

You may want to check out this other SO question

+0

是的!我对此很清楚。那是错的吗?我刚刚编辑了你所说的关于重复错误的内容,但尚未正常工作。 –

+0

,这样你工作的函数在表格呈现时绘制每个表格单元格,然后将该单元格添加到表格本身(非常高级别的解释)..您可以看到该函数返回一个'UITableViewCell',所以按顺序要添加这些视图,您必须将它们添加到单元格而不是表格。我会打开一个项目,我有几个,并得到你需要的确切的行 – AngeloS

+0

我编辑了我的答案,包含一个链接到另一个SO问题,我用一个tableview实现登录功能。如果你不想创建一个登录,这也会帮助你,因为它将子视图添加到单元格 – AngeloS