Advertisement
Guest User

Untitled

a guest
Dec 17th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.37 KB | None | 0 0
  1. //  Custom cell
  2.  
  3. import UIKit
  4.  
  5. protocol CellDelegate: class {
  6.     func youtuberTableViewCell(_ youtuberTableViewCell: PictureCell, subscribeButtonTappedFor youtuber: String)
  7. }
  8.  
  9. class PictureCell: UITableViewCell {
  10.     @IBOutlet weak var youtuberLabel: UILabel!
  11.     @IBOutlet weak var subscribeButton: UIButton!
  12.    
  13.     var youtuber : String?
  14.    
  15.     weak var delegate : CellDelegate?
  16.    
  17.     override func awakeFromNib() {
  18.         super.awakeFromNib()            
  19.         self.subscribeButton.frame = CGRect(origin: CGPoint(x: 200,y :60), size: CGSize(width: 100, height: 24))
  20.         let cellHeight: CGFloat = 44.0
  21.         self.subscribeButton.center = CGPoint(x: bounds.width / (4/3), y: cellHeight / 2.0)
  22.         self.subscribeButton.addTarget(self, action: #selector(subscribeButtonTapped(_:)), for: .touchUpInside)
  23.     }
  24.    
  25.     override func setSelected(_ selected: Bool, animated: Bool) {
  26.         super.setSelected(selected, animated: animated)
  27.  
  28.         // Configure the view for the selected state
  29.     }
  30.    
  31.     @IBAction func subscribeButtonTapped(_ sender: UIButton){
  32.         if let youtuber = youtuber,
  33.            let _ = delegate {
  34.             self.delegate?.youtuberTableViewCell(self, subscribeButtonTappedFor: youtuber)
  35.         }
  36.       }
  37. }
  38.  
  39. // ViewController
  40.  
  41. import UIKit
  42.  
  43. class ViewController: UIViewController {
  44.     let titleName: String = "Intro project"
  45.     var tableView: UITableView = UITableView()
  46.    
  47.     let youtubers = ["Brian Voong", "Seth Everman", "Dave Lee", "Cybershell", "Bill Wurtz"]
  48.    
  49.     struct Cells {
  50.         static let photoCell: String = "PictureCell"
  51.     }
  52.  
  53.     override func viewDidLoad() {
  54.         super.viewDidLoad()
  55.         title = titleName
  56.         configureTableView()
  57.     }
  58.    
  59.     func configureTableView() {
  60.         view.addSubview(tableView)
  61.         setTableViewDelegates()
  62.         tableView.rowHeight = 100
  63.         tableView.register(PictureCell.self, forCellReuseIdentifier: Cells.photoCell)
  64.         tableView.pin(to: view)
  65.     }
  66.    
  67.     func setTableViewDelegates() {
  68.         tableView.delegate = self
  69.         tableView.dataSource = self
  70.     }
  71. }
  72.  
  73. extension ViewController: UITableViewDelegate, UITableViewDataSource {
  74.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  75.         let cell = tableView.dequeueReusableCell(withIdentifier: Cells.photoCell) as! PictureCell
  76.        
  77.         // =========================================================//
  78.        
  79.         cell.youtuberLabel?.text = youtubers[indexPath.row]
  80.         cell.youtuber = youtubers[indexPath.row]
  81.         cell.delegate = self
  82.        
  83.         // =========================================================//
  84.        
  85.         //cell.set(picture: picture)
  86.         return cell
  87.     }
  88.    
  89.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  90.         return 5
  91.     }
  92. }
  93.  
  94. extension ViewController: CellDelegate {
  95.     func youtuberTableViewCell(_ youtuberTableViewCell: PictureCell, subscribeButtonTappedFor youtuber: String) {
  96.         let alert = UIAlertController(title: "Subscribed!", message: "Subscribed to \(youtuber)", preferredStyle: .alert)
  97.         let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
  98.         alert.addAction(okAction)
  99.            
  100.         self.present(alert, animated: true, completion: nil)
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement