I first programmatically created a tableview :
private func setupTableView() {
tableView = UITableView(frame: CGRect(x: 0, y: 180, width: view.frame.width, height: view.frame.height), style: UITableView.Style.plain)
tableView.dataSource = self
tableView.delegate = self
tableView.register(ItemTableViewCell.self, forCellReuseIdentifier: "Cell")
view.addSubview(tableView)
}
and set the cellForRow method as below :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ItemTableViewCell
guard let finalItems = presenter.finalItems?[indexPath.row] else { return cell }
presenter.configure(cell: cell, FinalItem: finalItems)
return cell
}
Then I configure the ItemTableViewCell as below :
class ItemTableViewCell: UITableViewCell {
private var iconImageView : UIImageView = {
let imgView = UIImageView(image: #imageLiteral(resourceName: "Image"))
imgView.contentMode = .scaleAspectFit
imgView.clipsToBounds = true
return imgView
}()
private var titleLabel : UILabel = {
let lbl = UILabel()
lbl.textColor = .black
lbl.font = UIFont.boldSystemFont(ofSize: 12)
lbl.textAlignment = .left
return lbl
}()
func configure(finalItem: FinalItem) {
titleLabel.text = finalItem.title
iconImageView.downloaded(from: finalItem.images_url.small)
}
}
When I push to the DetailViewController with the uinavigationbar, the elements contained in the rows (titles, labels...) are still visible a few milli seconds:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let finalItem = finalItems[indexPath.row]
let detailViewController = ModuleBuilder.createDetailModuleWith(finalItem)
detailViewController.finalItem = finalItem
navigationController?.pushViewController(detailViewController, animated: true)
}
This is not what I am expecting. I never figure this problem out before.
Related
I have a tableView, that is accessed via a sideMenu where this particular viewController is a child viewController.
For some reason, when I add the tableView.delegate = self, also tableView.dataSource
and register the cell. My app crashes. When I set this view Controller as the initial view controller the table view shows up, however when I return it to my home page, the app crashes, and does not want to load.
My IBOutlets are connected correctly. I even tried connecting the data source and delegate in storyboard.
I have added two types of code for you all to look at, the first, has an IBOutlet, the second I have tried an addSubView(table) however, no table appears.
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(table)
table.delegate = self
table.dataSource = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
Here is my Wallet View Controller
//
// WalletViewController.swift
// handlwithcare
//
// Created by Charles Morgan on 16/10/2021.
//
import UIKit
import RealmSwift
class WalletViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let button = UIButton()
#IBOutlet var table: UITableView!
private var data = [AddCardItem]()
override func viewDidLoad() {
super.viewDidLoad()
table.delegate = self
table.dataSource = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
view.backgroundColor = UIColor(named: "RED")
button.setTitle("Add New Card", for: .normal)
view.addSubview(button)
button.backgroundColor = UIColor(named: "yellow-2")
button.setTitleColor(UIColor(named: "RED"), for: .normal)
button.frame = CGRect(x: 50, y: 800, width: 350, height: 50)
button.layer.cornerRadius = 15
button.addTarget(self, action: #selector(didTapAddButton), for: .touchUpInside)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = data[indexPath.row].item
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
#objc private func didTapAddButton() {
let rootVC = addCardViewController()
let navVC = UINavigationController(rootViewController: rootVC)
navVC.modalPresentationStyle = .fullScreen
present(navVC, animated: true)
}
}
class AddCardItem: Object {
#objc dynamic var item: String = ""
#objc dynamic var date: Date = Date()
}
Here is my Second attempt with a view.addSubView
import UIKit
import RealmSwift
class WalletViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let button = UIButton()
let table = UITableView()
private var data = [AddCardItem]()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(table)
table.delegate = self
table.dataSource = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
view.backgroundColor = UIColor(named: "RED")
button.setTitle("Add New Card", for: .normal)
view.addSubview(button)
button.backgroundColor = UIColor(named: "yellow-2")
button.setTitleColor(UIColor(named: "RED"), for: .normal)
button.frame = CGRect(x: 50, y: 800, width: 350, height: 50)
button.layer.cornerRadius = 15
button.addTarget(self, action: #selector(didTapAddButton), for: .touchUpInside)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = data[indexPath.row].item
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
#objc private func didTapAddButton() {
let rootVC = addCardViewController()
let navVC = UINavigationController(rootViewController: rootVC)
navVC.modalPresentationStyle = .fullScreen
present(navVC, animated: true)
}
}
class AddCardItem: Object {
#objc dynamic var item: String = ""
#objc dynamic var date: Date = Date()
}
Please change the code on the register CELL Tableview.
yourTableView.register(
UINib.init(nibName: "ProductCVC", bundle: nil),
forCellWithReuseIdentifier: "ProductCVC")
I'm a newbie, I have the following problem. There is a screen like this picture. I use UICollectionViewCell for header and I can't addTarget for the blue button in this.
I can’t manage. Could you help me?
class UserProfileVC: UICollectionViewController, UICollectionViewDelegateFlowLayout,UserProfileHeaderDelegate{
var currentUser: User?
var userToLoadFromSearchVC: User?
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// resgiter header class before use
self.collectionView!.register(UserProfileHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader , withReuseIdentifier: headerIdentifier)
// back ground color
self.collectionView?.backgroundColor = .white
//fetch user data
if userToLoadFromSearchVC == nil{
fetchCurrentUserData()
}
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 0
}
// config size for header
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 200)
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
// Declare header
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as! UserProfileHeader
// set delegate
header.delegate = self
if let user = self.currentUser {
header.user = user
} else if let userToLoadFromSearchVC = self.userToLoadFromSearchVC {
header.user = userToLoadFromSearchVC
self.navigationItem.title = userToLoadFromSearchVC.username
}
// Return header
return header
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
This is my code in header file.
class UserProfileHeader: UICollectionViewCell {
var delegate: UserProfileHeaderDelegate?
var user: User? {
didSet {
configuredEditProfileFollowButton()
setUserStats(for: user)
let fullName = user?.name
nameLabel.text = fullName
guard let profileImageUrl = user?.profileImage else {return}
profileImageView.loadImage(with:profileImageUrl)
}
}
let profileImageView : UIImageView = {
let iv = UIImageView()
iv.contentMode = .scaleAspectFill
iv.clipsToBounds = true
iv.backgroundColor = .lightGray
return iv
}()
let nameLabel: UILabel = {
let label = UILabel()
label.font = UIFont.boldSystemFont(ofSize: 12)
return label
}()
let postLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.textAlignment = .center
let attributedText = NSMutableAttributedString(string:"5\n",attributes:[NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 14)])
attributedText.append(NSAttributedString(string:"posts",attributes: [NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 14), NSAttributedString.Key.foregroundColor:UIColor.lightGray]))
label.attributedText = attributedText
return label
}()
let followersLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.textAlignment = .center
return label
}()
let followingLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.textAlignment = .center
let attributedText = NSMutableAttributedString(string:"5\n",attributes:[NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 14)])
attributedText.append(NSAttributedString(string:"following",attributes:[NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 14), NSAttributedString.Key.foregroundColor:UIColor.lightGray]))
label.attributedText = attributedText
return label
}()
let editProfileFollowButton : UIButton = {
let button = UIButton(type: .system)
button.setTitle("Loading", for: .normal)
button.layer.cornerRadius = 5
button.layer.borderColor = UIColor.lightGray.cgColor
button.layer.borderWidth = 0.5
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(handleEditProfileFollow), for: .touchUpOutside)
return button
}()
let gridButton : UIButton = {
let button = UIButton(type: .system)
button.setImage(UIImage(named: "grid"), for: .normal)
return button
}()
let listButton : UIButton = {
let button = UIButton(type: .system)
button.setImage(UIImage(named:"list"), for: .normal)
button.tintColor = UIColor(white: 0, alpha: 0.2)
return button
}()
let bookmarkButton : UIButton = {
let button = UIButton(type: .system)
button.setImage(UIImage(named:"ribbon"), for: .normal)
button.tintColor = UIColor(white: 0, alpha: 0.2)
button.addTarget(self, action: #selector(testFunc(_:)), for: UIControl.Event.touchUpInside)
return button
}()
#objc func testFunc(_ sender : UIButton){
print("Pressed ")
}
#objc func handleFollowersTapped() {
delegate?.handleFollowersTapped(for: self)
}
#objc func handleFollowingTapped() {
delegate?.handleFollowingTapped(for: self)
}
#objc func handleEditProfileFollow() {
guard let user = self.user else {return}
if editProfileFollowButton.titleLabel?.text == "Edit Profile"{
print("Handler edit profile ")
}
else{
if editProfileFollowButton.titleLabel?.text == "Follow"{
editProfileFollowButton.setTitle("Following", for: .normal)
user.follow()
}else{
editProfileFollowButton.setTitle("Follow", for: .normal)
user.unfollow()
}
}
}
This is because you are creating the button target before the UserProfileHeader initialized. So you need to create the button using lazy var (lazily).
private lazy var button: UIButton = {
let button = UIButton()
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(handleButtonTapped), for: .touchUpInside)
return button
}()
#objc private func handleButtonTapped() {
print("Button tapped")
}
Try this, i think this will help you.
class customCell: UICollectionViewCell {
#IBOutlet weak var btn: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: customCell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! customCell
cell.btn.tag = indexPath.row
cell.btn.addTarget(self, action: #selector(self.btnpressed(sender:)), for: .touchUpInside)
return cell
}
#objc func btnpressed(sender: UIButton!) {
print(sender.tag)
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: customCell, for: indexPath) as! customCell
view.pagePluginButtonAction = {
self.TapedBtn()
}
return view
}
func TapedBtn(){
print("click")
}
You need to add the target in 'viewForSupplementaryElementOfKind' where you set up your header. See example below, just after you set the header delegate.
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
// Declare header
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as! UserProfileHeader
// set delegate
header.delegate = self
// Add the target here
header.yourButton.addTarget(self, action: #selector(handleYourButton), for: .touchUpInside)
if let user = self.currentUser {
header.user = user
} else if let userToLoadFromSearchVC = self.userToLoadFromSearchVC {
header.user = userToLoadFromSearchVC
self.navigationItem.title = userToLoadFromSearchVC.username
}
// Return header
return header
}
I would like to make a transition by placing a UITableView within the UINavigationController class and tapping the cell in it.
However, no matter how many times I press cell, it does not make a transition.
The code is the last one, "self.pushViewController (editViewController, animated: true)".
I am assuming navigationPush, but I would like you to tell me why it does not work.
I will ask for answers.
import UIKit
import APIKit
class BookListViewController: UINavigationController {
struct BookInfo {
let bookImage: UIImage?
let bookTitle: UILabel?
let bookPrice: UILabel?
let bookID: UILabel?
}
private lazy var navBar: UINavigationBar = {
let navBar = UINavigationBar()
navBar.frame = CGRect(x: 0, y: view.safeAreaInsets.top, width: view.frame.width, height: 44)
navBar.barTintColor = UIColor.white
let navItem = UINavigationItem()
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(bookAdd))
navItem.rightBarButtonItem = addButton
navItem.title = "書籍一覧"
navBar.pushItem(navItem, animated: false)
return navBar
}()
private lazy var tableView: UITableView = {
let tableView = UITableView()
tableView.frame = CGRect(x: 0, y: navigationBar.frame.size.height + view.safeAreaInsets.top, width: view.frame.size.width, height: view.frame.size.height - view.safeAreaInsets.bottom)
tableView.rowHeight = 100
tableView.delegate = self
tableView.dataSource = self
tableView.register(BookListCell.self, forCellReuseIdentifier: NSStringFromClass(BookListCell.self))
return tableView
}()
private lazy var safeView: UIView = {
let safeView = UIView()
safeView.backgroundColor = UIColor.white
safeView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.safeAreaInsets.top)
return safeView
}()
private lazy var loadButton: UIButton = {
let button = UIButton()
button.setTitle("読込", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 13)
button.backgroundColor = UIColor.blue
button.layer.cornerRadius = 25
button.addTarget(self, action: #selector(moreLoad(sender:)), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
setupUI()
}
}
extension BookListViewController {
private func setupUI() {
view.addSubview(navBar)
view.addSubview(safeView)
view.addSubview(tableView)
view.addSubview(loadButton)
loadButton.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20.0).isActive = true
loadButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -100.0).isActive = true
loadButton.widthAnchor.constraint(equalToConstant: 50.0).isActive = true
loadButton.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
}
#objc func bookAdd() {
let bookAddViewController = BookAddViewController()
self.present(bookAddViewController, animated: true)
}
#objc func moreLoad(sender: UIButton) {
print("moreLoad!!")
}
}
extension BookListViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell: BookListCell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(BookListCell.self), for: indexPath) as? BookListCell {
cell.bookimage.image = UIImage(named: "hoge")
cell.title.text = "書籍一覧"
cell.price.text = "200.000円"
cell.bookDay.text = "2018/12/31"
cell.editButton.setTitle(">", for: .normal)
return cell
}
let cell = UITableViewCell()
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
//個別の情報を載せて遷移
let editViewController = R.storyboard.main.edit()!
self.pushViewController(editViewController, animated: true)
}
}
In storyboard you need to add navigation controller which make this controller as rootviewcontroller. Just by dragging from navigation controller to your tableview controller.
You should extend BookListViewController from UIViewController and present this controller from another controller like this self.present(UINavigationController(rootViewController: BookListViewController()), animated: true) then you can access navigationItem easily with self.navigationItem inside BookListViewController.
self show current context when we use self.navigationController then we are actually telling push to perform that push on that current view's navigation controller
use:
self.navigationController.pushViewController(editViewController, animated: true)
booktable.frame = CGRect(x: 0, y: booktopview.bounds.height, width: screenWidth, height: screenHeight-booktopview.bounds.height-tabbarView.bounds.height)
booktable.register(UITableViewCell.self, forCellReuseIdentifier: "mycell")
booktable.dataSource = self
booktable.delegate = self
booktable.separatorColor = UIColor.lightGray
booktable.backgroundColor = UIColor.clear
booktable.separatorStyle = .singleLine
bookview.addSubview(booktable)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(tableView == booktable)
{
let cell1 = booktable.dequeueReusableCell(withIdentifier: "mycell")
for object in (cell1?.contentView.subviews)!
{
object.removeFromSuperview();
}
let img :UIImageView = UIImageView()
let lbl : UILabel = UILabel()
img.frame = CGRect(x: 15, y: 15, width: 80, height: 130)
img.image = imgarray[indexPath.row]
img.layer.borderWidth = 1.0
img.layer.borderColor = UIColor.lightGray.cgColor
cell1?.contentView.addSubview(img)
imgheight = img.bounds.height
lbl.frame = CGRect(x: img.bounds.width + 40, y: (imgheight+40-80)/2, width: booktable.bounds.width-img.bounds.width + 40 - 100, height: 80)
lbl.text = imgname[indexPath.row]
lbl.numberOfLines = 0
lbl.textAlignment = .left
lbl.font = UIFont(name: "Arial", size: 23)
lbl.textColor = UIColor.black
cell1?.selectionStyle = .none
cell1?.contentView.addSubview(lbl)
return cell1!
}
The code shown above is for book table, which sometimes scrolls like normal and sometimes not scrolling at all. I am doing all the code programatically. I have tested this on both simulators and devices but still the problem exists. Any help is appreciated...
Create Custom UITableViewCell, let's say it is ListTableCell
class ListTableCell: UITableViewCell {
#IBOutlet weak var lblTemp: UILabel!
#IBOutlet weak var imgTemp: UIImage!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
I've created UITableViewCell with xib like this and bind IBOutlets
Let's say we have struct Model and array like this
struct Model {
let image : UIImage
let name: String
}
for i in 0...10 {
let model = Model(image: #imageLiteral(resourceName: "Cat03"), name: "Temp \(i)")
array.append(model)
}
Now on ViewController viewDidLoad() method,
tableView.register(UINib(nibName: "ListTableCell", bundle: nil), forCellReuseIdentifier: "ListTableCell")
Implement UITableViewDataSource methods like this,
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ListTableCell") as! ListTableCell
let model = array[indexPath.row]
cell.lblTemp.text = model.name
cell.imgTemp.image = model.image
return cell
}
FYI
For different tableviews, you can create different custom cell the same way and cellForRowAt indexPath and numberOfRowsInSection method will change appropriately.
Let me know in case of any queries.
UPDATE
Follow this and this to create CustomTableCell programmatically
I have done a UITableViewCell programmatically and it worked just fine with iOS 10. But after updating with iOS 11 and XCode 9, it behaves differently. The layout looks scrambled as below.
But if I tap on the cell then it rearranges and looks fine as below.
Here the code for UITableViewCell
import UIKit
import SnapKit
class AboutCell: UITableViewCell {
let roleLabel : UILabel = {
var tablelabel = UILabel()
tablelabel.font = UIFont (name: "Avenir Next Medium", size: 22)
tablelabel.textAlignment = .center
tablelabel.clipsToBounds = true
tablelabel.translatesAutoresizingMaskIntoConstraints = false
return tablelabel
}()
let nameLabel : UILabel = {
let tablelabel = UILabel()
tablelabel.font = UIFont (name: "Avenir Next Medium", size: 16)
tablelabel.textAlignment = .center
tablelabel.clipsToBounds = true
tablelabel.translatesAutoresizingMaskIntoConstraints = false
return tablelabel
}()
let webUrlLabel : UILabel = {
let tablelabel = UILabel()
tablelabel.font = UIFont (name: "Avenir Next Medium", size: 16)
tablelabel.textAlignment = .center
tablelabel.clipsToBounds = true
tablelabel.translatesAutoresizingMaskIntoConstraints = false
return tablelabel
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}
override func layoutSubviews() {
super.layoutSubviews()
roleLabel.frame = CGRect(x: 0, y: 10, width: self.contentView.bounds.size.width-20, height: 25)
nameLabel.frame = CGRect(x: 0, y: roleLabel.frame.origin.y+25, width: self.bounds.size.width-20, height: 25)
webUrlLabel.frame = CGRect(x: 0, y: nameLabel.frame.origin.y+25, width: self.bounds.size.width-20, height: 25)
}
func setupViews(){
contentView.addSubview(roleLabel)
contentView.addSubview(nameLabel)
contentView.addSubview(webUrlLabel)
}
func setValuesForCell(contributor : Contributors){
roleLabel.text = contributor.contributorRole
nameLabel.text = contributor.contributorName
webUrlLabel.text = contributor.contributorWeb
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
and the extension I wrote for TableView delegate and datasource
extension AboutController : UITableViewDelegate, UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contributorList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : AboutCell = tableView.dequeueReusableCell(withIdentifier: self.cellid, for: indexPath) as! AboutCell
cell.selectionStyle = .default
let contributor : Contributors = contributorList[indexPath.row]
cell.setValuesForCell(contributor: contributor)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
tableView.deselectRow(at: indexPath, animated: true)
}
}
and the ViewDidLoad method
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 100.0
tableView.rowHeight = 100
tableView.register(AboutCell.self, forCellReuseIdentifier: self.cellid)
view.addSubview(tableView)
tableView.snp.makeConstraints { (make) in
make.top.right.bottom.left.equalToSuperview()
}
let mayu = Contributors(contibutorRole: "Developer", contributorName: "J Mayooresan", contributorWeb: "http://jaymayu.com")
let janie = Contributors(contibutorRole: "Voice Artist", contributorName: "M Jananie", contributorWeb: "http://jaymayu.com")
let arjun = Contributors(contibutorRole: "Aathichudi Content", contributorName: "Arjunkumar", contributorWeb: "http://laymansite.com")
let artist = Contributors(contibutorRole: "Auvaiyar Art", contributorName: "Alvin", contributorWeb: "https://www.fiverr.com/alvincadiz18")
contributorList = [mayu, arjun, janie, artist]
tableView.delegate = self
tableView.dataSource = self
self.tableView.reloadData()
}
Since you're laying out your tableView using autolayout, you also need to ensure translatesAutoresizingMaskIntoConstraints is set to false.
SnapKit should be setting the tableView's translatesAutoresizingMaskIntoConstraints to false for you already.
Since you're laying out the cells manually (using frames in layoutSubviews). Setting the cells subview's translatesAutoresizingMaskIntoConstraints to false is likely not needed.
See Apple Docs here for translatesAutoresizingMaskIntoConstraints