Jakob Jakob - 4 months ago 9
iOS Question

How to get cells with different cell classes

I need to get the text from the

textfield
i.e from KgCustomCell and KgRepsCustomCell. I need to fetch the data from the field, when i run the buttonClicked method.

I have tried to add to instance variables, which contains kg and reps, but the first time i click the button, it's empty. The second time it's okay. But how can i load the data in the most correct way?

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()
})
}
}
}

Answer

If you just want the text of that textField than you can use delegate method of UITextField like this, first declare two instance var for that 2 textField's value

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

Now set delegate with textField in 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
}

Now add delegate method of UITextField

func textFieldDidEndEditing(textField: UITextField) {
     if(textField.tag == 0) {
         self.strKG = textField.text
     }
     else {
         self.strReps = textField.text
     }
}

Now just use this two string object in your button action method.

Comments