2013-02-24 32 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 
} 
相關問題