2011-02-01 89 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