2013-02-24 51 views
0

我使用包含标签和TextField的TabelViewCell创建了一个笔尖,然后我只用两行在TableView中加载此单元格。我想捕获一个项目的名称和它的价格,所以我更新了cellForRowAtIndexPath方法中的每个标签,因为我可以访问indexPath.row值。从单元格中的TextField中获取数据

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleCellIdentifier = @"AddBudgetItemCell"; 

    SCAddBudgetItemCell *cell = (SCAddBudgetItemCell *)[tableView dequeueReusableCellWithIdentifier:simpleCellIdentifier]; 

    if(cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:simpleCellIdentifier owner:self options:nil]; 

     cell = [nib objectAtIndex:0]; 
    } 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.dataTextField.delegate = self; 

    switch (indexPath.row) 
    { 
     case 0: 
      cell.titleLabel.text = @"Item"; 
      break; 

     case 1: 
      cell.titleLabel.text = @"Price ¥"; 
      break; 

     default: 
      break; 
    } 

    return cell; 
} 

但是我不知道我怎么能检索的文本字段的数据,因为我没有获得任何一种变量“细胞”我创造,也不是IndexPath.row值。

回答

0

据我了解,你有两种选择。如果里面你SCAddBudgetItemCell你有一个包含文本字段的字符串,则可能是你可以做这样的事情cell.yourtextfield.text(其中yourtexfield是你SCAddBudgetItemCell内的UITextView从您的视图 - 控制访问它的属性。

另一种选择是,让视图控制器响应UITextFieldDelegate并在用户通过实施委托方法编辑时存储字符串

- (void)textFieldDidBeginEditing:(UITextField *)textField{ 
    //Whenever people start editing your textfield 
} 
-(BOOL)textFieldShouldReturn:(UITextField *)textField{ 
    // 
return YES; 
} 
-(void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    //This is obvious. Whenever they end 
} 
相关问题