gpt4 book ai didi

iOS 如何选择行,当一个人选择它里面的 UITextField 时?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:12:57 26 4
gpt4 key购买 nike

所以我有一个 UITableView,其中所有单元格都有一个 UITextField 作为带有 tag=1 的 subview 。令我困扰的是,当用户单击 textField 并对其进行编辑时,我希望知道发生在哪一行。我认为可以解决的问题是,在选择 subview (UITextField) 时让单元格自行选择。我怎样才能做到这一点?

我尝试使用数组,但由于单元格被重复使用,所以它不起作用。循环遍历所有行太慢了。

最佳答案

默认禁用每个单元格中的 UITextField 并使用您的 UITableView 委托(delegate)方法 didSelectRowAtIndexPath:

  1. 将所选行的索引路径存储在属性中
  2. 启用UITextField
  3. 使 UITextField 成为第一响应者

在类扩展中定义属性:

@interface MyTableViewController ()
@property (strong, nonatomic) NSIndexPath *activeIndex;
@end

在您的 didSelectRowAtIndexPath: 实现中:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
self.activeIndex = indexPath;
AddCell *selectedCell = (AddCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[selectedCell.textField setEnabled:YES];
[selectedCell.textField becomeFirstResponder];
}

UITextField 放弃其第一响应者状态时,您需要再次禁用它。

假设您的 UITableViewController 是每个 UITextField 的委托(delegate),您可以在 UITextFieldDelegate 方法的实现中这样做:

-(void)textFieldDidEndEditing:(UITextField *)textField 
{
[textField setEnabled:NO];
}

关于iOS 如何选择行,当一个人选择它里面的 UITextField 时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17523176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com