How is the data saved with TableViewCell (UpdateProfile Screen)


#1

Created function through the delegate? Save data with TextField. Tell me so, can you? how can this be done in another way? Displays in the window nil. Tell me how with the class TableViewCell call in the usual ViewControllere The values of textField. I will be very grateful. It is necessary that when the data in the cells are changed, there will be a save.I have a Parse server.

import UIKit
import Parse

class UserProfile: UIViewController,UITableViewDelegate,UITableViewDataSource {

var delegate: saveTextField?

@IBOutlet weak var buttonSaveUs: UIButton!

@IBOutlet weak var userFoto: UIImageView!

@IBOutlet weak var tableView: UITableView!


@objc var refresh : UIRefreshControl!
let myUser: PFUser!
let ldUser = [PFUser]()


let myView = UIView()


override func viewDidLoad() {
    super.viewDidLoad()
  
    buttonSaveUs.addTarget(self, action: #selector(dataSaveUser), for: .touchUpInside)
    refresh = UIRefreshControl()
    refresh.attributedTitle = NSAttributedString(string: "Обновление")
    
    refresh.tintColor = UIColor.yellow
    refresh.addTarget(self, action:#selector(obnova), for: UIControlEvents.valueChanged)
    
    tableView.addSubview(refresh)
  
    tableView.tableFooterView = UIView(frame: .zero)
    tableView.estimatedRowHeight = 100
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.separatorStyle = .none
    tableView.delegate = self
    tableView.dataSource = self
    tableView.reloadData()
    loadObjectUser()
    // Do any additional setup after loading the view.
  
    }

func loadObjectUser() {

PFUser.current()!.fetchInBackground(block: { (currentUser, error) -> Void in

    if let user = currentUser as? PFUser {
        
         user.fetchFromLocalDatastoreInBackground()
        
    }
})

}

func segueUser() {
    self.performSegue(withIdentifier: "userprofile", sender: self)
    
}


func showActivityIndicatory(uiView: UIView) {
    let actInd: UIActivityIndicatorView = UIActivityIndicatorView()
    actInd.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
    actInd.center = uiView.center
    actInd.hidesWhenStopped = true
    actInd.activityIndicatorViewStyle =
        UIActivityIndicatorViewStyle.whiteLarge
    uiView.addSubview(actInd)
    actInd.startAnimating()
    
}


@IBAction func dataSaveUser(_ sender: UIButton) {
    
   
    func saveUserData(textfield: UserEditTableViewCell, textFieldTwo: UserEditTableViewCellTwo, textFieldThree: UserEditTableViewCellThree, textFieldFour: UserEditTableViewCellFour, PFUser: PFUser) {
        let myUser = PFUser
        let username = textfield.userTextField.text
        let country = textFieldTwo.countryTextfield.text
        let email = textFieldThree.emailTextField.text
        let password = textFieldFour.passwordTextField.text
        myUser.username = username
        myUser["country"] = country
        myUser.email = email
        myUser.password = password
        
        let imageProfileData = UIImageJPEGRepresentation(userFoto.image!, 0.5)
        let profileObject = PFFile(data: imageProfileData!)
        myUser.setObject(profileObject!, forKey: "profile_picture")
        
        myUser.saveInBackground(block: { (success, error) in
            if success {
                print("Saved in server")
            } else {
                print(error!)
            }
        })
    }
    self.dismiss(animated: true, completion: nil)

}

@IBAction func nowPhoto(_ sender: Any) {
    let myPickerImageController = UIImagePickerController()
    myPickerImageController.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
    myPickerImageController.sourceType = UIImagePickerControllerSourceType.photoLibrary
    
    
    self.present(myPickerImageController, animated: true, completion: nil)
    
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    userFoto.image = info [UIImagePickerControllerOriginalImage] as?UIImage
    self.dismiss(animated: true, completion: nil)
}

@IBAction func userSegue(_ sender: UIBarButtonItem) {

    self.performSegue(withIdentifier: "pushToUser", sender: self)
    
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 4
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   
    
   if  indexPath.row == 0  {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cellOne") as! UserEditTableViewCell
   cell.userTextField.text = PFUser.current()?.username
       return cell
   } else if indexPath.row == 1 {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cellTwo") as! UserEditTableViewCellTwo
    cell.countryTextfield.text = PFUser.current()?["country"] as? String
    return cell
   } else if indexPath.row == 2 {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellThree") as! UserEditTableViewCellThree
    cell.emailTextField.text = PFUser.current()?.email
    return cell
   } else  {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellFour") as! UserEditTableViewCellFour
    cell.passwordTextField.text = PFUser.current()?.password
    
    return cell
    }
   
    
}



func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

@objc func obnova() {

    tableView.reloadData()
    refresh.endRefreshing()
    
    
}

}

import UIKit
import Parse
protocol saveTextField: class {
func saveUserData(textfield: String,textFieldTwo:String,textFieldThree:String,textFieldFour:String, PFUser: PFUser)

}

class UserEditTableViewCell: UITableViewCell {

var delegate: saveTextField?
@IBOutlet weak var userTextField: UITextField!

{
didSet {
userTextField.layer.cornerRadius = 20
userTextField.textColor = .white
userTextField.returnKeyType = .next
}
}

}

class UserEditTableViewCellTwo : UITableViewCell {
var delegate: saveTextField?
@IBOutlet weak var countryTextfield: UITextField!{
didSet {
countryTextfield.layer.cornerRadius = 20
countryTextfield.textColor = .white
countryTextfield.returnKeyType = .next

}

}
}

class UserEditTableViewCellThree : UITableViewCell {
var delegate: saveTextField?
@IBOutlet weak var emailTextField: UITextField!
{
didSet {
emailTextField.layer.cornerRadius = 20
emailTextField.textColor = .white
emailTextField.returnKeyType = .next

}

}
}

class UserEditTableViewCellFour : UITableViewCell {
var delegate: saveTextField?
@IBOutlet weak var passwordTextField: UITextField!{
didSet {
passwordTextField.layer.cornerRadius = 20
passwordTextField.textColor = .white
passwordTextField.returnKeyType = .next

}

}

}