【发布时间】:2023-03-25 10:25:01
【问题描述】:
我现在尝试了很多天来设置和获取部分单元格中UITextFields 的输入。
在我的第一个视图中,我去了UITextField,在那里我说我想要有多少个部分,当按下按钮时,它会在UITableView 中给我那些部分(4 行)。
我现在的问题是,我没有得到每个 UITextField 的正确值。
例如:
- 所有部分都有 4 个文本字段
- 在文本字段中可以写 int 或 float
- 当我知道文本字段 1 和 2 的 int 时,我将它们分开...
这是我的代码....
#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
如何在正确的行中替换正确的 UITextField?
【问题讨论】:
-
理想情况下,您不应该在数据源回调中将对象添加到数据源中。为什么不在 viewDidLoad 中这样做呢?你的问题很难理解你在问什么。
-
那么,首先我应该在 viewdidload 中添加这些部分?
-
在别处创建所有部分数据,然后您的委托回调可以通过这种方式检索计数。关于你的实际问题,我还是不明白
-
好的,什么是有效的:我得到了很多我想要的部分......每个部分都有 4 个文本字段。我如何获取文本字段的值或字符串?
-
您应该在数组的数组中维护每个 UITextField 的数据。例如。一个包含多个数组的数组,其中每个数组代表一个节,在每个节数组中,一个字典表示行数据。而不是拥有 3 个单独的数组
标签: ios cocoa-touch uitableview sections