2016-07-28 64 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 {' –

相关问题