如何获取具有不同细胞类别的细胞

时间:2016-07-28 11:51:38

标签: ios swift uitableview

我需要从textfield获取文本,即来自KgCustomCell和KgRepsCustomCell。当我运行buttonClicked方法时,我需要从字段中获取数据。

我试图添加实例变量,其中包含kg和reps,但是第一次单击按钮时,它是空的。第二次没关系。但是,我如何以最正确的方式加载数据呢?

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

1 个答案:

答案 0 :(得分:1)

如果你只想要那个textField的文本而不是像这样使用UITextField的委托方法,首先要为这个2 textField的值声明两个实例var

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

现在在cellForRowAtIndexPath

中使用textField设置委托
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的委托方法

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

现在只需在按钮操作方法中使用这两个字符串对象。