Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Custom cell
- import UIKit
- protocol CellDelegate: class {
- func youtuberTableViewCell(_ youtuberTableViewCell: PictureCell, subscribeButtonTappedFor youtuber: String)
- }
- class PictureCell: UITableViewCell {
- @IBOutlet weak var youtuberLabel: UILabel!
- @IBOutlet weak var subscribeButton: UIButton!
- var youtuber : String?
- weak var delegate : CellDelegate?
- override func awakeFromNib() {
- super.awakeFromNib()
- self.subscribeButton.frame = CGRect(origin: CGPoint(x: 200,y :60), size: CGSize(width: 100, height: 24))
- let cellHeight: CGFloat = 44.0
- self.subscribeButton.center = CGPoint(x: bounds.width / (4/3), y: cellHeight / 2.0)
- self.subscribeButton.addTarget(self, action: #selector(subscribeButtonTapped(_:)), for: .touchUpInside)
- }
- override func setSelected(_ selected: Bool, animated: Bool) {
- super.setSelected(selected, animated: animated)
- // Configure the view for the selected state
- }
- @IBAction func subscribeButtonTapped(_ sender: UIButton){
- if let youtuber = youtuber,
- let _ = delegate {
- self.delegate?.youtuberTableViewCell(self, subscribeButtonTappedFor: youtuber)
- }
- }
- }
- // ViewController
- import UIKit
- class ViewController: UIViewController {
- let titleName: String = "Intro project"
- var tableView: UITableView = UITableView()
- let youtubers = ["Brian Voong", "Seth Everman", "Dave Lee", "Cybershell", "Bill Wurtz"]
- struct Cells {
- static let photoCell: String = "PictureCell"
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- title = titleName
- configureTableView()
- }
- func configureTableView() {
- view.addSubview(tableView)
- setTableViewDelegates()
- tableView.rowHeight = 100
- tableView.register(PictureCell.self, forCellReuseIdentifier: Cells.photoCell)
- tableView.pin(to: view)
- }
- func setTableViewDelegates() {
- tableView.delegate = self
- tableView.dataSource = self
- }
- }
- extension ViewController: UITableViewDelegate, UITableViewDataSource {
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: Cells.photoCell) as! PictureCell
- // =========================================================//
- cell.youtuberLabel?.text = youtubers[indexPath.row]
- cell.youtuber = youtubers[indexPath.row]
- cell.delegate = self
- // =========================================================//
- //cell.set(picture: picture)
- return cell
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return 5
- }
- }
- extension ViewController: CellDelegate {
- func youtuberTableViewCell(_ youtuberTableViewCell: PictureCell, subscribeButtonTappedFor youtuber: String) {
- let alert = UIAlertController(title: "Subscribed!", message: "Subscribed to \(youtuber)", preferredStyle: .alert)
- let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
- alert.addAction(okAction)
- self.present(alert, animated: true, completion: nil)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement