2016-07-28 61 views
1

我需要從textfield(即從KgCustomCell和KgRepsCustomCell)獲取文本。我需要從字段中獲取數據,當我運行buttonClicked方法。如何獲取具有不同單元類別的單元格

我試圖添加到實例變量,其中包含kg和代表,但第一次單擊按鈕,它是空的。第二次沒關係。但我怎樣才能以最正確的方式加載數據?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let index = indexPath.row 

    if indexPath.row == 0 && indexPath.section == 0 { 
     let exerciseName = tableView.dequeueReusableCellWithIdentifier("Exercise Name", forIndexPath: indexPath) as! LoggedExerciseNameCell 

     exerciseName.lblExerciseName.text = self.exercise?.name 

     return exerciseName 
    } 

    if index == 0 && indexPath.section == 1 { 
     let txtFieldKg = tableView.dequeueReusableCellWithIdentifier("Text KG", forIndexPath: indexPath) as! KgCustomCell 

     return txtFieldKg 
    } 

    if index == 1 && indexPath.section == 1 { 
     let txtFieldReps = tableView.dequeueReusableCellWithIdentifier("Text Reps", forIndexPath: indexPath) as! KgRepsCustomCell 

     //kg = txtFieldReps.textReps.text 

     return txtFieldReps 
    } 

    if index == 2 && indexPath.section == 1 { 
     let btnLog = tableView.dequeueReusableCellWithIdentifier("Button Log", forIndexPath: indexPath) as! ButtonLogWorkoutCustomCell 

     btnLog.btnLogExercise.addTarget(self, action: #selector(AddLogViewController.buttonClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside) 

     // kg = txtFieldReps.textReps.text 

     return btnLog 
    } 

    if indexPath.section == 2 { 
     let loggedExerciseInformation = tableView.dequeueReusableCellWithIdentifier("Logged Exercise", forIndexPath: indexPath) as! LoggedExerciseCustomCell 

     return loggedExerciseInformation 
    } 

    let noCell = tableView.dequeueReusableCellWithIdentifier("Button Log", forIndexPath: indexPath) 

    return noCell 
} 

func buttonClicked(sender:UIButton) { 
    let button = sender as UIButton 

    if let superview = button.superview { 
     if (superview.superview as? ButtonLogWorkoutCustomCell) != nil { 
      try! LogManagerDAO.sharedInstance.realm.write({ 
       exercise?.loggedKg = 4//Int(txtKG.text!)! 
       exercise?.loggedReps = 4//Int(txtReps.text!)! 
       log!.addExerciseToLog(exercise!) 

       loadLoggedExercise() 

       tableView.reloadData() 
      }) 
     } 
    } 
} 
+0

@dashandrest不要給標題添加標籤。已經有了*標籤*部分,並且這裏已經有「ios」了。參見[meta](http://meta.stackoverflow.com/questions/303606/is-it-ok-to-systematically-edit-the-questions-titles-like-this)。 – Moritz

+0

@EricD:好吧,當然肯定:) – D4ttatraya

回答

1

如果你只是想要一個文本框比你可以使用的UITextField像這樣的委託方法的內容,首先聲明兩個實例VAR爲2文本框的值

var strKG: String = "" 
var strReps: String = "" 

現在設置委託與文本字段中cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let index = indexPath.row 

    if indexPath.row == 0 && indexPath.section == 0 { 
     let exerciseName = tableView.dequeueReusableCellWithIdentifier("Exercise Name", forIndexPath: indexPath) as! LoggedExerciseNameCell 

     exerciseName.lblExerciseName.text = self.exercise?.name 

     return exerciseName 
    } 

    if index == 0 && indexPath.section == 1 { 
     let txtFieldKg = tableView.dequeueReusableCellWithIdentifier("Text KG", forIndexPath: indexPath) as! KgCustomCell 
     txtFieldReps.textField.tag = index 
     txtFieldKg.textField.delegate = self 
     return txtFieldKg 
    } 

    if index == 1 && indexPath.section == 1 { 
     let txtFieldReps = tableView.dequeueReusableCellWithIdentifier("Text Reps", forIndexPath: indexPath) as! KgRepsCustomCell 
     txtFieldReps.textField.tag = index 
     txtFieldReps.textField.delegate = self 
     return txtFieldReps 
    } 

    if index == 2 && indexPath.section == 1 { 
     let btnLog = tableView.dequeueReusableCellWithIdentifier("Button Log", forIndexPath: indexPath) as! ButtonLogWorkoutCustomCell 

     btnLog.btnLogExercise.addTarget(self, action: #selector(AddLogViewController.buttonClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside) 

     // kg = txtFieldReps.textReps.text 

     return btnLog 
    } 

    if indexPath.section == 2 { 
     let loggedExerciseInformation = tableView.dequeueReusableCellWithIdentifier("Logged Exercise", forIndexPath: indexPath) as! LoggedExerciseCustomCell 

     return loggedExerciseInformation 
    } 

    let noCell = tableView.dequeueReusableCellWithIdentifier("Button Log", forIndexPath: indexPath) 

    return noCell 
} 

現在增加的UITextField

的委託方法

現在只需在您的按鈕操作方法中使用這兩個字符串對象。

+0

你試過我的解決方案嗎? –

+0

Hi @Nirav。 當我嘗試你的解決方案時,我得到以下錯誤:不能分配'AddLogViewController'類型的值鍵入'UITextFieldDelegate?'。 它出現在這一行的錯誤:txtFieldReps.textField.delegate = self – Grumme

+0

你需要爲你的'AddLogViewController'實現'UITextFieldDelegate'這樣的'class AddLogViewController:UIViewController,UITextField {' –

相關問題