2012-02-23 11 views
0

データベースのデータをグループ化されたスタイルのテーブルビューに取り込みたいとします。 レコードの数を変更することができます(動的に変更)グループ化されたスタイルのiPhoneでのテーブルビュー

私は試しましたが、見出しのあるテーブルビューで別のセクションを作成できました。

EX:データベースには、5つのフィールド名、住所、連絡先、従業員、技術があります。

だから、私が行ったセクションのヘッダーに名前フィールドの値を入れたい..... しかし、セクションの下に他の4つのフィールドの値を入力しようとすると、1セクションのみ。

第2の問題は、画面を上下にスクロールすると、値が誤って配置されるということです。つまり、ランダムに場所が変更されます。

+1

私は間もなく、いくつかのコードを見ていただくようコメントをいただけると思います。特にテーブルを作成するために使用するテーブル。 –

+1

コードを私たちと共有してください。特にあなたのcellForRowAtIndexPath。 –

+0

私はコードを追加しました.... plzはrowforindex部分を変更します – Mania

答えて

0

numberOfSectionsInTableViewとnumberOfRowsInSectionが正しく行われていることを確認してください。また、各セクション内のセクションと行を識別するために、cellForRowAtIndexPathをコーディングする必要があります。完全な例が必要な場合は、そのTableViewで現在使用しているコードを投稿してください。あなたが望むように変更することができます。

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 = @"[email protected]"; 
      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; 
} 

は、私は、ユーザー名とパスワードのためのちょうど2つのテキストフィールドを作成しました。 else if条件を使用して、必要に応じて連続する行のそれぞれにテキストフィールドを挿入することができます。

- (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 @""; 

} 

だから、ここに私のコードは、ちょうど2つのテキストフィールド(ユーザー名とパスワード)およびログインボタンでログインページを作成するために使用されます。必要に応じて自分のコードを変更することができます。 乾杯!

+1

ありがとうAjit、よい説明。 +1あなたのために。 :) – NSExpression

関連する問題