【问题标题】:UITableView section with UITextFields带有 UITextFields 的 UITableView 部分
【发布时间】: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


【解决方案1】:

我建议你使用类似这样的数据源:

NSMutableArray *tableViewData = [NSMutableArray array];

[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];

上面的代码代表tableViewData,包含4个数组,每个数组包含一个字典,将保存单个单元格的数据。在我的示例中,我只使用了 UITextField 的单个键值对。

您可以随心所欲地动态创建它,并且您的委托和数据源方法可以从中读取。而不是拥有 3 个单独的数组。

例如

-(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
}

通过使用上述方法,您可以更新此字典,并在一个地方为您的所有 UITableView 维护一个状态,并保持代码的整洁和可读性。要访问任何UITextField,只需在字典中的UITableView 数据源和委托方法中找到它。

希望这会有所帮助!

【讨论】:

  • 非常感谢!下班后我会看看,我需要一些时间来解决这个问题......
  • 到目前为止一切正常,我的部分带有部分标题及其编号,但现在我不明白如何从文本字段中获取值以及如何将它们保存在他们没有得到的文本字段中滚动视图时丢失。
  • 你更新了NSDictionary的TextField里面的文本,然后你从willDisplayCell中读取,这样滚动的时候就不会丢失了。
  • 我很难做到这一点!我可以这样做吗: textfield.text = cellData; ?或者我该怎么做?
  • 类似... textfield.text = (UITextField *)[cellData objectForKey:@"textField"].text;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-31
  • 2013-07-09
  • 2014-01-21
  • 2010-11-06
相关资源
最近更新 更多