0

我需要帮助添加逻辑以获取静态表视图单元格部分索引。

我有一个带有 2 个部分的普通静态 UITableView,每个部分中有 2 个带有文本字段的单元格。

我实现了一个代码,用于将带有 prev/next/done 按钮的工具栏添加到键盘。

如果文本字段位于第 0 部分,则上一个/下一个工作,但是当点击下一步转到第 1 部分中的下一个文本字段时,应用程序崩溃。

我应该在哪里放置“if 条件”来检查文本字段所在的部分?

这是我的 .h 文件

#import <UIKit/UIKit.h>

@interface TextFieldTVCViewController : UITableViewController <UITextFieldDelegate, UIAlertViewDelegate>

@property(strong, nonatomic) IBOutlet UITextField *dep1TextField;
@property(strong, nonatomic) IBOutlet UITextField *arr1TextField;
@property(strong, nonatomic) IBOutlet UITextField *dep2TextField;
@property(strong, nonatomic) IBOutlet UITextField *arr2TextField;

@property(strong, nonatomic) IBOutlet UIToolbar *keyboardToolbar;
@property(strong, nonatomic) IBOutlet UIBarButtonItem *toolbarActionButton;

@property(strong, nonatomic) NSArray *textFields;

- (IBAction) closeKeyboard;
- (IBAction) nextField;
- (IBAction) prevField;

@end

这是我的 .m 文件

#import "TextFieldTVCViewController.h"

@interface TextFieldTVCViewController ()

@end

@implementation TextFieldTVCViewController

@synthesize dep1TextField, dep2TextField, arr1TextField, arr2TextField;
@synthesize keyboardToolbar, toolbarActionButton;
@synthesize textFields;


- (void)viewDidLoad
{
 [super viewDidLoad];
 textFields = [[NSArray alloc] initWithObjects:dep1TextField, arr1TextField, dep2TextField, arr2TextField, nil];

}

- (void) textFieldDidBeginEditing:(UITextField *)textField{
[textField setInputAccessoryView:keyboardToolbar];
for (int i=0; i<[textFields count]; i++) {
    if ([textFields objectAtIndex:i]==textField) {
        if (i==[textFields count]-1) {
            toolbarActionButton.title =@"Done";
            [toolbarActionButton setStyle:UIBarButtonItemStyleDone];
        }
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
}
}

- (IBAction) closeKeyboard{
for(UITextField *t in textFields){
    if ([t isEditing]) {
        [t resignFirstResponder];
        break;
    }
}
}

- (IBAction) nextField{
for (int i=0; i<[textFields count]; i++) {

    if ([[textFields objectAtIndex:i] isEditing] && i!=[textFields count]-1) {
        [[textFields objectAtIndex:i+1] becomeFirstResponder];
        if (i+1==[textFields count]-1) {
            [toolbarActionButton setTitle:@"Done"];
            [toolbarActionButton setStyle:UIBarButtonItemStyleDone];
        }else {
            [toolbarActionButton setTitle:@"Close"];
            [toolbarActionButton setStyle:UIBarButtonItemStyleBordered];
        }

        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i+1 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];


        break;
    }
}

}

- (IBAction) prevField{
for (int i=0; i<[textFields count]; i++) {
    if ([[textFields objectAtIndex:i] isEditing] && i!=0) {
        [[textFields objectAtIndex:i-1] becomeFirstResponder];
        [toolbarActionButton setTitle:@"Close"];
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i-1 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
        [toolbarActionButton setStyle:UIBarButtonItemStyleBordered];

        break;
    }
}
}
@end

我从中获取工具栏代码的站点:

http://idebuggerman.blogspot.com/2010/08/uitoolbar-for-keyboard-with.html?showComment=1371639814883

谢谢

4

1 回答 1

0

我认为您保留一系列文本字段的方法不是很干净。这会导致代码非常复杂,并且您必须进行大量迭代,这很容易出错。

相反,使用枚举来标记文本字段。在“标签”下的情节提要中插入适当的数字。在您的实现文件之上,定义您的字段。

typedef enum { 
   Dep1Field = 100, Arr1Field,
   Dep2Field,       Arr2Field, 
   NumberOfFields
} TableFields;

现在您可以轻松识别委托回调和方法中的文本字段。我建议在 ivar 中保留对已编辑文本字段的引用。您可以在标准 UITextField 委托回调中设置它。

@implementation ViewController {
    UITextField *_textFieldBeingEdited;
}
//...

“下一个”和“上一个”逻辑现在很简单:

// next
if (textFieldBeingEdited && textFieldBeingEdited.tag < NumberOfFields-1) {
    [textFieldBeingEdited resignFirstResponder];
    UITextField *nextTextField = 
         (UITextField*)[self.view viewWithTag:textFieldBeingEdited.tag+1];
    [nextTextField becomeFirstResponder]; 
}
于 2013-06-19T17:03:39.670 回答