单击复选标记删除行项目

时间:2019-01-18 08:45:13

标签: swift

@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)
    }

    }

}

[我希望通过单击删除按钮删除我选择的图像,这些图像来自我以前的控制器1

2 个答案:

答案 0 :(得分:0)

@Rahul检查以下代码:

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()
    }
}

答案 1 :(得分:0)

解决此类问题的一种优雅方法是使用“委托”,我将为您提供一个示例,说明如何解决该问题。

首先是TableViewCell声明协议:

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

将以下方法添加到TableViewCell类中

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)
}

获取要用于删除电影的按钮的IBAction:

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

您的TableView委托和DataSource方法实现应如下所示:

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
    }
}

委托实施以从列表中删除电影:

extension ViewController : MovieTableViewCellDelegate {
    func movieSelection(_ movie: Movie,indexPath: IndexPath) {
        // Write your code here to delete movie
        listmoviesArray.remove(at: indexPath.row)
        moviesTableView.reloadData()
    }
}