2013-02-25 29 views
1

我有具有相當數量的細胞在其用於創建形式的表格視圖。我創建了一個自定義TableViewCell。此單元格包含一個標籤和一個文本字段,因此,例如,每個行會是這個樣子iPhone的TableView用的TextField - 下一個,上按鈕問題

enter image description here

我也有一個包含一個標籤和一個文本視圖的自定義表格單元格,看起來一樣上面的圖片,只是單元格較大,爲textview留出空間。

現在,這些文本框和文本視圖中有一個工具欄添加的,所以當文本字段成爲第一個響應,顯示與按鈕的工具欄,完成後,一個和下一個按鈕。

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithTextField:)]; 

     UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:self action:@selector(nextTableTextField:)]; 

     UIBarButtonItem *previousButton = [[UIBarButtonItem alloc]initWithTitle:@"Previous" style:UIBarButtonItemStyleDone target:self action:@selector(previousTableTextField:)]; 


     UIToolbar *numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; 
     numberToolbar.barStyle = UIBarStyleBlackTranslucent; 
     numberToolbar.items = [NSArray arrayWithObjects: 
           previousButton, 
           nextButton, 
           [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], 
           doneButton, 
           nil]; 

     [numberToolbar sizeToFit]; 

     field.inputAccessoryView = numberToolbar; 

我遇到的問題是,說我有10個錶行這樣的屏幕只能顯示5排滿,有點第6行。我應該添加我也添加了代碼到文本字段委託,以便當文本字段成爲第一響應者時,屏幕滾動以允許文本字段完全顯示。查看我的舊帖子here獲取代碼。

所以當屏幕上的負荷我按下第一個單元格,然後單擊Next,做工精細,在接下來的小區中移動到第二個文本字段。然後我再次點擊下一個按鈕,然後進入第3格,然後是第4格,然後是第5格。現在在這一點上出現問題。當我在第5個單元格中,然後單擊下一個時,第6個文本字段將成爲第一個響應者,但由於某種原因,屏幕不會滾動。現在更大的問題是,當我再次單擊下一個時,它不會移動到第7個文本字段,因爲第7個單元格不在內存中,因爲它在屏幕上不可見。因此,如果我接下來打了什麼,它什麼都不會做,文本字段6將保持爲第一響應者。所以我需要先向下滾動一下,所以單元格7在下一個按鈕可以工作之前是可見的,並且使下一個文本字段成爲第一響應者。

這是同一個問題打前面的按鈕時,如果擊中以前按鈕和以前的小區不在屏幕,然後直到細胞是在屏幕上可見它不會做任何事情。

這已造成我挺頭疼的,似乎無法找出解決的辦法。有沒有其他人有過類似的問題?有沒有很好的解決這個問題?

在此先感謝

編輯:

我還要補充,這使得數據保存問題,因爲說我有一個按鈕,點擊時通過每個表格單元的循環和每個字段保存的數據,只會循環顯示在屏幕上的表格單元格,而忽略其餘部分。

回答

1

它的工作對我來說,我使用的時候「做」,這是發射塊鍵盤上按下按鈕,將焦點移到下一個文本派出細胞:

FRTextFieldTableViewCell *textFieldCell = [tableView_ dequeueReusableCellWithIdentifier:[FRTextFieldTableViewCell reuseIdentifier]]; 
     if(textFieldCell == nil) { 
      textFieldCell = [[FRTextFieldTableViewCell alloc] initWithStyle: 
          UITableViewCellStyleDefault reuseIdentifier:[FRTextFieldTableViewCell reuseIdentifier]]; 
     } 

     [textFieldCell.textField initialize]; 
     textFieldCell.textField.secureTextEntry = (indexPath.row == 3); 

     __weak FRSignUpViewController *self_ = self; 
     __weak FRTextFieldTableViewCell *textFieldCell_ = textFieldCell; 
     if(indexPath.row == 1) { 
      textFieldCell.textField.placeholder = NSLocalizedString(@"name", @""); 
      textFieldCell.object = self.regUser.name; 
      textFieldCell.didChangedValueAction = ^(NSString *object) { 
       [self_ setName:object]; 
      }; 
      textFieldCell.didEndOnExitAction = ^{ 
       [self_ setName:textFieldCell_.object]; 
       FRTextFieldTableViewCell *nextCell = (FRTextFieldTableViewCell *)[self_.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]]; 
       [nextCell.textField becomeFirstResponder]; 
      }; 
     } 
     if(indexPath.row == 2) { 
      textFieldCell.textField.placeholder = NSLocalizedString(@"email", @""); 
      textFieldCell.didChangedValueAction = ^(NSString *object) { 
       [self_ setLoginString:object]; 
      }; 
      textFieldCell.didEndOnExitAction = ^{ 
       [self_ setLoginString:textFieldCell_.object]; 
       FRTextFieldTableViewCell *nextCell = (FRTextFieldTableViewCell *)[self_.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]]; 
       [nextCell.textField becomeFirstResponder]; 
      }; 
     } 
     if(indexPath.row == 3) { 
      textFieldCell.textField.placeholder = NSLocalizedString(@"password", @""); 
      textFieldCell.didChangedValueAction = ^(NSString *object) { 
       [self_ setPasswordString:object]; 
      }; 
      textFieldCell.didEndOnExitAction = ^{ 
       [self_ setPasswordString:textFieldCell_.object]; 
       [self_ signUp]; 
      }; 
     } 
     [textFieldCell reloadData]; 
     return textFieldCell; 

BlocksTypedefs:

/* type defenition for blocks, can be used by any app class */ 
typedef void (^CompletionBlock)(id, NSError *); 
typedef void (^SimpleBlock)(void); 
typedef void (^InfoBlock)(id); 
typedef void (^ConfirmationBlock)(BOOL); 

TableViewCell代碼:

.h文件中:

#import "FRTableViewCell.h" 
#import "BlocksTypedefs.h" 
#import "FRTextFieldWithPadding.h" 

@interface FRTextFieldTableViewCell : FRTableViewCell <UITextFieldDelegate> 

@property (nonatomic, copy) SimpleBlock didEndOnExitAction; 
@property (nonatomic, copy) SimpleBlock didEndEditingAction; 
@property (nonatomic, copy) InfoBlock didChangedValueAction; 
@property (nonatomic, strong) IBOutlet FRTextFieldWithPadding *textField; 

@end 

。m:

#import "FRTextFieldTableViewCell.h" 

@implementation FRTextFieldTableViewCell 

- (void)awakeFromNib { 
    [super awakeFromNib]; 
    self.backgroundColor = [UIColor clearColor]; 
} 

- (void)reloadData { 
    self.textField.text = self.object; 
} 

- (IBAction)textFieldValueDidChanged:(UITextField *)sender { 
    self.object = sender.text; 
    if (self.didChangedValueAction) { 
     self.didChangedValueAction(self.object); 
    } 
} 

- (IBAction)textFieldDidEndOnExit:(UITextField *)sender { 
    self.object = sender.text; 
    if (self.didEndOnExitAction) { 
     self.didEndOnExitAction(); 
    } 
} 

#pragma mark - UITextFieldDelegate 

- (void)textFieldDidEndEditing:(UITextField *)textField { 
    self.object = textField.text; 
    if (self.didEndEditingAction) { 
     self.didEndEditingAction(); 
    } 
} 

@end 
+0

你有沒有嘗試過這樣的代碼說10個單元格,它是否仍然去下一個文本字段即使單元沒有加載到內存中呢? – AdamM 2013-02-25 11:04:29

+0

是的,原因,它會通過調用線[self_.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:(indexPath.row + 1)inSection:0]]內部didEndOnExitHandler – iiFreeman 2013-02-25 12:26:10

+0

加載你能告訴我你是如何將塊代碼函數添加到您的表請給我一些細胞,這樣我就可以看到你是如何實現它的,例如你可以告訴我你用來設置didEndOnExitAction的代碼。 – AdamM 2013-02-25 13:52:54

0
CGRect frame = CGRectMake(0.0, self.view.bounds.size.height, self.view.bounds.size.width, 44.0); 
fieldAccessoryView = [[UIToolbar alloc] initWithFrame:frame]; 
fieldAccessoryView.barStyle = UIBarStyleBlackOpaque; 
fieldAccessoryView.tag = 200; 

[fieldAccessoryView setBarStyle:UIBarStyleBlack]; 

UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)]; 

UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Previous", @""), NSLocalizedString(@"Next", @""), nil]]; 
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; 
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 
[segmentedControl setMomentary:YES]; 
UIBarButtonItem *segmentButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl]; 

[fieldAccessoryView setItems:[NSArray arrayWithObjects:segmentButton, spaceButton, doneButton, nil] animated:NO]; 
[segmentButton release]; 
[spaceButton release]; 
[doneButton release]; 
[segmentedControl release]; 
+0

您是否看過我的問題?這不回答我的問題,這只是一種不同的方式來設置下一個先前和完成的按鈕,我已經知道如何設置 – AdamM 2013-02-25 10:10:08

相關問題