@objc func printNum() {

print(”hello world”)

}

this function can be used with objective c code.

1. Adding a Share button

this is the viewdidload function

navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareFlag))

this outside the viewdidload function

@objc func shareFlag() {
        guard let image = ImageView.image?.jpegData(compressionQuality: 0.8) else {
            print("No image found")
            return
        }
        
//      this shares two things an image and a name the commented line below only shares an image
        let vc = UIActivityViewController(activityItems: [title ?? "Unknown country", image], applicationActivities: [])
//        let vc = UIActivityViewController(activityItems: [image], applicationActivities: [])
        vc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
        present(vc, animated: true)
    }

Handling rows in uitable view

  1. This is for the viewcontroller containing tableview ⇒ select prototype row and set identifier to “Picture
  2. Now for the other view controller select view controller and set storyboard id to “Detail

type this outside the viewdidload function

 		override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return pictures_hd.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
        cell.textLabel?.text = "\(indexPath.row+1).  " + pictures_hd[indexPath.row].replacingOccurrences(of: ".png", with: "")
        return cell
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let vc = storyboard?.instantiateViewController(withIdentifier:"Detail") as? DetailViewController {
            let sortedPictures = pictures_hd.sorted()
            vc.selectedImage = sortedPictures[indexPath.row]
            navigationController?.pushViewController(vc, animated: true)
        }
    }

To remove a cell

//  to remove a cell
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
      if editingStyle == .delete {
        print("Deleted")

        self.items.remove(at: indexPath.row)
        self.tableView.deleteRows(at: [indexPath], with: .automatic)
      }
    }