2013-02-22 26 views
0

我試圖讓用戶能夠使用箭頭鍵在表格視圖(每個單元格的兩個文本字段)內循環顯示所有文本字段。重新使用單元格內的文本字段的導航

目前我正在做的是爲每個文本字段設置一個標籤,並將每個文本字段添加到數組中。一旦用戶點擊文本字段開始編輯它,一些箭頭按鈕出現,當他們按下箭頭鍵時,我抓住當前選定文本字段的標籤,將其用作數組的位置,將文本字段拉出想要去,並設置新的文本字段作爲第一響應者。

然而,問題在於我的細胞被重新使用的事實。因此,如果用戶在屏幕外滾動單元格,當單元格出現時,表格第1行中的單元格可能具有標籤爲15和16的文本字段,並且這些文本字段數組的末尾會破壞我的箭頭鍵。而他們滾動得越多,文本字段越亂。

在維持可重複使用的單元格的同時,可以完成我想要做的事情嗎?或者這只是要求我不重複使用它們?

這裏是我的箭代碼...

- (void)arrowPressedHandler:(UIButton *)button 
{ 
UITextField *newTextFieldSelection; 

//tags are offset by 2 because I have use tag 1 for something else, and tag 0 cannot be used 
int realLocation = selectedTextField.tag - 2; 

//arrow code. for an up button i go back 2 slots in the array, right is + 1 in array 
//etc etc 
@try{ 
    switch (button.tag) { 
     case NumericKeyboardViewUpArrow: 
      newTextFieldSelection = [textFields objectAtIndex:realLocation - 2]; 
      [newTextFieldSelection becomeFirstResponder]; 
      break; 
     case NumericKeyboardViewLeftArrow: 
      newTextFieldSelection = [textFields objectAtIndex:realLocation - 1]; 
      [newTextFieldSelection becomeFirstResponder]; 
      break; 
     case NumericKeyboardViewRightArrow: 
      newTextFieldSelection = [textFields objectAtIndex:realLocation +1]; 
      [newTextFieldSelection becomeFirstResponder]; 
      break; 
     case NumericKeyboardViewDownArrow: 
      newTextFieldSelection = [textFields objectAtIndex:realLocation + 2]; 
      [newTextFieldSelection becomeFirstResponder]; 
      break; 
     default: 
      break; 
    } 
} 
@catch (NSException *e) 
{ 
    return; 
} 
} 

回答

0

我將採取略有不同的方法。

而不是給所有的textField的不同標籤,給他們一個標籤是每行內唯一,但行之間相同。即左邊的獲得標記1000,右邊的獲得1001.然後,使用單元格的indexPath來獲得適當的行。您的arrowPressedHandler:方法看起來像這樣(在僞代碼中):

- (void)arrowPressedHandler:(UIButton *)button 
{ 
    NSIndexPath *selectedIndexPath = indexPathOfSelectedTextfield; 
    UITextField *newTextFieldSelection; 
    NSIndexPath *newIndexPath; 
    NSUInteger newTag = 0; 

    @try{ 
     switch (button.tag) { 
      case NumericKeyboardViewUpArrow: 
       // Move back two textFields (which would be the one directly above this one) 
       newIndexPath = selectedIndexPath - 1; 
       newTag = selectedTextField.tag; 
       break; 
      case NumericKeyboardViewLeftArrow: 
       // Move back one textField. 
       if (selectedTextField.tag == 1000) { 
        // Selected text field is on left. Select right textfield in the row above 
        newIndexPath = selectedIndexPath - 1; 
        newTag = 1001; 
       } else { 
        // Selected text field is on right. Select the left textfield in the same row 
        newIndexPath = selectedIndexPath; 
        newTag = 1000; 
       } 
       break; 
      // etc. 
     } 
     [self.tableview scrollToRowAtIndexPath:newIndexPath]; 
     newTextFieldSelection = [[self.tableview cellForRowAtIndexPath:newIndexPath] viewWithTag:newTag]; 
     [newTextFieldSelection becomeFirstResponder]; 

    } 
    @catch (NSException *e) 
    { 
     return; 
    } 

} 
+0

我明白了。我用索引路徑折騰了一些想法,但這比我迄今爲止想到的要好。這應該工作。我會測試一下。一個問題,但是,你將如何獲得'indexPathOfSelectedTextField'值? – JMD 2013-02-22 15:46:19

+0

它會涉及更多一點(確保你不在第一行/最後一行等),但不應該太難。獲取所選文本字段所在行的indexPath可能有點棘手。看看這個答案,我已經發布了一個很好的解決方案(項目符號3):http://stackoverflow.com/a/14352372/937822 – lnafziger 2013-02-22 15:52:50

+0

lol,獲得索引路徑看起來有點hackey,但它沒有出現任何優雅的解決方案。謝謝您的幫助! – JMD 2013-02-22 15:58:27

相關問題