2011-02-01 91 views
0

我有一個UITableView,其中的單元格是在NIB文件中定製的,所以我可以有一個UILabel和UITextField。設置UITableViewCell子視圖標籤屬性

因此我的cellForRowAtIndexPath看起來是這樣的:

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    SectionCustomCell *cell = (SectionCustomCell *)[tableView  
    dequeueReusableCellWithIdentifier: @"Section"]; 

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SectionCustomCell"              
          owner:self            
          options:nil]; 

    cell = [nib objectAtIndex:0]; 

    // Set the label value 
    [[cell inputLabel] setText:@"something"]; 

    // Set the textfield tag property 
} 

因此,對於給定段/行,我要分配一些文本到的UILabel,定義爲inputLabel,並有一個的UITextField,叫inputTextField從用戶處獲得一些文字。

我的計劃是設置的UITextField的標籤屬性,以便我能確定我在委託textFieldDidEndEditing獲得的字段。

現在我的問題,如果我把這個代碼:

UITextField *textField = nil; 
for (UIView *oneView in cell.contentView.subviews) 
{ 
    if ([oneView isMemberOfClass:[UITextField class]]) 
     textField = (UITextField *)oneView; 
} 
textField.tag = [indexPath row]; 

標籤屬性設置正確。 (我知道這從NSLog聲明)。但是,如果我做了以下,它是而不是設置正確。它在IB中定義的總是1。

cell.inputTextField.tag = [indexPath row]; 

,但對我來說這應該工作。我在設置標籤文本時採用相同的原則。有人可以幫助我理解爲什麼它不起作用嗎?

我是新來的iOS所以要溫柔:-)

感謝

邁克

回答

0

確保您已連接文本框和IBOutlet中的IB。 如果不起作用嘗試把這個代碼:

NSLog(@"%i", cell.inputTextField == nil); 

如果打印1到控制檯,則意味着將inputTextField是零所以某處筆尖文件之間,您的自定義類和實現代碼如下數據源的連接迷路。但正如我第一次說這是最有可能的文本字段在IB中沒有正確連接。

+0

謝謝 - 一如既往,我沒有正確連接插座。衛生署! – hydev 2011-02-02 13:29:18