Muhammad Ahmed Baig
Muhammad Ahmed Baig

Reputation: 726

update a cell in tableview also update another cell

I am little bit confused in a scenario, I think its my logical mistake please help me to resolve it, i am really confused. Scenario is that:

  1. There is Questions in Form.

  2. Every Question Have There Answer, 1st is YES, 2nd is NO, And 3rd is Not Applicable.

  3. By Default YES is selected.
  4. If User Click on Yes And Not Applicable there is not change in UI occurs, But if User Click on NO there is a textfield shows In which user should describe Reason of NO.

Now these all thing Happens But a problem is occur when user clicks on NO the textfield shows but when user scroll down another cell shows textfield, and NO is also selected on that cell, which is not selected by user. I think its issue with index or my logic please help, i am using delegate pattern for button events.

please see the attachments below

When User Select NO in Question No. 0

And Scroll Down than Question No. 6 is also shown as NO

Hope Any One Help me, Thanks in advance

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "FormCell", for: indexPath) as! FormCell

    cell.questionLbl.text = "Question No \(indexPath.row): What is Your Name"
    cell.index = indexPath as NSIndexPath?
    cell.delegate = self
    cell.tag = indexPath.row

    return cell
}

func yesBtnPressedAt(indexPath: NSIndexPath) {
    print("YES")

    let cell = self.tableView.cellForRow(at: indexPath as IndexPath) as! FormCell
    if(cell.tag == indexPath.row){
        self.tableView.beginUpdates()

        cell.yesBtn.backgroundColor = ColorUtilities.selectedAnswerColor
        cell.noBtn.backgroundColor = ColorUtilities.unSelectedAnswerColor
        cell.notApplicableBtn.backgroundColor = ColorUtilities.unSelectedAnswerColor

        cell.resonTextFiled.isHidden = true
        cell.goToTeamBtn.isHidden = true
        cell.heightForMaineView.constant = 89.0
        cell.heightForTextField.constant = 0.0

        self.tableView.endUpdates()
    }
}

func noBtnPressedAt(indexPath: NSIndexPath) {
    print("NO")

    let cell = self.tableView.cellForRow(at: indexPath as IndexPath) as! FormCell

    if(cell.tag == indexPath.row){

        self.tableView.beginUpdates()

        cell.yesBtn.backgroundColor = ColorUtilities.unSelectedAnswerColor
        cell.noBtn.backgroundColor = ColorUtilities.selectedAnswerColor
        cell.notApplicableBtn.backgroundColor = ColorUtilities.unSelectedAnswerColor

        cell.resonTextFiled.isHidden = false
        cell.goToTeamBtn.isHidden = false

        cell.heightForMaineView.constant = 129.0
        cell.heightForTextField.constant = 30.0

        self.tableView.endUpdates()

    }
}

func notApplicableBtnPressedAt(indexPath: NSIndexPath) {
    print("Not Applicable")

    let cell = self.tableView.cellForRow(at: indexPath as IndexPath) as! FormCell

    if(cell.tag == indexPath.row){

        self.tableView.beginUpdates()

        cell.yesBtn.backgroundColor = ColorUtilities.unSelectedAnswerColor
        cell.noBtn.backgroundColor = ColorUtilities.unSelectedAnswerColor
        cell.notApplicableBtn.backgroundColor = ColorUtilities.selectedAnswerColor

        cell.resonTextFiled.isHidden = true
        cell.goToTeamBtn.isHidden = true

        cell.heightForMaineView.constant = 89.0
        cell.heightForTextField.constant = 0.0

        self.tableView.endUpdates()

    } }

Upvotes: 0

Views: 740

Answers (3)

toiavalle
toiavalle

Reputation: 434

If Jupiters answer is what you looking for... Here is a Swift version

self.tableView.beginUpdates();
self.tableView reloadRows(atIndexPath: "your indexPath", withRowAnimation: .none);
self.tableView.endUpdates();

Upvotes: 0

jupiter1992
jupiter1992

Reputation: 81

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@["your indexPath"] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

nice work

Upvotes: 1

Daivest
Daivest

Reputation: 66

it's all about reusable cells mechanics in tableview, for more information need some code contained in

func tableView(_ tableView: UITableView, 
  cellForRowAt indexPath: IndexPath) -> UITableViewCell

Upvotes: 0

Related Questions