UITableView中的代码生成表单未显示

时间:2013-06-29 14:42:43

标签: ios uitableview

我创建了一个UITableViewController和一个用于登录的xib文件,而不使用storyboard。 xib包含一个UITableView,这意味着我必须自己生成单元格。我设置了UITableView的数据源和委托以及像这样的生成表格单元格。

#import "SWFLoginViewController.h"

@interface SWFLoginViewController ()

@end

@implementation SWFLoginViewController

@synthesize userNameField;
@synthesize userPasswordField;
@synthesize user;
@synthesize password;

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    switch (section) {
        case 0:
            return 2;
            break;
        case 1:
            return 1;
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    // Make cell unselectable
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UITextField* tf = nil ;
    switch ( indexPath.row ) {
        case 0: {
            cell.textLabel.text = NSLocalizedString(@"ui.username", @"");
            tf = userNameField = [self makeTextField:self.user placeholder:NSLocalizedString(@"ui.username", @"")];
            [cell addSubview:userNameField];
            break ;
        }
        case 1: {
            cell.textLabel.text = NSLocalizedString(@"ui.password", @"");
            tf = userPasswordField = [self makeTextField:self.password placeholder:NSLocalizedString(@"ui.password", @"")];
            [cell addSubview:userPasswordField];
            break ;
        }
    }

    // Textfield dimensions
    tf.frame = CGRectMake(120, 12, 170, 30);

    // Workaround to dismiss keyboard when Done/Return is tapped
    [tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];

    // We want to handle textFieldDidEndEditing
    tf.delegate = self ;

    return cell;
}



-(UITextField*) makeTextField: (NSString*)text
                  placeholder: (NSString*)placeholder  {
    UITextField *tf = [[UITextField alloc] init];
    tf.placeholder = placeholder ;
    tf.text = text ;
    tf.autocorrectionType = UITextAutocorrectionTypeNo ;
    tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
    tf.adjustsFontSizeToFitWidth = YES;
    tf.textColor = [UIColor colorWithRed:56.0f/255.0f green:84.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
    return tf ;
}

// Workaround to hide keyboard when Done is tapped
- (IBAction)textFieldFinished:(id)sender {
    // [sender resignFirstResponder];
}

// Textfield value changed, store the new value.
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ( textField == userNameField ) {
        self.user = textField.text ;
    } else if ( textField == userPasswordField ) {
        self.password = textField.text ;
    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

头文件:

#import <UIKit/UIKit.h>

@interface SWFLoginViewController : UITableViewController <UITextFieldDelegate,UITableViewDataSource, UITableViewDelegate>

// Creates a textfield with the specified text and placeholder text
-(UITextField*) makeTextField: (NSString*)text
                  placeholder: (NSString*)placeholder  ;

// Handles UIControlEventEditingDidEndOnExit
- (IBAction)textFieldFinished:(id)sender ;

@property (nonatomic,strong) UITextField* userNameField;
@property (nonatomic,strong) UITextField* userPasswordField;
@property (nonatomic,copy) NSString* user ;
@property (nonatomic,copy) NSString* password ;

@end

当我在iPod中启动我的应用程序时,除了背景纹理之外别无他物。

我已经在这里停留了一段时间,我真的不知道出了什么问题。然后我在numberOfRowsInSection放了一个断点,并且它一直没有被击中。

请帮忙。

1 个答案:

答案 0 :(得分:2)

设置数据源(数组或等效项)后,您应至少拨打一次[yourTableView reloadData]

由于此处您没有任何此类数据源,因此最好在viewDidAppearviewDidLoad内进行调用。更好的是在登录视图控制器文件中有一个UITableView属性和插座,然后你有一个点可以进行这个调用。

相关问题