Swift - Adding target to UIButton in UITableView not working - ios

I have a UITableView, and in each of the UITableViewCells, I have a button, which should perform an action when selected. If the user decides the select the actual cell, I handle another action within the didSelectRowAt function. I am not sure if I am missing something, but my button does not work when selected, but performs the didSelectRowAt function.
The layout and constraints work just fine. It is just not recognizing the tap, and then pushes to the didSelectRowAt
UIViewController Code (Clipped)
class MyProfile: UIViewController, UITableViewDelegate, UITableViewDataSource {
let tableView : UITableView = {
let tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.separatorStyle = .none
tableView.backgroundColor = UIColor.white
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 10).isActive = true
tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 10).isActive = true
view.backgroundColor = UIColor.white
tableView.delegate = self
tableView.dataSource = self
tableView.register(ProfileCell.self, forCellReuseIdentifier: "profileCell")
// data and backend stuff to reload tableview has been removed, so there is not too much code
// Do any additional setup after loading the view.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
self.myServices.text = "My Services"
return myJobs.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "profileCell", for: indexPath) as! ProfileCell
cell.selectionStyle = .none
cell.deleteButton.addTarget(self, action: #selector(deletePostPressed), for: .touchUpInside)
cell.editButton.addTarget(self, action: #selector(editPostPressed), for: .touchUpInside)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.navigationController?.pushViewController(ViewPostController(), animated: true)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 175
}
#objc func deletePostPressed() {
print("delete")
}
#objc func editPostPressed() {
print("edit")
self.present(Home(), animated: true, completion: nil)
}
}
UITableViewCell Code (Clipped)
class ProfileCell: UITableViewCell {
let editButton : UIButton = {
let button = UIButton(type: .system)
button.setTitle("Edit", for: .normal)
button.setTitleColor(UIColor.mainBlue, for: .normal)
button.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1)
button.isUserInteractionEnabled = true
button.translatesAutoresizingMaskIntoConstraints = false
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.mainBlue.cgColor
return button
}()
let deleteButton : UIButton = {
let button = UIButton(type: .system)
button.setTitle("Delete", for: .normal)
button.setTitleColor(UIColor.mainBlue, for: .normal)
button.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1)
button.isUserInteractionEnabled = true
button.translatesAutoresizingMaskIntoConstraints = false
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.mainBlue.cgColor
return button
}()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.selectionStyle = .none
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
isUserInteractionEnabled = true
addSubview(editButton)
editButton.layer.cornerRadius = 15
editButton.layer.masksToBounds = true
editButton.topAnchor.constraint(equalTo: saleNumber.bottomAnchor, constant: 7).isActive = true
editButton.rightAnchor.constraint(equalTo: informationView.rightAnchor, constant: -15).isActive = true
editButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
editButton.widthAnchor.constraint(equalToConstant: 85).isActive = true
addSubview(deleteButton)
deleteButton.layer.cornerRadius = 15
deleteButton.layer.masksToBounds = true
deleteButton.topAnchor.constraint(equalTo: saleNumber.bottomAnchor, constant: 7).isActive = true
deleteButton.rightAnchor.constraint(equalTo: editButton.leftAnchor, constant: -15).isActive = true
deleteButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
deleteButton.widthAnchor.constraint(equalToConstant: 85).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
I am not sure what else could be the problem. I have declared delegates, corrected everything, and made sure everything else that needed to be initialized was, and still it doesn't work.

It's a matter of hierarchy, you need to add the buttons to the contentView. If you just add a subview to the view, it goes behind the contentView of the UITableViewCell.
Visually, where the selected one is the contentView of the cell, you can see that the actual button is behind it:
So contentView.addSubview(editButton) and contentView.addSubview(deleteButton) does the job.

Related

IOS 16 UITableViewCell accessibility API not working, breaking XCTest

Since the upgrade to IOS 16 the accessibility api for cells has stopped working.
Using this API's is not working anymore:
In a CellConfiguration
public struct CellConfiguration: UIContentConfiguration {
public func makeContentView() -> UIView & UIContentView {
CellContent(configuration: self)
}
public func updated(for state: UIConfigurationState) -> CellConfiguration {
self
}
}
private class CellContent: UIView, UIContentView {
init(configuration: UIContentConfiguration) {
self.configuration = configuration
super.init(frame: .zero)
isAccessibilityElement = true
accessibilityLabel = "I'm a label"
accessibilityValue = "I'm a value"
accessibilityTraits = [.header] // or other traits as well
accessibilityIdentifier = "cellID"
}
}
In a UITableViewController
private func createCell(in tableView: UITableView) -> UITableViewCell? {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier")
else { return nil }
cell.contentConfiguration = CellConfiguration()
cell.selectionStyle = .none
return cell
}
The cell is not accessible in the simulator and on the phone. I can't find it. This has worked prior to IOS16. Thus breaking any existing XCTest's and any progress implementing app that has accessibility.
This is the right way, declare objects and set attributed an constraints:
class YourViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let cellId = "cellId"
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
fileprivate func setupTableView() {
view.backgroundColor = .white
view.addSubview(tableView)
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.delegate = self
tableView.dataSource = self
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(MyCell.self, forCellReuseIdentifier: cellId) // register your cell
tableView.backgroundColor = #colorLiteral(red: 0.94826442, green: 0.9495814443, blue: 0.96922189, alpha: 1)
tableView.separatorColor = UIColor(white: 1, alpha: 0.4)
//tableView.tableFooterView = UIView()
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0 // remove blank space on top o tableView
} else {
UITableView.appearance().sectionHeaderTopPadding = CGFloat(0) // remove blank space on top o tableView in old OS
}
now set delegate and datasource:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = UIView()
let label = UILabel()
switch section {
case 0:
v.backgroundColor = #colorLiteral(red: 0.94826442, green: 0.9495814443, blue: 0.96922189, alpha: 1)
label.text = "Section 1"
default:
label.text = "Section 2"
v.backgroundColor = #colorLiteral(red: 0.94826442, green: 0.9495814443, blue: 0.96922189, alpha: 1)
}
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = .systemBlue
v.addSubview(label)
label.topAnchor.constraint(equalTo: v.topAnchor).isActive = true
label.leadingAnchor.constraint(equalTo: v.leadingAnchor, constant: 20).isActive = true
label.trailingAnchor.constraint(equalTo: v.trailingAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: v.bottomAnchor).isActive = true
return v
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 1:
return 1
default:
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! MyCell
let section = indexPath.section
switch section {
case 0:
cell.myLabel.text = "Title: I'm a label"
cell.subtitle.text = "Subtitle: I'm a value"
default:
cell.myLabel.text = "Title: I'm a label2"
cell.subtitle.text = "Subtitle: I'm a value2"
}
return cell
}
Your cell looks like:
class MyCell: UITableViewCell {
let myLabel: UILabel = {
let label = UILabel()
label.textColor = .black
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let subtitle: UILabel = {
let label = UILabel()
label.textColor = .gray
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.backgroundColor = .white
let stackView = UIStackView(arrangedSubviews: [myLabel, subtitle])
stackView.distribution = .fillEqually
stackView.axis = .vertical
stackView.spacing = 2
stackView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(stackView)
stackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20).isActive = true
stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20).isActive = true
stackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -20).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
this is the result:

swift UIViwe.animate not work in UITableViewCell

I made an animation that moves an orange View when a button is pressed.
blue = UIButton, orange = animation target View
for button in toggleButtons {
button.addTarget(self, action: #selector(toggleButtonAction), for: .touchUpInside)
}
I created 3 buttons and wrote logic to move them to the position of the buttons, but the orange view moves, but no animation is applied.
#objc func toggleButtonAction(_ sender: UIButton) {
self.toggleLeading.constant = sender.frame.width * CGFloat(sender.tag)
UIView.animate(withDuration: 3, delay: 0, options: .curveEaseIn, animations: {
self.layoutIfNeeded()
}, completion: nil)
}
The code above is my animation code. It works fine in other UIViewControllers, but it doesn't work in the Custom TableviewCell.
When the button is pressed, the orangeView moves, but it moves like a teleportation rather than an animation.
I think we need more code to see why is not working for you but I quickly tried this on a playground and seems to be working fine:
import PlaygroundSupport
import UIKit
class MyCell: UITableViewCell {
private var leadingConst: NSLayoutConstraint!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let view = UILabel()
view.text = "Move view"
view.backgroundColor = .orange
view.translatesAutoresizingMaskIntoConstraints = false
let buttons: [UIButton] = (0...2).map {
let button = UIButton()
button.backgroundColor = .blue
button.tag = $0
button.setTitle("\($0 + 1)", for: .normal)
button.addTarget(self, action: #selector(onTap), for: .touchUpInside)
return button
}
let stackView = UIStackView(arrangedSubviews: buttons)
stackView.distribution = .fillEqually
stackView.axis = .horizontal
stackView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(stackView)
contentView.addSubview(view)
leadingConst = view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 0)
NSLayoutConstraint.activate([
leadingConst,
view.heightAnchor.constraint(equalToConstant: 25),
view.widthAnchor.constraint(equalTo: stackView.widthAnchor, multiplier: 1 / 3),
view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
stackView.topAnchor.constraint(equalTo: contentView.topAnchor),
stackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
stackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])
}
required init?(coder: NSCoder) {
nil
}
#objc func onTap(_ sender: UIButton) {
self.leadingConst.constant = sender.frame.width * CGFloat(sender.tag)
UIView.animate(withDuration: 3, delay: 0, options: .curveEaseIn, animations: {
self.contentView.layoutIfNeeded()
}, completion: nil)
}
}
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath)
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
50
}
override func numberOfSections(in tableView: UITableView) -> Int {
1
}
}
PlaygroundPage.current.liveView = ViewController()

How to put a floating action button in a tableView in swift in iOS?

I am trying to use an floating action button in iOS to impose on a table view so that I can add items in the tableview with that . please help me with the code.
Here is the complete code for it. It has been done without using storyboard.
TableView:
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
let nameArray = ["India","Usa","UK"]
let tableView: UITableView = {
let table = UITableView()
table.translatesAutoresizingMaskIntoConstraints = false
return table
}()
let btnFloating : UIButton = {
let floating = UIButton()
floating.translatesAutoresizingMaskIntoConstraints = false
floating .backgroundColor = .cyan
floating.setTitle("ADD", for: .normal)
return floating
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.addSubview(btnFloating)
tableView.dataSource = self
setuoConstrain()
//Set the action of add button
btnFloating.addTarget(self, action: #selector(btnAddTapp(sender:)), for: .touchUpInside)
}
func setuoConstrain(){
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
//Constrain For Button :
btnFloating.heightAnchor.constraint(equalToConstant: 64).isActive = true
btnFloating.widthAnchor.constraint(equalToConstant: 64).isActive = true
btnFloating.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -24).isActive = true
btnFloating.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -36).isActive = true
}
//This function is for add button . What action you want , can put inside this function
#objc func btnAddTapp(sender: UIButton){
print("add button tapp")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let nameCell = NameTableCell(style: .default, reuseIdentifier: "NameTableCell")
nameCell.lblName.text = nameArray[indexPath.row]
return nameCell
}
}
TableViewCell:
import UIKit
class NameTableCell: UITableViewCell {
let lblName: UILabel = {
let name = UILabel()
name.translatesAutoresizingMaskIntoConstraints = false
return name
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addSubview(lblName)
constrain()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func constrain(){
lblName.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
}
}
func setupFloatingActionButton() {
Floaty.global.button.buttonImage = UIImage(named: "icon-social")
Floaty.global.button.buttonColor = UIColor.white
let facebookItem = FloatyItem()
facebookItem.icon = UIImage(named: "icon-facebook")
facebookItem.title = "Facebook"
Floaty.global.button.addItem(item: facebookItem)
let gmailItem = FloatyItem()
Floaty.global.button.addItem("Gmail", icon: UIImage(named: "icon-gmail"), handler: {_
in
print("Gmail Button tapp")
})
let twitterItem = FloatyItem()
Floaty.global.button.addItem("Twitter", icon: UIImage(named: "icon-twitter"), handler: {_ in
print("twitter Button tapp")
})
//Floaty.global.button.animationSpeed = 0.50
Floaty.global.button.openAnimationType = .fade
//Floaty.global.button.rotationDegrees = 90.00
Floaty.global.show()
}

UITableViewCell delegate not working

Conditions:
Swift 4, Xcode 9.3
Target: iOS 11.3
UI Done Programatically
Using Constraints
Episode is an object that holds the source as a String
Situation:
Here is my custom cell: EpisodeCell.swift
import UIKit
protocol EpisodeCellDelegate {
func didTapPlayButton(url: String)
}
class EpisodeCell: UITableViewCell {
var delegate: EpisodeCellDelegate?
var episode: Episode!
let cellView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.init(hex: "#EBE4D3")
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let episodeTitle: UILabel = {
let label = UILabel()
label.textColor = .darkGray
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let playButton: UIButton = {
let btn = UIButton.init(type: .custom)
btn.setTitle("PLAY", for: .normal)
btn.setTitleColor(.gray, for: .normal)
btn.isUserInteractionEnabled = true
btn.addTarget(self, action: #selector(playPressed), for: .touchUpInside)
return btn
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}
private func setup(){
self.accessoryType = .none
self.addSubview(cellView)
cellView.addSubview(episodeTitle)
cellView.addSubview(playButton)
cellView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
cellView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
cellView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
cellView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
episodeTitle.topAnchor.constraint(equalTo: cellView.topAnchor, constant: 10).isActive = true
episodeTitle.leadingAnchor.constraint(equalTo: cellView.leadingAnchor, constant: 10).isActive = true
episodeTitle.trailingAnchor.constraint(equalTo: cellView.trailingAnchor).isActive = true
episodeTitle.centerYAnchor.constraint(equalTo: cellView.centerYAnchor).isActive = true
playButton.translatesAutoresizingMaskIntoConstraints = false
playButton.trailingAnchor.constraint(equalTo: cellView.trailingAnchor, constant: -10).isActive = true
playButton.centerYAnchor.constraint(equalTo: cellView.centerYAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("initCoder has not been implemented")
}
#objc func playPressed() {
self.delegate?.didTapPlayButton(url: episode.source)
}
}
And here is how I implemented on my View Controller with the tableview: EpisodesViewController.swift
extension EpisodesViewController: EpisodeCellDelegate {
func didTapPlayButton(url: String) {
print("WOAH: \(url)")
}
}
extension EpisodesViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "episodeCell") as! EpisodeCell
cell.episode = series.episodes![indexPath.row]
cell.episodeTitle.text = ep.episodeName
cell.delegate = self
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (series.episodes?.count)!
}
}
Problem:
I'm having difficulty in working the button to be tapped in a custom table view cell. My tableview conforms to the protocol but it doesn't work.
You should do lazy initialisation for control inside the tableviewcell. Below code does the magic for me. Just change the below part of the code alone
lazy var cellView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
lazy var episodeTitle: UILabel = {
let label = UILabel()
label.textColor = .darkGray
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy var playButton: UIButton = {
let btn = UIButton.init(type: .custom)
btn.setTitle("PLAY", for: .normal)
btn.setTitleColor(.gray, for: .normal)
btn.isUserInteractionEnabled = true
btn.addTarget(self, action: #selector(playPressed), for: .touchUpInside)
return btn
}()
You shouldn't have the action that a cell performs inside the cell. Cells can be reused, and a cell that was in row 1 can end up in row 4 at any time. Instead of your playPressed() routine, use the traditional didSelectRowAtIndexPath call, which uses the IndexPath, not the cell itself, to determine the action to take.

UITableView not appearing properly using constraints

I'm having problems displaying this UITableView under the UIImageView. It does this weird thing, where you can't see the table or anything at all, however when you scroll up or down, you can see the blue cell appear in the 40px high UITableView.
class TvDetailHeader: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
private let contributorCellId = "contributorCellId"
let thumbnailImageView: UIImageView = {
let imageView = UIImageView(image: #imageLiteral(resourceName: "stranger things poster"))
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.layer.cornerRadius = 6
imageView.contentMode = .scaleAspectFill
imageView.layer.masksToBounds = true
return imageView
}()
let contributorTableView: UITableView = {
let tableView = UITableView()
tableView.separatorStyle = .none
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.backgroundColor = .blue
return tableView
}()
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: contributorCellId, for: indexPath) as! ContributorCell
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
override func setupViews() {
super.setupViews()
contributorTableView.delegate = self
contributorTableView.dataSource = self
contributorTableView.register(ContributorCell.self, forCellReuseIdentifier: contributorCellId)
backgroundColor = .white
addSubview(thumbnailImageView)
addSubview(contributorTableView)
thumbnailImageView.topAnchor.constraint(equalTo: topAnchor, constant: 25).isActive = true
thumbnailImageView.leftAnchor.constraint(equalTo: leftAnchor, constant: 20).isActive = true
thumbnailImageView.widthAnchor.constraint(equalToConstant: 60).isActive = true
thumbnailImageView.heightAnchor.constraint(equalToConstant: 90).isActive = true
contributorTableView.topAnchor.constraint(equalTo: thumbnailImageView.bottomAnchor, constant: 10).isActive = true
contributorTableView.leftAnchor.constraint(equalTo: leftAnchor, constant: 20).isActive = true
contributorTableView.widthAnchor.constraint(equalToConstant: frame.width).isActive = true
contributorTableView.heightAnchor.constraint(equalToConstant: 40).isActive = true
}
}
class ContributorCell: UITableViewCell {
let cellView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
addSubview(cellView)
backgroundColor = .blue
cellView.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
cellView.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true
cellView.widthAnchor.constraint(equalToConstant: 30).isActive = true
cellView.heightAnchor.constraint(equalToConstant: frame.height).isActive = true
}
}

Resources