【发布时间】: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