0
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var tableview: UITableView!
var headerLabel:String!
var ListmoviesArray:[UIImage]!
override func  viewDidLoad()   {
    super.viewDidLoad()
titleLabel.text = headerLabel
                }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return ListmoviesArray.count
                }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->  UITableViewCell {
let cell = self.tableview.dequeueReusableCell(withIdentifier: "ListTableViewCell", for: indexPath) as! ListTableViewCell
    cell.myImageView.image = ListmoviesArray[indexPath.row]
    cell.btnCheckMark.addTarget(self, action: #selector(checkMarkButtonClicked),  for: .touchUpInside)
    cell.selectionStyle = .none

return cell
                 }
@objc func checkMarkButtonClicked ( sender: UIButton)   {
  sender.isSelected = !sender.isSelected
    if sender.isSelected {
    sender.setImage(UIImage(named: "Checked"), for: .normal)
    } else {
        sender.setImage(UIImage(named: "UnChecked"), for: .normal)
    }

    }

}

[I want that my selected images will delete by clicking on delete button these images are coming from my previous controller 1

  • Hello @Rahul Mishra , Please check below link : https://stackoverflow.com/q/40859066/2058242 How to delete element from array? – Jay Mehta Jan 18 '19 at 08:57
  • Hello @Rahul Mishra, You have to add each element or index of element whenever do check on list item. On tap of delete button you should have to remove all the element from master array with comparison of deleted elements. – Bhavik Modi Jan 18 '19 at 09:14

2 Answers2

0

@Rahul check following code:

class Movie : NSObject {
    var imageName: String!
    var selected: Bool!
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableview: UITableView!
    var headerLabel:String!
    var ListmoviesArray:[Movie]!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // Add your data of type Movie here
        ListmoviesArray = [ ]
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return ListmoviesArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->  UITableViewCell {
        let cell = self.tableview.dequeueReusableCell(withIdentifier: "ListTableViewCell", for: indexPath) as! ListTableViewCell
        cell.imageView?.image = UIImage(named: ListmoviesArray[indexPath.row].imageName)
        cell.btnCheckMark.addTarget(self, action: #selector(checkMarkButtonClicked),  for: .touchUpInside)
        cell.btnCheckMark.tag = indexPath.row
        cell.btnCheckMark.setImage(UIImage(named: ListmoviesArray[indexPath.row].selected ? "Checked" : "UnChecked"), for: .normal)
        cell.selectionStyle = .none

        return cell
    }

    @objc func checkMarkButtonClicked ( sender: UIButton)   {
        sender.isSelected = !sender.isSelected
        ListmoviesArray[sender.tag].selected = sender.isSelected
        if sender.isSelected {
            sender.setImage(UIImage(named: "Checked"), for: .normal)
        } else {
            sender.setImage(UIImage(named: "UnChecked"), for: .normal)
        }
    }

    @IBAction func deleteButtonTapped(_ sender: UIButton) {
        for selectedItem in ListmoviesArray {
            if (selectedItem.selected == true) {
                ListmoviesArray.remove(at: sender.tag)
            }
        }
        tableview.reloadData()
    }
}
Bhavik Modi
  • 1,517
  • 14
  • 29
  • It will give that error Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value on this line if sender.isSelected { sender.setImage(UIImage(named: "Checked"), for: .normal) if (!selectedItemsArray.contains(sender.tag)) { selectedItemsArray.append(sender.tag) } – Rahul Mishra Jan 18 '19 at 10:09
  • please give me more clarification about the answer Thankyou – Rahul Mishra Jan 18 '19 at 11:19
  • @objc func checkMarkButtonClicked ( sender: UIButton) { sender.isSelected = !sender.isSelected if sender.isSelected { self.selectedItemsArray.append(sender.tag) sender.setImage(UIImage(named: "Checked"), for: .normal) } else { // let index = self.selectedItemsArray.index(of: sender.tag) sender.setImage(UIImage(named: "unChecked"), for: .normal) self.selectedItemsArray.remove(at: sender.tag) } } – Rahul Mishra Jan 18 '19 at 13:12
  • @IBAction func deleteButtonTapped(_ sender: Any) { for selectedItem in selectedItemsArray { ListmoviesArray.remove(at: selectedItem) } self.selectedItemsArray.removeAll() tableview.reloadData() } This code works when i checked and delete first time it deletes but in second time when i select and delete it crash why please help me on this – Rahul Mishra Jan 18 '19 at 13:13
0

The elegant way to solve this kind of problem is to use Delegation, I will give you an example of how you can solve the problem.

First of all in TableViewCell Declare protocol:

protocol  MovieTableViewCellDelegate : class {
    func movieSelection(_ movie : Movie,indexPath: IndexPath)
}

Add the following method into your TableViewCell Class

func setupCell(_ movie : Movie, indexPath: IndexPath) {
    self.indexPath = indexPath
    self.selectedMovie = movie
    movieLabel.text = movie.name
   // This is just to show you selected movie
  // let image : UIImage =  movie.isSelected ? #imageLiteral(resourceName: "checkBoxSelected") : #imageLiteral(resourceName: "checkBoxUnselected")
 //   checkBoxButton.setImage(image, for: .normal)
}

Get the IBAction of a button you want to use to delete a movie:

  @IBAction func checkBoxButtonAction(_ sender: Any) {
        delegate.movieSelection(selectedMovie, indexPath: indexPath)
    }

Your TableView Delegate and DataSource Method Implementation should look like this :

extension ViewController : UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listmoviesArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MovieTableViewCell", for: indexPath) as! MovieTableViewCell
        cell.delegate = self
        cell.setupCell(listmoviesArray[indexPath.row],indexPath: indexPath)
        return cell
    }
}

Delegate implementation to delete movie from list :

extension ViewController : MovieTableViewCellDelegate {
    func movieSelection(_ movie: Movie,indexPath: IndexPath) {
        // Write your code here to delete movie
        listmoviesArray.remove(at: indexPath.row)
        moviesTableView.reloadData()
    }
}
Jarvis The Avenger
  • 2,750
  • 1
  • 19
  • 37