swift-tableView 代理方法实现点击按钮删除效果

前端之家收集整理的这篇文章主要介绍了swift-tableView 代理方法实现点击按钮删除效果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. import UIKit
  2.  
  3. protocol FirstTableViewCellDelegate: NSObjectProtocol {
  4. func deleteCell(cell: FirstTableViewCell,button: UIButton)
  5. }
  6.  
  7. class FirstTableViewCell: UITableViewCell {
  8. @IBOutlet weak var myLabel: UILabel!
  9. @IBOutlet weak var myButton: UIButton!
  10.  
  11. weak var delegate:FirstTableViewCellDelegate?
  12. override func awakeFromNib() {
  13. super.awakeFromNib()
  14. prepareUI()
  15. // Initialization code
  16. }
  17.  
  18. override func setSelected(selected: Bool,animated: Bool) {
  19. super.setSelected(selected,animated: animated)
  20.  
  21. // Configure the view for the selected state
  22. }
  23. func prepareUI() {
  24. myButton.setTitle("删除",forState: UIControlState.Normal)
  25. myButton.backgroundColor = UIColor.redColor()
  26. myButton.layer.cornerRadius = 15
  27. myButton.layer.masksToBounds = true
  28. myButton.setTitleColor(UIColor.whiteColor(),forState: UIControlState.Normal)
  29. myButton.addTarget(self,action: "BtnClick:",forControlEvents: UIControlEvents.TouchUpInside)
  30. }
  31. @objc private func BtnClick(button:UIButton){
  32. delegate?.deleteCell(self,button: button)
  33. }
  34. }

  1. import UIKit
  2.  
  3. class FirstViewController: UIViewController {
  4.  
  5. @IBOutlet weak var tableView: UITableView!
  6. let cellIdentified = "cell"
  7. var numbers = ["One","Two","Three","Foure","Five","Six","Seven","Eight","Nine","Ten"];
  8. override func viewDidLoad() {
  9. super.viewDidLoad()
  10.  
  11. // 准备UI
  12. prepareUI()
  13. // Do any additional setup after loading the view.
  14. }
  15.  
  16. private func prepareUI() {
  17. navigationItem.title = "首页"
  18. tableView.delegate = self
  19. tableView.dataSource = self
  20. // tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: cellIdentified)
  21. tableView.registerNib(UINib(nibName: "FirstTableViewCell",bundle: nil),forCellReuseIdentifier: cellIdentified)
  22. }
  23. override func didReceiveMemoryWarning() {
  24. super.didReceiveMemoryWarning()
  25. // Dispose of any resources that can be recreated.
  26. }
  27. func tableView(tableView: UITableView,commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) {
  28. if editingStyle == UITableViewCellEditingStyle.Delete {
  29. numbers.removeAtIndex(indexPath.row)
  30. tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation: UITableViewRowAnimation.Automatic)
  31. }
  32. }
  33.  
  34. /*
  35. // MARK: - Navigation
  36.  
  37. // In a storyboard-based application,you will often want to do a little preparation before navigation
  38. override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
  39. // Get the new view controller using segue.destinationViewController.
  40. // Pass the selected object to the new view controller.
  41. }
  42. */
  43.  
  44. }
  45.  
  46. extension FirstViewController:UITableViewDelegate,UITableViewDataSource,FirstTableViewCellDelegate {
  47. func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  48. // let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentified,forIndexPath: indexPath)
  49. let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentified,forIndexPath: indexPath) as! FirstTableViewCell
  50. // cell.textLabel?.text = numbers[indexPath.row]
  51. cell.myLabel.text = numbers[indexPath.row]
  52. cell.delegate = self
  53. cell.selectionStyle = UITableViewCellSelectionStyle.None
  54. return cell
  55. }
  56. func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
  57. return numbers.count
  58. }
  59. func deleteCell(cell: FirstTableViewCell,button: UIButton) {
  60. let idx:NSIndexPath = tableView.indexPathForCell(cell)!
  61. numbers.removeAtIndex(idx.row)
  62. tableView.deleteRowsAtIndexPaths([idx],withRowAnimation: UITableViewRowAnimation.Top)
  63. }
  64. }
  65.  
  66.  
  67.  

猜你在找的Swift相关文章