UITableViewCell中的UITextField – 防止文本重置的方法

我有一个足够高的UITableView ,它需要滚动。 表中最顶端的单元格包含一个供用户input一些文本的UITextField

构build它的标准方法可能是创build并添加文本字段,并将其添加到在cellFOrRowAtIndexPath:创build或回收的单元格中cellFOrRowAtIndexPath:但是,此不断重新创build意味着在单元格滚出时,字段中input的文本将被删除并回到视图。

到目前为止我发现的解决schemebuild议使用UITextField委托来跟踪文本更改并将其存储在iVar或属性中。 我想知道为什么这是build议,而不是我使用的更简单的方法:

我在UITableViewController的init方法中创buildUITextField ,并立即将其存储在属性中。 在cellFOrROwAtIndexPath我简单地添加预先存在的字段,而不是初始化一个新的。 单元本身可以被回收而没有问题,但是因为我总是使用唯一的UITextField ,所以内容被维护。

这是一个合理的方法吗? 什么可能会出错? 任何改进(也许我仍然可以在cellForRowAtIndexPath创build字段,但首先检查属性是否为零?)

cellForRowAtIndexPath中创build单元格时,必须为第一个单元格(即cellId1)使用一个可重复使用的标识符,其余部分(即,cellId2)使用另一个标识符。

如果你这样做,当你通过调用[tableView dequeueReusableCellWithIdentifier:@"cellId1"]得到第一个元素的单元格时,你将总是得到相同的对象,不会被其他单元格重用。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MyCell *cell = nil; // Only for first row if (indexPath.row == 0) { static NSString *cellId1 = @"cellId1"; cell = [tableView dequeueReusableCellWithIdentifier:cellId1]; if (cell == nil) { cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId1]; } } else { static NSString *cellId2 = @"cellId2"; cell = [tableView cellId2]; if (cell == nil) { cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault cellId2]; } } // do whatever return cell; } 

如果只有一个UITextField,那么我同意你的方法比使用UITextField委托(我认为)更好/相同。

不过,让我们假设您要“展开”您的视图,以便现在有大约7-8个或更多的TextField。 然后,如果你开始使用你的方法,那么问题将是你将在内存中存储7-8个或更多的TextField,并维护它们。

在这种情况下,更好的方法是只创build在屏幕上可见的文本字段数量。 然后你创build一个字典,将维护文本字段中的内容(你可以通过UITextFieldDelegate方法获得)。 这样,当单元格被重用时,可以使用相同的文本字段。 只有值才会改变,并且会被字典中的值决定。

在旁注中,在每个表滚动过程中调用cellForRowAtIndexPath时做最小的创build,因此在cellForRowAtIndexPath中创build一个textField可能会很昂贵。

 #import "ViewController.h" #import "TxtFieldCell.h" #define NUMBER_OF_ROWS 26 @interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITableView *tablView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.tablView.datasource = self; //set textfield delegate in storyboard textFieldValuesArray = [[NSMutableArray alloc] init]; for(int i=0; i<NUMBER_OF_ROWS; i++){ [textFieldValuesArray addObject:@""]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark - TableView Datasource - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TxtFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TxtFieldCellId" forIndexPath:indexPath]; cell.txtField.tag = indexPath.row; if (textFieldValuesArray.count > 0) { NSString *strText = [textFieldValuesArray objectAtIndex:indexPath.row]; cell.txtField.text = strText; } return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return NUMBER_OF_ROWS; } #pragma mark - TextField Delegate - (void)textFieldDidEndEditing:(UITextField *)textField { [textFieldValuesArray replaceObjectAtIndex:textField.tag withObject:textField.text]; }