After update tableview did not work. Why is this happen? - ios

I have interesting problem in my machine. I want to make table view and I did it many times but after the update its not working. Is the making way is changed or what? Can anyone help me?
Here my controller and delegate methods:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let tableView: UITableView = {
let tv = UITableView()
tv.separatorStyle = .none
tv.allowsSelection = false
return tv
}()
override func viewDidLoad() {
super.viewDidLoad()
titleOfapp ()
setupTableView()
}
let sipCellId = "sipCellId"
func titleOfapp () {
let titleLabelMarka = UILabel()
titleLabelMarka.textAlignment = .center
titleLabelMarka.text = "Pak Terminal"
titleLabelMarka.textColor = UIColor.white
titleLabelMarka.font = UIFont(name:"Comfortaa-bold", size: 24)
navigationItem.titleView = titleLabelMarka
navigationController?.navigationBar.barTintColor = UIColor.init(red: 80.0/255.0, green: 197.0/255.0, blue: 247.0/255.0, alpha: 1.0)
}
func setupTableView() {
tableView.delegate = self
tableView.dataSource = self
tableView.register(sipCell.self, forCellReuseIdentifier: sipCellId)
view.addSubview(tableView)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: sipCellId, for: indexPath) as! sipCell
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
}
And here my custom cell code:
class sipCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let cellView: UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()
var detailLabel: UILabel = {
let tf = UILabel()
tf.text = "mehmet akyol"
tf.textColor = .black
tf.font = UIFont(name: "Comfortaa-Bold", size: 13)
return tf
}()
func setup(){
addSubview(cellView)
addSubview(detailLabel)
cellView.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 4, paddingLeft: 8, paddingBottom: 4, paddingRight: 8, width: 60, height: 55)
detailLabel.anchor(top: nil, left: cellView.leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 4, paddingLeft: 8, paddingBottom: 4, paddingRight: 8, width: 60, height: 40)
detailLabel.centerYAnchor.constraint(equalTo: cellView.centerYAnchor).isActive = true
}
}

You need to create constraints for the table
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
tableView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor)
])
or set a frame

Related

TableView inside UIView doesn't show up Swift

I’m having a problem with embedding tableView inside a UIView. I tell you what my goal is:
My goal is to have a little tableView with 3 rows (like here - Link), now I’ve made a custom UIView with a tableView inside but for some reason it doesn’t load my data + doesn’t show anything. I must be honest I’m not sure if it is the right way of doing that, cause maybe there’s a better way of doing that, so I hope you guys can help me out.
Here's my code:
import Foundation
import UIKit
class TypeTableView: UIView, UITableViewDataSource, UITableViewDelegate {
var typesData = ["Videos","Images","Posts"]
var tableView = UITableView()
// let screenHeight = UIScreen.main.bounds.height
// let screenWidth = UIScreen.main.bounds.width
//
override init(frame: CGRect){
super.init(frame: frame)
self.backgroundColor = .red
setup()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func setup() {
tableView = UITableView(frame: CGRect(x: 0, y: 0, width: 1000, height: 100),style: .grouped)
tableView.backgroundColor = .red
tableView.layer.cornerRadius = 10
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
self.addSubview(tableView)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return typesData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
cell.textLabel?.text = typesData[indexPath.row]
return cell
}
}
Here's the custom cell of the tableView(the tableView is inside the customCell):
import UIKit
class ByType: UITableViewCell {
// var typesData = ["Videos","Images","Posts"]
#IBOutlet var groupedTableView: TypeTableView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
// self.preservesSuperviewLayoutMargins = false
self.separatorInset = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
self.layoutMargins = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
groupedTableView.layer.cornerRadius = 5
groupedTableView.layer.masksToBounds = true;
groupedTableView.backgroundColor = .red
groupedTableView.layer.borderWidth = 0.5
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}

Display array of images on a certain amount of tableview cells

I have an UIImageView inside my table view cell, named pic. I want to use an array named colors (filled with UIImages), to display on 3 tableview cells.
I have the ViewController class and the tableview cell class listed below. The tableview displays the imageview pic. I assume you would place the color array in cellForRowAt method.
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var colors:[UIImage] = [
UIImage(named: "blue")!,
UIImage(named: "red")!,
UIImage(named: "red")!
]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 118
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! customtv
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
class customtv: UITableViewCell {
lazy var backView : UIView = {
let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width , height: 110))
view.backgroundColor = .green
return view
}()
lazy var pic : UIImageview = {
let view = UIImageview(frame: CGRect(x: 100, y: 6, width: 100 , height: 100))
view.backgroundColor = .red
return view
}()
override func layoutSubviews() {
backView.clipsToBounds = true
backView.frame = CGRect(x: 0, y: 6, width: bounds.maxX , height: 110)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(animated, animated: true)
addSubview(backView)
addSubview(pic)
}
}
Try with this example below
import UIKit
class ViewController1: UIViewController {
var tableView: UITableView?
var colors:[UIImage] = [
UIImage(named: "blue")!,
UIImage(named: "red")!,
UIImage(named: "red")!
]
override func viewDidLoad() {
super.viewDidLoad()
loadUI()
}
func loadUI() {
tableView = UITableView()
self.view.addSubview(tableView.unsafelyUnwrapped)
tableView?.register(CustomTVC.self, forCellReuseIdentifier: "cell")
tableView?.delegate=self
tableView?.dataSource=self
tableView?.translatesAutoresizingMaskIntoConstraints = false
tableView?.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
tableView?.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
tableView?.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
tableView?.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
}
}
extension ViewController1: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 118
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTVC
cell.pic.image = colors[indexPath.row]
return cell
}
}
class CustomTVC: UITableViewCell {
lazy var backView : UIView = {
let view = UIView()
view.backgroundColor = .green
return view
}()
lazy var pic : UIImageView = {
let view = UIImageView()
view.backgroundColor = .red
return view
}()
override func awakeFromNib() {
super.awakeFromNib()
commonInit()
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override func layoutSubviews() {
backView.clipsToBounds = true
backView.frame = CGRect(x: 0, y: 6, width: bounds.maxX , height: 110)
}
func commonInit() {
contentView.addSubview(backView)
backView.translatesAutoresizingMaskIntoConstraints = false
backView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 4).isActive = true
backView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 4).isActive = true
backView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -4).isActive = true
backView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -4).isActive = true
backView.addSubview(pic)
pic.translatesAutoresizingMaskIntoConstraints = false
pic.topAnchor.constraint(equalTo: backView.topAnchor, constant: 4).isActive = true
pic.leftAnchor.constraint(equalTo: backView.leftAnchor, constant: 4).isActive = true
pic.bottomAnchor.constraint(equalTo: backView.bottomAnchor, constant: -4).isActive = true
pic.widthAnchor.constraint(equalTo: pic.heightAnchor).isActive = true
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(animated, animated: true)
}
}

Using a programmatic custom UILabel in a UITableView

I'm attempting to use a custom UITableViewCell that I created programmatically in a UITableView.
The UITableViewCell class:
class PopulatedCellStyleTableViewCell: UITableViewCell {
private let userDisplayName: UILabel = {
let lbl = UILabel()
lbl.font = UIFont(name: "Baskerville-Bold", size: 16)
lbl.textColor = .purple
lbl.textAlignment = .left
lbl.backgroundColor = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 0.5)
return lbl
}()
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
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(userDisplayName)
//from a UIKit extension that allows me to easily add anchors
userDisplayName.anchor(top: self.topAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, paddingTop: 5, paddingLeft: 0, paddingBottom: 5, paddingRight: 0, width: self.frame.width, height: self.frame.height, enableInsets: false)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
And the UITableViewController class:
class LoveListController: UITableViewController {
var loveList: [String]!
let populatedCell = "populatedCell"
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(PopulatedCellStyleTableViewCell.self, forCellReuseIdentifier: populatedCell)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return loveList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: populatedCell, for: indexPath)
// Configure the cell...
cell.textLabel?.text = loveList[indexPath.row]
return cell
}
}
Upon inspecting the debug view hierarchy, the labels load in, but the actual text is rendered in a UITableViewCellContentView instead of in my UILabel, and I have no idea why.
If this is easily solvable or has previously been discussed, please point me in the right direction and/or teach me how to discover the solution myself!
You need to cast the cell and use your custom userDisplayName not textLabel
let cell = tableView.dequeueReusableCell(withIdentifier: populatedCell, for: indexPath) as! PopulatedCellStyleTableViewCell
cell.userDisplayName.text = loveList[indexPath.row]
plus it's better to add the label to contentView and create the constraints with it
self.contentView.addSubview(userDisplayName)
self.userDisplayName.anchor(top: self.contentView.topAnchor .....

Change Spacing of Cocoa Class TableViewCell in ViewController - Swift

I have searched for hours trying stackOverFlows solutions to this issue!!! All of which cause my custom UITable Cells to overlap and not space out. I just want my spacing to be automatic like 'return UITableView.automaticDimension' but say + 8 so that my custom cells aren't right next to each other. I cannot figure it out and maybe it is because I have one line of code that I shouldn't.
This is my cocoa touch class that creates my custom cell 'DiscussionTableViewCell':
class DiscussionTableViewCell: UITableViewCell {
lazy var backView: UIView = {
let view = UIView(frame: CGRect(x: 10, y: 6, width: UIScreen.main.bounds.width - 20, height: 55))
view.backgroundColor = UIColor.rgb(r: 194, g: 208, b: 242)
//view.layer.borderWidth = 3
return view
}()
lazy var discussionTitleLabel: UILabel = {
let LDiscussionTitleLabel = UILabel(frame: CGRect(x: 4, y: 6, width: backView.frame.width - 116, height: 30))
LDiscussionTitleLabel.textAlignment = .left
LDiscussionTitleLabel.font = UIFont.boldSystemFont(ofSize: 18)
return LDiscussionTitleLabel
}()
lazy var discussionDescriptionLabel: UILabel = {
let LDiscussionDescriptionLabel = UILabel(frame: CGRect(x: 4, y: 27, width: backView.frame.width - 30, height: 30))
LDiscussionDescriptionLabel.textAlignment = .left
LDiscussionDescriptionLabel.font = UIFont.systemFont(ofSize: 16)
LDiscussionDescriptionLabel.textColor = UIColor.gray
return LDiscussionDescriptionLabel
}()
override func awakeFromNib() {
super.awakeFromNib()
//layoutMargins = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
// Initialization code
}
override func layoutSubviews() {
//super.layoutSubviews()
contentView.backgroundColor = UIColor.rgb(r: 203, g: 215, b: 242)
backgroundColor = UIColor.yellow
backView.layer.cornerRadius = 5
backView.clipsToBounds = true
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
addSubview(backView)
backView.addSubview(discussionTitleLabel)
backView.addSubview(discussionDescriptionLabel)
// Configure the view for the selected state
}
}
And a section of my ViewController that applies to assigning text to the table cell, and spacing between cells:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return discussionTitles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DiscussionTableViewCell
cell.discussionTitleLabel.text = discussionTitles[indexPath.row]
cell.discussionDescriptionLabel.text = discussionDescriptions[indexPath.row]
return cell
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (singlePageVar == 100) {
return 1407//UITableView.automaticDimension
}
return 62//UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("section: \(indexPath.section)")
print("row: \(indexPath.row)")
var topicNumberRow = (indexPath.row)
mainTopicNumberRow = topicNumberRow
DiscussionBoardTopicThread()
}
//function I call in viewdidload to show table
func setupDiscussionBoard() {
discussionBoardView.separatorStyle = .none
view.addSubview(discussionBoardView)
discussionBoardView.backgroundColor = UIColor(red: 203/255, green: 215/255, blue: 242/255, alpha: 1.0)
discussionBoardView.dataSource = self
discussionBoardView.delegate = self
discussionBoardView.translatesAutoresizingMaskIntoConstraints = false
discussionBoardView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
discussionBoardView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
discussionBoardView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
discussionBoardView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
discussionBoardView.register(DiscussionTableViewCell.self, forCellReuseIdentifier: "cell")
}
}

Calling textViewDidBeginEditing when UITextView is Edited in Custom Cell

I can't seem to get my textViewDidBeginEditing function to call when my textview is being edited in my ViewController.
Here's the code for my custom cell:
class NoteDetailCell: UITableViewCell {
let noteText: UITextView = {
let label = UITextView()
label.font = UIFont(name: "Nunito",size: 16)
label.backgroundColor = UIColor(r: 244, g: 244, b: 255)
label.text = "Have to check out this food place. The best burgers in town."
label.textColor = UIColor(r: 88, g: 88, b: 88)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .`default`, reuseIdentifier: reuseIdentifier)
self.selectionStyle = UITableViewCellSelectionStyle.none
addSubview(noteText)
noteText.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true
noteText.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 8).isActive = true
noteText.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -8).isActive = true
}
}
Here's my code for my tableview:
class TripNoteViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {
var noteDetails: UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
noteDetails.frame = CGRect(x: 0, y: 65, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-140)
noteDetails.delegate = self
noteDetails.dataSource = self
noteDetails.separatorColor = UIColor.clear
noteDetails.register(NoteDetailCell.self, forCellReuseIdentifier: "detailsCell")
noteDetails.register(NoteOriginCell.self, forCellReuseIdentifier: "visitTrip")
noteDetails.register(NoteActionsCell.self, forCellReuseIdentifier: "noteActions")
}
func textViewDidBeginEditing(noteText: UITextView) {
print("noteText text")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: detailsCell) as! NoteDetailCell
cell.noteText.isScrollEnabled = false
cell.noteText.delegate = self
return cell
}
}
I would like to call the textViewDidBeginEditing function from my ViewController when the textfield in my custom cell is being edited. Any suggestions on what I'm getting wrong?
You have not implemented the method defined by the UITextViewDelegate protocol. Change:
func textViewDidBeginEditing(noteText: UITextView) {
print("noteText text")
}
to:
func textViewDidBeginEditing(_ noteText: UITextView) {
print("noteText text")
}
Since you have the wrong signature, it's not called.

Resources