【问题标题】:How to get the value of textfield in a tableview cell on click of a button?如何在单击按钮时获取表格视图单元格中文本字段的值?
【发布时间】:2017-08-16 09:06:21
【问题描述】:

我有一个表格视图,用户可以在其中输入他的社交媒体个人资料网址。我想在单击按钮时获取该文本字段的值。表格视图是动态的。我附上了截图供参考。我正在使用 Objective C 来构建一个 iOs 应用程序,我是新手。关于如何做到这一点的任何建议?提前致谢。

[![这是单元格中带有文本字段的表格视图]

我已经试过了:

NSMutableArray *Array = [NSMutableArray array];

for (int i=0; i < getMYurl.count; i++){

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow: i inSection: 0];

    SHOWURLTableViewCell *cellS = [table cellForRowAtIndexPath:indexPath];

    NSMutableDictionary *Dict = [NSMutableDictionary dictionary];

    UITextField *TextName= (UITextField*)[cellS.contentView viewWithTag:indexPath.row];
}

还有我的 cellForRowAtIndexPath 方法:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cellS  = (SHOWURLTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    cellS.labelDesc.text = mergeName;


    NSString *urlText = [[getMYurl valueForKey:@"url"]objectAtIndex:indexPath.row];
    cellS.labelUrl.text = urlText;

    NSString *urlName = [[getMYurl valueForKey:@"url_name"]objectAtIndex:indexPath.row];

//    [urlMap setValue:mergeName forKey:urlText];

    cellS.urlNameET.tag = 100;
}

【问题讨论】:

  • NSMutableArray Array = [NSMutableArray 数组]; for (int i=0; i )[cellS.contentView viewWithTag:indexPath.row]; }
  • 显示你的 cellforRow 方法
  • 您应该在结束输入后将 textfiled 的文本存储在任何数组变量中(索引方式)。因为 tableview 破坏了 tableview 单元格,而它没有显示在屏幕上。
  • @PramodTapaniya 哦,我试试。顺便说一句,谢谢..
  • 你能解释一下 getMYurl dict 中的 url 和 url_name 是什么吗?

标签: ios objective-c iphone uitableview


【解决方案1】:

您应该在结束输入后将 textfiled 的文本存储在任何数组变量中(索引方式)。因为 tableview 破坏了 tableview 单元格,而它没有显示在屏幕上。

- (void)textFieldDidEndEditing:(UITextField *)textField{       
          UITableViewCell *cell = (UITableViewCell*) textField.superview.superview;
          NSIndexPath *txtIndPath = [self.tblPendingDeliveryData indexPathForCell:cell];

          [myMutableArray insertObject:textField.text atIndex:txtIndPath.row];
    }

不要忘记将委托设置为文本字段

【讨论】:

  • 我在 cellForRowAt 方法的 cell.textfield 上将委托设置为 self 并使用了您的方法,但数组为空白。
  • 如果解决了您的问题,请标记为正确答案。
  • 是的,正在调用该方法。我在方法中输入了一个日志。日志被打印出来。
  • 如何声明数组?
  • mergeAllData = [[NSMutableArray alloc]init];
猜你喜欢
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多