2012-09-07 52 views
13

我有一個自定義單元格中的兩個文本框如何獲得TableView單元格的文本字段委託方法的indexpath值我想從用戶獲取輸入值並將其保存到relavent對象。用戶可以通過點擊按鈕,在細胞(添加更多)提前加入更多的細胞..如何在uitextfield中獲取單元格的indexpath委託方法?

enter image description here

謝謝...

+0

[將參數傳遞給選擇器]的可能重複(http://stackoverflow.com/questions/12297511/pass-an-argument-to-selector) – 2012-09-07 13:30:00

回答

17

更新到iOS7!

隨着iOS7新功能,現在代碼應該是:

UITableViewCell *textFieldRowCell; 

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { 
    // Load resources for iOS 6.1 or earlier 
    textFieldRowCell = (UITableViewCell *) textField.superview.superview; 

} else { 
    // Load resources for iOS 7 or later 
    textFieldRowCell = (UITableViewCell *) textField.superview.superview.superview; 
    // TextField -> UITableVieCellContentView -> (in iOS 7!)ScrollView -> Whoola! 
} 

NSIndexPath *indexPath = [self.tableView indexPathForCell:textFieldRowCell]; 
1

集小區indexpath值UITextFieldtag屬性,你可以訪問indexpath代理方法如textfield.tag

+0

但我有兩個文本字段,如果它的一個我可以使用你說 – Deepak

+0

這兩個文本字段都將在標記中具有唯一的索引路徑。然後你可以檢查佔位符來確定哪個是url,哪個是名字textfield.like這個NSLog(@「%@」,self.textField.placeholder);如果([self.textField.placeholder isEqualToString:@「Enter name」]) – prashant

+0

我認爲你想讓我知道,如果你仍然掛着 – prashant

2

要獲取indexPath,請嘗試以下代碼。

UIView *contentView = (UIVIew *)[textfield superview]; 
    UITableViewCell *cell = (UITableViewCell *)[contentView superview]; 
    if(IS_IOS7_OR_GREATER) { 
     cell = (UITableViewCell *)[[contentView superview] superview]; 
    } 
    NSIndexPath *indexPath = [self.tableview indexPathForCell:cell]; 

Tats it it you done。

簡單,

NSIndexPath *indexPath = [self.tableview indexPathForCell:(UITableViewCell *)[(UIVIew *)[textfield superview] superview]]; 

if(IS_IOS7_OR_GREATER) { 
    cell = (UITableViewCell *)[[[textfield superview] superview] superview]; 
} 

檢查更新的答案。

+0

感謝它的工作:) – Deepak

+0

我對這種情況有一個疑問 – Siva

+0

是的,什麼你的疑問是?你能把它作爲一個問題發佈嗎? –

12

一個更動態的解決方案(沒有硬編碼上海華盈水平和不同的IOS版本相同的代碼)。

此外,如果單元格不可見,則indexPathForCell:將不起作用,因此我使用indexPathForRowAtPoint:作爲解決方法。

//find the UITableViewcell superview 
UIView *cell = textField; 
while (cell && ![cell isKindOfClass:[UITableViewCell class]]) 
    cell = cell.superview; 

//use the UITableViewcell superview to get the NSIndexPath 
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:cell.center]; 
1

您可以設置文本框的標籤的cellForRowAtIndexPath:這樣它存儲兩種細胞和文本字段

例如的信息:如果它是細胞在第4行,第1和第2個文本框的標籤可以分別是41和42。同樣,第5行的文本字段標籤應爲51和52,依此類推......

然後在textfield委託方法中,您可以獲取textfield.tag來標識活動文本字段。

+0

最簡單的方法總是最好的! – Md1079

10

這就是我一直在做的事情,並且一直有更好的運氣。我抓住了textField框架的原點。將其轉換爲一個點。然後將該點轉換爲索引路徑。

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    CGPoint origin = textField.frame.origin; 
    CGPoint point = [textField.superview convertPoint:origin toView:self.tableView]; 

    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point]; 

    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 

} 
+0

Awsome解決方案.. – svmrajesh

+1

這應該是正確的答案。 – wtoh

0

這可以在任何實例中的Objective-C運行來完成(不必是的UITextField),與任何相關聯的對象(不必須是NSIndexPath)。

對於這個問題,我們可以創建一個類別UIView+RepresentingIndexPath

我們的界面允許我們設置和檢索的NSIndexPath:

@interface UIView (RepresentingIndexPath) 
- (void)representIndexPath:(NSIndexPath *)indexPath; 
- (NSIndexPath *)representedIndexPath; 
@end 

我們實現使用Objective-C的相關對象來設置和檢索一個視圖索引路徑:

#import "UIView+RepresentingIndexPath.h" 
#import <objc/runtime.h> 

static char IndexPathKey; 

@implementation UIView (RepresentingIndexPath) 

- (void)representIndexPath:(NSIndexPath *)indexPath 
{ 
    objc_setAssociatedObject(self, &IndexPathKey, indexPath, OBJC_ASSOCIATION_COPY_NONATOMIC); 
} 

- (NSIndexPath *)representedIndexPath 
{ 
    return objc_getAssociatedObject(self, &IndexPathKey); 
} 

@end 

在行動:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    TextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCell" forIndexPath:indexPath]; 
    [cell.textField addTarget:self action:@selector(textFieldTextChanged:) forControlEvents:UIControlEventEditingChanged]; 
    [cell.textField representIndexPath:indexPath]; 
    return cell; 
} 

- (void)textFieldTextChanged:(UITextField *)sender 
{ 
    NSIndexPath *indexPath = [sender representedIndexPath]; 
    NSLog(@"%@", indexPath); 
} 

最後一個注意!如果你可以在不這樣做的情況下實現你想要做的事情,那麼在運行時混淆就應該避免。只是想我會添加另一種解決方案!

4

試試這個方法來獲取文本框從tableview控制器

#pragma mark - Get textfield indexpath 
- (NSIndexPath *)TextFieldIndexpath:(UITextField *)textField 
{ 
    CGPoint point = [textField.superview convertPoint:textField.frame.origin toView:self.TblView]; 
    NSIndexPath * indexPath = [self.TblView indexPathForRowAtPoint:point]; 
    NSLog(@"Indexpath = %@", indexPath); 
    return indexPath; 
} 
0

動態任何地方,我覺得這個答案搜索我怎樣才能找到一個細胞具有的UITextField內索引路徑。

所以,感謝上面的答案,我把我的代碼放在這裏,希望可能是有用的。

- (void)searchSelectedIndexPath:(UIView*)view { 
    // This allow to find selected index path for a table view cell with a text field inside. 
    for (UIView* subview in view.subviews) { 
     if ([view isKindOfClass:[UITextField class]]) { 
      if ([view isFirstResponder]) { 
       UIView *cell = view; 
       while (cell && ![cell isKindOfClass:[UITableViewCell class]]) { 
        cell = cell.superview; 
       } 
       self.selectedIndexPath = [self.tableView indexPathForRowAtPoint:cell.center]; 
       return; 
      } 
     } 
     [self searchSelectedIndexPath:subview]; 
    } 
} 

這樣,當鍵盤通知將加薪:

- (void)keyboardDidShow:(NSNotification*)notification { 
    [self searchSelectedIndexPath:self.tableView]; 
} 
0

如果有人像我一樣需要@Kerkness的回答(這個人真的爲我工作)在SWIFT:

func textFieldDidBeginEditing(_ textField: UITextField) { 
    let origin: CGPoint = textField.frame.origin 
    let point: CGPoint? = textField.superview?.convert(origin, to: tableView) 
    let indexPath: IndexPath? = tableView.indexPathForRow(at: point ?? CGPoint.zero) 
    tableView.scrollToRow(at: indexPath!, at: .middle, animated: true) 
} 

它應該足夠簡單:您明白了,然後您就可以獲得indexPath並根據需要做任何事情!

相關問題