在分组样式的iPhone中的表视图

0 iphone cocoa-touch uitableview

我想将数据库中的数据以分组样式填充到表视图中.记录数量可以变化.(动态变化)

我尝试过,但我只能在带有标题的表格视图中创建不同的部分.

对于EX:我的数据库有5个字段NAME,ADDRESS,CONTACT,SALARY,TECHNOLOGY.

所以我想把name字段值放在我做的部分的标题中.....成功但是当我尝试填充该部分下的其他四个字段值时,它将仅填充1个部分.

第二个问题是,当我在屏幕上上下滚动时,值变得错位意味着随机改变它们的位置.

The*_*der 5

首先在.h文件中导入表视图委托,并确保遵循此格式,

@interface CustomtableViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource>
{
    UITextField * username;
    UIButton * submit;
}

@implementation CustomtableViewController

- (void)viewDidLoad
{
    UIView *newView = [[UIView alloc]initWithFrame:CGRectMake(10, 70, 300, 45)];
    submit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [submit setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //[submit setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled];
    [submit setTitle:@"Login" forState:UIControlStateNormal];
    [submit.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
    [submit setFrame:CGRectMake(10.0, 15.0, 280.0, 44.0)];
    [newView addSubview:submit];

    [self.tableView setTableFooterView:newView];

   [super viewDidLoad];

}

  #pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //self.tableView.contentOffset = CGPointMake( 10,  320);
        [self.tableView setContentInset:UIEdgeInsetsMake(50,0,0,0)];
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if ([indexPath section] == 0) {
        username = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
        username.adjustsFontSizeToFitWidth = YES;
        username.textColor = [UIColor blackColor];
        if ([indexPath row] == 0) {
            username.placeholder = @"example@gmail.com";
            username.keyboardType = UIKeyboardTypeEmailAddress;
            username.returnKeyType = UIReturnKeyNext;
            cell.textLabel.text = @"Username";
            username.clearButtonMode = YES;
        }
        else {
            username.placeholder = @"minimum 6 characters";
            username.keyboardType = UIKeyboardTypeDefault;
            username.returnKeyType = UIReturnKeyDone;
            username.secureTextEntry = YES;
            cell.textLabel.text = @"Password";
            username.clearButtonMode = UITextFieldViewModeAlways;
        }
        username.backgroundColor = [UIColor whiteColor];
        username.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
        username.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
        username.textAlignment = NSTextAlignmentLeft;
        username.tag = 0;


        username.clearButtonMode = UITextFieldViewModeAlways; // no clear 'x' button to the right
        [username setEnabled: YES];


    [cell.contentView addSubview: username];

         }

    // Configure the cell...

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

在这里,我只为用户名和密码创建了两个文本字段.您可以使用else if条件根据需要在每个连续行中插入任何no text文本字段.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [NSString stringWithFormat:@"User Login"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 50;
}


- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {

    return @"";

}
Run Code Online (Sandbox Code Playgroud)

所以,我的代码只用于创建一个包含两个文本字段(用户名和密码)和一个登录按钮的登录页面.您可以根据需要修改我的代码.干杯!