【问题标题】:iOS Getting Reference of Subview on UITableViewCelliOS 在 UITableViewCell 上获取子视图的参考
【发布时间】:2014-01-22 19:10:03
【问题描述】:

我正在使用UITableView 创建自定义控件。此控件只是一个在一个部分中包含多行的表格视图。我们称之为MyCustomControl

在表格视图的每个单元格中,我添加了一个UITextField 控件。

----------------
| ------------ |
|| Text Field || -> CELL
| ------------ |
----------------

这个单元格只是默认的UITableViewCell,我用[cell addSubview:textField];添加了文本字段

我使用以下方法访问表格视图上的每个文本字段组件:

- (UITextField*) textFieldAtIndex:(NSInteger)index
{
    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:index inSection:0];

    UITableViewCell* cell = [self tableView:[self containerTableView] cellForRowAtIndexPath:indexPath];

    UITextField* textField = [[cell subviews] lastObject];

    return textField; // It should returning the reference of textField right??
}

曾几何时,我在项目的某个视图控制器中使用了这个MyCustomControl

- (void) viewDidLoad
{
    [super viewDidLoad];

    CGPoint origin = CGPointMake(100.f, 100.f);

    CGFloat width = 200.f;

    // Create custom control with text field generated as many as given parameter
    MyCustomControl* textGroup = [[MyCustomControl alloc] initWithOrigin:origin width:width textFieldCount:2];

    // Here is the problem, I try to set current view controllers's text fields 
    // with the text field generated by MyCustomControl.
    // Text field returned are new text field with same properties instead of reference.
    self.txtUsername = [textGroup textFieldAtIndex:0]; // Text Field are copied!
    self.txtPassword = [textGroup textFieldAtIndex:1]; // Not return a pointer!

    [self.txtUsername setDelegate:self]; // Fail
    [self.txtPassword setDelegate:self]; // Fail

    [[self view] addSubview:textGroup]; 

    [[self view] addSubview:[self txtUsername]]; // New text field appeared on view
}

我希望能够完全控制从方法 textFieldAtIndex: 访问的 MyCustomControl 中的文本字段,但我没有引用该文本字段,而是在我的视图控制器中获得了文本字段的新副本。而且我不能为该文本字段设置委托,也不能像它的文本一样设置所有其他内容。

如何从该表格视图单元格中的文本字段中获取引用?

【问题讨论】:

  • 是自定义单元格吗?
  • 不,它不是自定义单元格。

标签: ios uitableview


【解决方案1】:

检查我对此的回答,他们想要一个文本字段,但过程是相同的,比这里的其他一些解决方案干净得多:

How to get UITableView Label Text string - Custom Cell

假设你知道索引路径。

【讨论】:

  • 是的。我从来没想过!多可惜!我只需要将其设为自定义单元格,然后将文本字段设为单元格的属性。再次,你是对的兄弟。非常感谢您的帮助。 :D
  • 你明白了。不用担心,除非它们是自定义单元格,否则您不应该真正将子视图添加到单元格中,这使得做这种事情变得更加困难......快乐编码:-)
【解决方案2】:

将您的自定义单元格类和文本字段的 IBOutlet 设置为它 在 cellForRowAtIndexPath 中不需要的地方隐藏此文本字段

Textfield 具有 touchDown 操作方法 在您创建新的时设置它(与按钮默认具有 touchUpInside 相同 - 您可以通过选择控件并单击来查看此选项在连接检查器上)

从下面的方法你可以得到你的文本字段的参考

- (IBAction)actionTextField:(id)sender {

UIView *view = sender.superview;
while (view && ![view isKindOfClass:[UITableViewCell self]]) view = view.superview;

UITableViewCell *cell = (UITableViewCell *)view;
indexPathForDeletion = [self.tableViewListViewVC indexPathForCell:cell];
NSLog(@"cell is in section %d, row %d", indexPathForDeletion.section, indexPathForDeletion.row);


}

【讨论】:

    【解决方案3】:

    自定义控件类中的 tableview 单元格不是在您创建自定义类的实例时创建的,因此它建议您在创建文本字段之前分配文本字段,这是我猜的问题。 要从每个单元格中获取文本字段的引用,您可以在创建单元格时为每个文本字段赋予标签值,并且可以在当前控制器中随时通过标签值访问它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多