在自定义UITableViewCell中管理UITextField的代理 [英] Managing Delegate of UITextFields in Custom UITableViewCell
问题描述
所以我已经看了很多,这里的任何内容似乎都没有解释这样做的正确方法。我在自定义UITableViewCell中有7个UITextField。
So I have looked around a quite a bit, and nothing on here seems to explain exactly the correct way of doing this. I have 7 UITextFields within a custom UITableViewCell.
我的问题是:管理这些UITextFields委托的正确方法是什么?
My question is this: What is the correct way of managing the delegate of these UITextFields?
由于自定义单元格在技术上是项目模型部分的一部分,我宁愿让控制UITableView的控制器也控制表格中的文本字段。单元格,但我无法弄清楚如何将文本字段(在UITableViewCell的子类中创建)的委托设置为此视图控制器。
Since the custom cells are technically part of the "model" portion of the project, I would rather have the controller that controls the UITableView also control the text fields in the table's cells, but I can not figure out how to set the delegate for the text fields (which are created in the subclass of UITableViewCell) to this view controller.
是否为只是让UITableViewCell的子类符合UITextField委托并管理其中的所有内容的不良做法?如果是这样,我该怎么办呢?
Would it be bad practice to just make the subclass of UITableViewCell conform to UITextField delegate and manage all of that stuff in there? If so, how else should I go about doing this?
谢谢,任何帮助都将不胜感激。
Thanks, any help would be appreciated.
推荐答案
将单元格的文本字段的委托设置为视图控制器应该没有问题。
You shouldn't have a problem setting the delegate of the cell's text field to be your view controller.
这是你需要做的:
1)视图控制器需要实现 UITextFieldDelegate
协议
1) The view controller needs to implement the UITextFieldDelegate
protocol
2)为自定义单元格中的文本字段声明属性
2) Declare a property for the text field in your custom cell
@property (nonatomic, retain) IBOutlet UITextField *textField;
3)然后在方法中将视图控制器设置为文本字段的委托cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
// use this if you created your cell with IB
cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil] objectAtIndex:0];
// otherwise use this
cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// now set the view controller as the text field delegate
cell.textField.delegate = self;
}
// configure cell...
return cell;
}
这篇关于在自定义UITableViewCell中管理UITextField的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!