IPhone table view in grouped style

I want to populate data from a database as a table in a grouped style. The number of entries can be changed. (Dynamic change)

I tried, but I can just create different sections as a table with a heading.

for EX: my database has 5 fields NAME, ADDRESS, CONTACT, SALARY, TECHNOLOGY.

So, I want to put the value of the name field in the section header, which I did ..... successfully, but when I try to fill in the remaining four field values ​​in the section, then it will be filled only for 1 section.

The second problem is that when scrolling up and down the screen, the values ​​become inappropriate, randomly changes its place.

0
source share
2 answers

Make sure you have numberOfSectionsInTableView and numberOfRowsInSection is done correctly. In addition, cellForRowAtIndexPath must be encoded to identify the sections and row in each section. If you need a complete example, submit the code you are using now for this TableView, and I can modify it to do what you want.

0
source

First import the table view delegates into your .h file and make sure you follow this format,

@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; } 

Here I created only two text fields for username and password. You can use the else if clause to insert any number of text fields into each of the consecutive lines according to your needs.

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

So, my code here is only used to create a login page with two text fields (Username and Password) and a Login button. You can change my code according to your needs. Hooray!

+5
source

Source: https://habr.com/ru/post/1340565/


All Articles