OGeek|极客世界-中国程序员成长平台

标题: ios - 带有 UITextFields 的 UITableView 部分 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:59
标题: ios - 带有 UITextFields 的 UITableView 部分

我现在尝试了很多天来设置和获取部分单元格中 UITextFields 的输入。

在我的第一个 View 中,我去了一个 UITextField,我在其中说我想要有多少个部分,当按下按钮时,它会在 中为我提供那些部分(4 行) >UITableView。 我现在的问题是,我没有得到每个 UITextField 的正确值。

例如:

这是我的代码....

#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)viewWillAppearBOOL)animated 
{
    [super viewWillAppear:animated];
    [self.myTableView reloadData]; 
}


- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated. 
}

#pragma mark - Table View

-(NSInteger)numberOfSectionsInTableViewUITableView *)tableView {

    int anzahl = [_AnzahlStationen intValue];//from first view...anzahl=quantity of sections

    //adds sections
    do {

        [sectionsArray addObject:[NSString stringWithFormat"alette %lu",sectionsArray.count+1]];

    } while ([sectionsArray count] < anzahl);

    return sectionsArray.count;
}


-(NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {

    return rowsArray.count;
}


-(NSString *)tableViewUITableView *)tableView titleForHeaderInSectionNSInteger)section 
{
    return  [sectionsArray objectAtIndex:section];
}


- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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)textFieldShouldReturnUITextField *)textField {
    [textField resignFirstResponder];
    return YES; 
}

- (void)textFieldDidEndEditingUITextField *)textField {

    [textFieldArray replaceObjectAtIndex:0 withObject:textField.text];
}

@end

如何在正确的行中替换正确的 UITextField?



Best Answer-推荐答案


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

NSMutableArray *tableViewData = [NSMutableArray array];

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

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

您可以随心所欲地动态创建它,并且您的委托(delegate)和数据源方法可以从中读取。而不是拥有 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 数据源和委托(delegate)方法的字典中找到它。

希望这会有所帮助!

关于ios - 带有 UITextFields 的 UITableView 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19550348/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4