-1

Ok guys so here's the problem. I'm loading a UITableView inside a UIView and it's not displaying the two textfields:

.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

Anyone could please tell me what I'm doing wrong?

1 Answers1

1

It looks like you're adding the textfields to the tableview and not the actual cell

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

Also, you're adding the username twice

EDIT:

You may want to check out this other SO question

Community
  • 1
  • 1
AngeloS
  • 5,248
  • 5
  • 38
  • 58
  • Yep! I'm aware of that. Is that wrong? I've just edited what you've said about the repetition error and it's not working yet. – Roger Fernandez Guri Dec 13 '12 at 23:34
  • so that function you're working in draws each table cell when the table is rendered and then adds the cell to the table itself (very high level explanation) .. you can see that that function returns a `UITableViewCell`, so in order to add those views, you must add them to the cell and not the table. I'll open up a project i have in a few and get the exact line you need – AngeloS Dec 13 '12 at 23:38
  • I edited my answer to contain a link to another SO question that I used to implement a login feature using a tableview. If you're not trying to create a login, that'll help you too because it adds the subview to the cell – AngeloS Dec 13 '12 at 23:42
  • I tried your code but I just got an empty tableView without the textfields not quite the image you're showing in your post. I love to have that. – Roger Fernandez Guri Dec 13 '12 at 23:56
  • I think you're missing `loginTableView.delegate = self` and `loginTableView.datasource = self` in your view did load. Can you set breakpoints to see if that function is being called? – AngeloS Dec 13 '12 at 23:58
  • Also, add those lines of code before you add the tableview to your uiview and after you initialize your tableview – AngeloS Dec 14 '12 at 00:03
  • Ok AngeloS i got it working thanks to your advise! THANK YOU! Now it's just that it crashes when I try to add the email or password. – Roger Fernandez Guri Dec 14 '12 at 00:09