好吧,大家好,这是问题所在。我加载一个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
任何人都可以请告诉我,我什么做错了?
它只是掩盖了文本字段?我没有看到你曾经设置过表格视图的大小。 – rdelmar
我刚刚编辑了你所说的内容,但它还没有正常工作。 –