在 uitableview 的文本字段之间导航
我正在制作一个 coredata 应用程序,其中指示我制作一个表格视图(填写表单),并在每一行中使用文本字段,我成功地做到了。然后,我制作了一个带有上一个和下一个按钮的工具栏,并将其添加到 textField 的 inputAccsoryView 中,以便在每个单元格的文本字段之间导航。每行一个文本字段。我还可以使用上一个和下一个按钮方法来实现魔法。现在的问题是:
假设我有 3 个部分。在第一部分 4 行。第二部分 6 行,最后部分 1 行。现在,当我从第一行开始并按“下一步”直到第 11 行时,它工作正常,但然后我按“下一步”,因为第一行已出列,所以没有任何反应,我得到“nil”。我正在使用的代码:
配置单元格,
if (cell == nil) {
NSLog(@"tag inside:%i",tag);
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:identifier] autorelease];
UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 170, 25)];
theTextField.textAlignment = UITextAlignmentRight;
theTextField.delegate = self;
theTextField.tag = tag;
theTextField.inputAccessoryView = [self configureTheToolBar];
[theTextField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[cell.contentView addSubview:theTextField];
[theTextField release];
}
cell.textLabel.text = rowLabel;
UITextField *textField = (UITextField *)[cell.contentView viewWithTag:tag];
textField.text = rowValue
我为文本字段提供标签,如 601,602 等。
然后获取 currentTextField 标记,
- (void)textFieldDidBeginEditing:(UITextField *)textField{
currentTextField = textField.tag;
}
然后使用下一个方法
if(currentTextField == 611){
currentTextField = 601;
} else{
currentTextField = currentTextField + 1;
}
NSLog(@"current text fiedld = %i",currentTextField);
NSLog(@"text ; %@",[self cellForTag:currentTextField].textLabel.text);
UITextField *firstResponderField = (UITextField *) [[self cellForTag:currentTextField].contentView viewWithTag:currentTextField];
[firstResponderField becomeFirstResponder];
和 cellForTag 方法:
-(UITableViewCell *)cellForTag:(NSInteger)tag{
UITableViewCell *cell;
switch (tag) {
case 601:{
NSUInteger onlyRow[] = {0, 0};
NSIndexPath *onlyRowPath = [NSIndexPath indexPathWithIndexes:onlyRow length:2];
//[self.tableView scrollToRowAtIndexPath:onlyRowPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
cell = [self.tableView cellForRowAtIndexPath:onlyRowPath];
break;
}
return cell
此代码部分工作,因为一旦屏幕外的单元格内存不足,我无法到达该单元格中的文本字段,并且下一个按钮将停止工作,直到 currentTextfield 值到达任何可见行。可能有什么解决办法。 我的堆栈溢出了。 :)
好的这个线程可以解决问题,但我不这样做不知道怎么办。谁能解释一下怎么做?
I am making an coredata application in which I was instructed to make a tableview (filling form) with textfield in each row which I did successfully. Then I made a toolbar with previous and next button and add to textField's inputAccsoryView for navigating between the textfields of each cell. One textfield per row . I am also able to do the magic with previous and next button methods . Now the problem :
Suppose I have 3 sections . In first section 4 rows . In second section 6 rows and in the last section 1 row . Now when I start from the 1st row and presses next till the 11th row it is working properly but then I press next nothing happens as the first row is dequeued I get "nil" for it . The code I am using :
configuring the cell
if (cell == nil) {
NSLog(@"tag inside:%i",tag);
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:identifier] autorelease];
UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 170, 25)];
theTextField.textAlignment = UITextAlignmentRight;
theTextField.delegate = self;
theTextField.tag = tag;
theTextField.inputAccessoryView = [self configureTheToolBar];
[theTextField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[cell.contentView addSubview:theTextField];
[theTextField release];
}
cell.textLabel.text = rowLabel;
UITextField *textField = (UITextField *)[cell.contentView viewWithTag:tag];
textField.text = rowValue
I am providing tags for textfield like 601 ,602 etc .
Then getting the currentTextField tag
- (void)textFieldDidBeginEditing:(UITextField *)textField{
currentTextField = textField.tag;
}
then the next method
if(currentTextField == 611){
currentTextField = 601;
} else{
currentTextField = currentTextField + 1;
}
NSLog(@"current text fiedld = %i",currentTextField);
NSLog(@"text ; %@",[self cellForTag:currentTextField].textLabel.text);
UITextField *firstResponderField = (UITextField *) [[self cellForTag:currentTextField].contentView viewWithTag:currentTextField];
[firstResponderField becomeFirstResponder];
and the cellForTag method :
-(UITableViewCell *)cellForTag:(NSInteger)tag{
UITableViewCell *cell;
switch (tag) {
case 601:{
NSUInteger onlyRow[] = {0, 0};
NSIndexPath *onlyRowPath = [NSIndexPath indexPathWithIndexes:onlyRow length:2];
//[self.tableView scrollToRowAtIndexPath:onlyRowPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
cell = [self.tableView cellForRowAtIndexPath:onlyRowPath];
break;
}
return cell
this code is working partially as once off screen cells get out of memory I can't reach textfield in that cell and next button stops working till the currentTextfield value reaches to any visiblerow . What could be the solution . my stack is over flowed. :)
Ok this thread does the trick but I don't know how . can anyone explain me how ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一旦单元格离开屏幕,它将不再可访问。因此,您可能希望在用户完成编辑后立即通过连接 UIControlEventEditingDidEnd 事件来获取文本字段的值:
在 myTextChanged 函数中,您可以将值存储起来,如果您想保留这些值,可以将其存储在字典中他们针对您的标签进行键入。
Once a cell goes off screen it will no longer be accessible. Because of this you might want to grab the values of your text fields as soon as the user has finished editing them by wiring up the UIControlEventEditingDidEnd event:
In your myTextChanged function you can store the values away, perhaps in a dictionary if you want to keep them keyed against your tags.