[英]UITableView section with UITextFields
I am trying for many days now to set and get inputs of UITextFields
in cells of sections. 我现在尝试了很多天,以在部分单元格中设置和获取UITextFields
输入。
In my first view i have go a UITextField
where I say how many sections I want to have, and when pressing a button, it gives me that sections (with 4 rows) in the UITableView
. 在我的第一个视图中,我进入了一个UITextField
,我在其中说了我想拥有多少个部分,按下按钮时,它给了我UITableView
中的这些部分(具有4行)。 my problem now is, that I don't get the correct values of each UITextField
. 我现在的问题是,我没有获得每个UITextField
的正确值。
for example: 例如:
here is my code.... 这是我的代码。
#import "ViewController.h"
@interface ViewController ()
{
NSMutableArray *sectionsArray;
NSMutableArray *rowsArray;
NSMutableArray *textFieldArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.
[[self myTableView]setDelegate:self];
[[self myTableView]setDataSource:self];
sectionsArray = [[NSMutableArray alloc]init];
rowsArray = [[NSMutableArray alloc]initWithObjects:@"",@"",@"",@"", nil];
textFieldArray = [[NSMutableArray alloc]initWithObjects:@"",@"",@"",@"", nil];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.myTableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
int anzahl = [_AnzahlStationen intValue];//from first view...anzahl=quantity of sections
//adds sections
do {
[sectionsArray addObject:[NSString stringWithFormat:@"Palette %lu",sectionsArray.count+1]];
} while ([sectionsArray count] < anzahl);
return sectionsArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return rowsArray.count;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [sectionsArray objectAtIndex:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 24)];
textField.font = [UIFont fontWithName:@"Gujarati Sangam MN" size:15];
textField.textColor = [UIColor redColor];
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
textField.tag = 17;
[cell.contentView addSubview:textField];
textField.delegate = self;
}
UITextField *textField = (UITextField *)[cell.contentView viewWithTag:17];
textField.text = [textFieldArray objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textFieldArray replaceObjectAtIndex:0 withObject:textField.text];
}
@end
How can I replace the correct UITextField in the correct row? 如何在正确的行中替换正确的UITextField?
I recommend you use a datasource similar to this: 我建议您使用类似于以下的数据源:
NSMutableArray *tableViewData = [NSMutableArray array];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
The above code represents tableViewData
, containing 4 arrays, each array contains one dictionary which will maintain the data for a single cell. 上面的代码表示tableViewData
,其中包含4个数组,每个数组包含一个字典,该字典将维护单个单元格的数据。 In my example I have just used a single key-value pair of a UITextField
. 在我的示例中,我只使用了UITextField
的单个键值对。
You can dynamically create this however you like, and your delegate and datasource methods can read from it. 您可以根据需要动态创建它,并且委托和数据源方法可以从中读取。 Rather than having 3 separate arrays. 而不是拥有3个单独的数组。
Eg 例如
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [tableViewData count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[tableViewData objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *cellData = [[tableViewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
// Create your cell and use the data stored in the dictionary
}
By using the above approach, you are able to update this dictionary, and maintain a state for all of your UITableView
in a single place, and it keeps the code tidy and readable. 通过使用上述方法,您可以更新此字典,并在单个位置维护所有UITableView
的状态,并且可以使代码整洁和可读。 To access any UITextField
, just find it in the dictionary in the UITableView
datasource and delegate methods. 要访问任何UITextField
,只需在UITableView
数据源和委托方法的字典中找到它即可。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.