skip to Main Content

I have a tableView and viewController. I need to add a photo with image picker(i already done that) and then I need to save it with a description as a new post in my tableView. What is the best way to do that?

I have already found an information how to add text as a new cell in tableView, but that is not i want to have.

Here is my code

@IBAction func photoButton(_ sender: UIButton){  
    let vc = UIImagePickerController()
    vc.sourceType = .photoLibrary //PHPpicker
    vc.delegate = self
    vc.allowsEditing = true
    present(vc,animated: true)
 }

    
@IBAction func saveButtonPressed(_ sender: UIButton){

}
extension NewPostScreenController : UIImagePickerControllerDelegate , UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        UIImagePickerController.InfoKey(rawValue: "")
        
        if let image = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerEditedImage")] as? UIImage {
            ChangeImage.image = image
        }
        picker.dismiss(animated: true, completion: nil)
        
    }
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
}

What should i add to saveButtonPressed?

Here is screenshot of my storyboard

2

Answers


  1. My advice is make sure you break down the problem into parts. Your solution is probably going to require at least these a few things:

    1. You’ll need a delegate/protocol method to signal back to the tableView that the UIPickerView got an image. You then need to pass that image back to the tableView somehow.
    2. Inside the TableViewDataSource assigned to the tableView, you’ll need to receive the signal and process it:
    3. You’ll probably want a subclass of UITableViewCell that includes a UIImageView subview or a UIImage property. There’s a lot of ways to skin this but probably the simplest is working with a prototype cell in the storyboard and doing autolayout + connecting outlets. Other possibilities are creating a CocoaTouch class with accompanying xib and invoking that in your cellForRowAt function.
    4. You need to set the UIImage in said custom UITableViewCell in cellForRowAt for the TableViewDelegate
    5. When you receive the signal that a new image was received, you’ll probably want to call tableView.reloadTable in the delegate function so it’ll reload when user is done.
    Login or Signup to reply.
  2. Remember that you need implement a UITableViewCell in your tableView to show the posts.

    Now, to save post and that these are not deleted when closing the application, i recommend using UserDefaults or Core Data to save the post only in your phone, or use the Firebase to save the post in a data cloud.
    With firebase you can save your data without make a server code.

    Firebase Page
    https://firebase.google.com

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search