• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 带有 UITextFields 的 UITableView 部分

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:59:16 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

例如:

  • 所有部分都有 4 个文本字段
  • 在 textfields 中可以写 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)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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap