tableView.reloadData() only called once - ios

I'm fairly new to Swift and I'm trying to make a tableView appear in a popup as shown here.
I have the datasource and delegate set to self and I call reloadData() after fetching data from Cloud Firestore. The thing is, numberOfRowsInSection might get called once, but can't get called again. CellForRowAt never gets called.
Does this have to do with the fact that I made my tableView programmatically? Something like it doesn't think the frame is set or something, even though it is. The table does work if I just make the table in Xcode manually and link an outlet. The sad thing is I do the same thing in a different view controller, but in that view controller it does work and I can't find any differences in the code.
Here's the function that gets called when you press the button
#IBAction func ShowTeams(_ sender: Any) {
RefreshData()
let startPoint = CGPoint(x: self.btnShowTeams.frame.origin.x + 15, y: self.btnShowTeams.frame.origin.y + 23)
tblTeams = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180))
tblTeams.register(UITableViewCell.self, forCellReuseIdentifier: "cellTeams")
let popover = Popover()
tblTeams.rowHeight = 35
tblTeams.contentInset = UIEdgeInsets(top: 15,left: 0,bottom: 0,right: 0)
tblTeams.separatorColor = UIColor(hexFromString: "13293d")
popover.show(tblTeams, point: startPoint)
}
Here are the functions that set up the tableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if tableView == tblTeams{
print("this shows like once, and yes there's data in dataTeams")
return dataTeams.count
}else{
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == tblTeams{
print("this doesnt show")
let cell = tableView.dequeueReusableCell(withIdentifier: "cellTeams", for: indexPath)
cell.textLabel?.textAlignment = .center
cell.textLabel?.font = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.bold)
cell.accessoryType = .disclosureIndicator
cell.textLabel!.text = dataTeams[indexPath.row]
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: "cellInvites", for: indexPath)
return cell
}
}
Here's the fetch data function
func RefreshData(){
let db = Firestore.firestore()
let uid = Auth.auth().currentUser!.uid
dataTeams = [String]()
var i = 1
while i <= 6 {
db.collection("teams").whereField("uid\(i)", isEqualTo: uid)
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
self.dataTeams.append((document["username"] as? String)!)
}
print("the code does always make it here, I checked")
self.tblTeams.reloadData()
}
}
i = i+1
}
}
And the stuff at the top for good measure. Thank you!
import UIKit
import Firebase
import FirebaseFirestore
import GradientLoadingBar
import SCLAlertView
import Popover
class Game: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var btnShowTeams: UIButton!
var dataTeams = [String]()
var tblTeams: UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tblTeams.dataSource = self
tblTeams.delegate = self
RefreshData()
}

#IBAction func ShowTeams(_ sender: Any) {
RefreshData()
let startPoint = CGPoint(x: self.btnShowTeams.frame.origin.x + 15, y: self.btnShowTeams.frame.origin.y + 23)
tblTeams = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180))
tblTeams.register(UITableViewCell.self, forCellReuseIdentifier: "cellTeams")
let popover = Popover()
tblTeams.rowHeight = 35
tblTeams.contentInset = UIEdgeInsets(top: 15,left: 0,bottom: 0,right: 0)
tblTeams.separatorColor = UIColor(hexFromString: "13293d")
popover.show(tblTeams, point: startPoint)
}
In the above code you're creating new table view every time you click your button. And the newly created tableview has nil data source and delegate (by default).
So either set data source or delegate in above method
tblTeams.dataSource = self
tblTeams.delegate = self
or reuse the existing table view by replacing
tblTeams = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180))
with
tblTeams.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180)

Related

Segment Controller not scrolling properly

I am trying to implement a method to where I have a tableviewcontroll that has a header where it shows a head picture as well as another section with the User's information and Profile picture. Below the head I want there to be a segment control like twitter that will allow a user to switch between different tableviews. I want that segment control to be below the header view so when a user pulls down, the segment control moves down with the header view, when you scroll up the segment control moves up with the headerview but then sticks below the navigation bar similar to how twitter and instagram's UI works. I have implemented the code below but I am getting an issue to when I scroll up, the segment controller doesn't move but when I pull down on the screen the segment control moves down with the header view. Any Idea why it is doing this?
Here is the code
class RandomTableViewController: UITableViewController {
#IBOutlet weak var trackImage: UIImageView!
private let tableHeaderViewHeight: CGFloat = 320.0
private let tableHeaderViewCutAway: CGFloat = 0.1
var headerView: HeaderView!
var headerMaskLayer: CAShapeLayer!
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
tableView.estimatedSectionHeaderHeight = 40.0
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
tableView.tableFooterView = UIView()
headerView = tableView.tableHeaderView as! HeaderView
// headerView.imageView = trackImage
tableView.tableHeaderView = nil
tableView.addSubview(headerView)
tableView.contentInset = UIEdgeInsets(top: tableHeaderViewHeight, left: 0, bottom: 0, right: 0)
tableView.contentOffset = CGPoint(x: 0, y: -tableHeaderViewHeight + 64)
//cut away header view
headerMaskLayer = CAShapeLayer()
headerMaskLayer.fillColor = UIColor.black.cgColor
headerView.layer.mask = headerMaskLayer
let effectiveHeight = tableHeaderViewHeight - tableHeaderViewCutAway/2
tableView.contentInset = UIEdgeInsets(top: effectiveHeight, left: 0, bottom: 0, right: 0)
tableView.contentOffset = CGPoint(x: 0, y: -effectiveHeight)
updateHeaderView()
}
func updateHeaderView() {
let effectiveHeight = tableHeaderViewHeight - tableHeaderViewCutAway/2
var headerRect = CGRect(x: 0, y: -effectiveHeight, width: tableView.bounds.width, height: tableHeaderViewHeight)
if tableView.contentOffset.y < -effectiveHeight {
headerRect.origin.y = tableView.contentOffset.y
headerRect.size.height = -tableView.contentOffset.y + tableHeaderViewCutAway/2
}
headerView.frame = headerRect
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y:0))
path.addLine(to: CGPoint(x: headerRect.width, y: 0))
path.addLine(to: CGPoint(x: headerRect.width, y: headerRect.height))
path.addLine(to: CGPoint(x: 0, y: headerRect.height - tableHeaderViewCutAway))
headerMaskLayer?.path = path.cgPath
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
self.tableView.decelerationRate = UIScrollView.DecelerationRate.fast
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return UITableView.automaticDimension
}
#IBAction func backButton(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
extension RandomTableViewController {
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = UIView()
v.backgroundColor = .white
let segmentedControl = UISegmentedControl(frame: CGRect(x: 10, y: 5, width: tableView.frame.width - 20, height: 30))
segmentedControl.insertSegment(withTitle: "One", at: 0, animated: false)
segmentedControl.insertSegment(withTitle: "Two", at: 1, animated: false)
segmentedControl.insertSegment(withTitle: "Three", at: 2, animated: false)
v.addSubview(segmentedControl)
return v
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
100
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "CellID")
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
}
extension RandomTableViewController {
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
updateHeaderView()
}
}
I have custom HeaderView code which is
import UIKit
class HeaderView: UIView {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var trackImage: UIImageView!
#IBOutlet weak var backButton: UIButton!
}
Here is what it is doing
UPDATE:
I implemented the grouped tableview and setting the estimatedSectioHeaderHeight in the viewForSectionInHeader and now the segmented control moves up and down but it is not sticking below the navigation bar when scrolling up. Also there seems to be a formatting issue now. Here is how the simulator looks
change the table style to grouped and set height of the segment view in heightForHeaderInSection.
Check whether it works.

UITableView skipping every other cell when populating labels swift

I'm trying to create several UITableViews programmatically in the same scroll view. I'm correctly loading the tables' data into each array, but for some reason the table is populating every other cell. I added a print statement in cellForRowAt: to make sure it wasn't skipping data somehow and it doesn't seem to.
Also, didSelectRowAt: isn't being called when I click on a row, but didHighlightRowAt: is called correctly. And using that the table cells seem to load properly, it's just skipping every other cell when populating them.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cellIdentifier = ""
switch tableView {
case tableView1:
cellIdentifier = "AddOnTableViewCell1"
case tableView2:
cellIdentifier = "AddOnTableViewCell2"
case tableView3:
cellIdentifier = "AddOnTableViewCell3"
case tableView4:
cellIdentifier = "AddOnTableViewCell4"
default:
print("Too many tables")
}
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? AddOnTableViewCell else {
fatalError("Dequed cell error")
}
let titleLabel = UILabel(frame: CGRect(x: 16, y: cell.frame.minY + 10, width: view.frame.width - 30, height: 30))
titleLabel.textColor = .black
titleLabel.textAlignment = .left
switch tableView {
case tableView1:
titleLabel.text = addOnContents1[indexPath.row]
print("\(indexPath.row), \(addOnContents1[indexPath.row])")
case tableView2:
titleLabel.text = addOnContents2[indexPath.row]
print("\(indexPath.row), \(addOnContents2[indexPath.row])")
case tableView3:
titleLabel.text = addOnContents3[indexPath.row]
print("\(indexPath.row), \(addOnContents3[indexPath.row])")
case tableView4:
titleLabel.text = addOnContents4[indexPath.row]
print("\(indexPath.row), \(addOnContents4[indexPath.row])")
default:
print("Too many tables")
}
cell.addSubview(titleLabel)
return cell
}
The print statements above show that the data added correctly corresponds to the indexRow.path, i.e. 0 is item00, 1 is item01, 2 is item02, 3 is item03, ...
Then here are the other two methods mentioned.
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
switch tableView {
case tableView1:
print(addOnContents1[indexPath.row])
case tableView2:
print(addOnContents2[indexPath.row])
case tableView3:
print(addOnContents3[indexPath.row])
case tableView4:
print(addOnContents4[indexPath.row])
default:
print("Too many tables")
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Pressed")
switch tableView {
case tableView1:
print(indexPath.row)
case tableView2:
print(indexPath.row)
case tableView3:
print(indexPath.row)
case tableView4:
print(indexPath.row)
default:
print("Too many tables")
}
}
Finally, here's my code for setting up the tableViews
func loadAddOnViews() {
var count = 1
for item in addOns {
var contentSize: CGFloat = 0
switch count {
case 1:
let label = UILabel(frame: CGRect(x: 16, y: yCoordinateForNewContent, width: scrollView.frame.width - 32, height: 50))
label.text = addOns[0].title
yCoordinateForNewContent += 50
scrollView.addSubview(label)
contentSize = CGFloat(addOnContents1.count)
print("content size: \(contentSize)")
contentSize = contentSize * cellSize
tableView1 = UITableView(frame: CGRect(x: 0, y: yCoordinateForNewContent, width: scrollView.frame.width, height: contentSize))
scrollView.addSubview(tableView1)
tableView1.delegate = self
tableView1.dataSource = self
self.tableView1.register(AddOnTableViewCell.self, forCellReuseIdentifier: "AddOnTableViewCell1")
yCoordinateForNewContent += contentSize + 20
tableView1.reloadData()
case 2:
let label = UILabel(frame: CGRect(x: 16, y: yCoordinateForNewContent, width: scrollView.frame.width - 32, height: 50))
label.text = addOns[1].title
yCoordinateForNewContent += 50
scrollView.addSubview(label)
contentSize = CGFloat(addOnContents2.count)
print("content size: \(contentSize)")
contentSize = contentSize * cellSize
tableView2 = UITableView(frame: CGRect(x: 0, y: yCoordinateForNewContent, width: scrollView.frame.width, height: contentSize))
scrollView.addSubview(tableView2)
tableView2.delegate = self
tableView2.dataSource = self
self.tableView2.register(AddOnTableViewCell.self, forCellReuseIdentifier: "AddOnTableViewCell2")
yCoordinateForNewContent += contentSize + 20
tableView2.reloadData()
case 3:
let label = UILabel(frame: CGRect(x: 16, y: yCoordinateForNewContent, width: scrollView.frame.width - 32, height: 50))
label.text = addOns[2].title
yCoordinateForNewContent += 50
scrollView.addSubview(label)
contentSize = CGFloat(addOnContents3.count)
print("content size: \(contentSize)")
contentSize = contentSize * cellSize
tableView3 = UITableView(frame: CGRect(x: 0, y: yCoordinateForNewContent, width: scrollView.frame.width, height: contentSize))
scrollView.addSubview(tableView3)
tableView3.delegate = self
tableView3.dataSource = self
self.tableView3.register(AddOnTableViewCell.self, forCellReuseIdentifier: "AddOnTableViewCell3")
yCoordinateForNewContent += contentSize + 20
tableView3.reloadData()
case 4:
let label = UILabel(frame: CGRect(x: 16, y: yCoordinateForNewContent, width: scrollView.frame.width - 32, height: 50))
label.text = addOns[3].title
yCoordinateForNewContent += 50
scrollView.addSubview(label)
contentSize = CGFloat(addOnContents4.count)
print("content size: \(contentSize)")
contentSize = contentSize * cellSize
tableView4 = UITableView(frame: CGRect(x: 0, y: yCoordinateForNewContent, width: scrollView.frame.width, height: contentSize))
scrollView.addSubview(tableView4)
tableView4.delegate = self
tableView4.dataSource = self
self.tableView4.register(AddOnTableViewCell.self, forCellReuseIdentifier: "AddOnTableViewCell4")
yCoordinateForNewContent += contentSize + 20
tableView4.reloadData()
default:
print("too many tables")
}
count += 1
}
}
And here's the screen with example data:
SimulatorScreen
Any help is greatly appreciated!
Thanks #Naresh for bringing my attention to this. I had declared the label in the AddOnTableViewCell class, but I was creating a label in the cellForRowAt: method, which I was adding as a subview to the cell. I simplified the tables to a single table with an array of arrays to hold the data. I made the following change to my AddOnTableViewCell class:
class AddOnTableViewCell: UITableViewCell {
var titleLabel: UILabel!
init(frame: CGRect, title:String) {
super.init(style: .default, reuseIdentifier: "AddOnTableViewCell")
titleLabel = UILabel(frame: CGRect(x: 16 , y: self.frame.minY + 10, width: self.frame.width - 32, height: 40))
titleLabel.textColor = UIColor.black
titleLabel.text = title
addSubview(titleLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
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
}}
and I simplified my cellForRowAt: method:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let title = tableContents[indexPath.section][indexPath.row]
let cell = AddOnTableViewCell(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: cellSize), title: title)
return cell
}
Thanks!

Xcode (Swift 3.0) - How to retain values when switching between controllers in Side bar menu

I have a login screen and upon successful login, I'm passing the object details through a navigation controller to side bar menu controller (topView controller). In side bar menu, I have two options and upon switching from other view controller to top view controller, the values are removed(As per my understanding, I'm passing values from loginVC and it may not be holding those values).
As of now side bar menu transistion is working perfectly fine. But when I switch back from AnotherVC to HomeVC , it is not holding the values which are passed from LoginVC.
Can someone help me to solve this.
Below are my code snippets
StoryBoard:
Code Snippets:
On login button click: (LOGIN VC)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "loginSuccessIdentifier" {
if let navController = segue.destination as? UINavigationController {
if let destinController = navController.topViewController as? HomeViewController {
destinController.loggedInUser = sender as! UserDetails!
}
}
}
}
HOME VC:
var loggedInUser:UserDetails! //UserDetails class is defined separately. Contains variables like id,firstname, lastname etc. I'm displaying those values on HomeVC
override func viewDidLoad() {
super.viewDidLoad()
addSlideMenuButton()
tokenLbl.adjustsFontSizeToFitWidth = true
nameLbl.adjustsFontSizeToFitWidth = true
idLbl.adjustsFontSizeToFitWidth = true
if(loggedInUser != nil)
{
firstLbl.text = loggedInUser.token
secondLbl.text = loggedInUser.lastname
idLbl.text = String(loggedInUser.agentId)
}
}
SIDEBARMENU VC:
protocol SlideMenuDelegate {
func slideMenuItemSelectedAtIndex(_ index : Int32)
}
class MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet var tblMenuOptions : UITableView!
#IBOutlet var btnCloseMenuOverlay : UIButton!
var arrayMenuOptions = [Dictionary<String,String>]()
var btnMenu : UIButton!
var delegate : SlideMenuDelegate?
override func viewDidLoad() {
super.viewDidLoad()
tblMenuOptions.tableFooterView = UIView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateArrayMenuOptions()
}
func updateArrayMenuOptions(){
arrayMenuOptions.append(["title":"HOME VC", "icon":"Icon1"])
arrayMenuOptions.append(["title":"ANOTHER VC", "icon":"Icon2"])
tblMenuOptions.reloadData()
}
#IBAction func onCloseMenuClick(_ button:UIButton!){
btnMenu.tag = 0
if (self.delegate != nil) {
var index = Int32(button.tag)
if(button == self.btnCloseMenuOverlay){
index = -1
}
delegate?.slideMenuItemSelectedAtIndex(index)
}
UIView.animate(withDuration: 0.3, animations: { () -> Void in
self.view.frame = CGRect(x: -UIScreen.main.bounds.size.width, y: 0, width: UIScreen.main.bounds.size.width,height: UIScreen.main.bounds.size.height)
self.view.layoutIfNeeded()
self.view.backgroundColor = UIColor.clear
}, completion: { (finished) -> Void in
self.view.removeFromSuperview()
self.removeFromParentViewController()
})
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellMenu")!
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.layoutMargins = UIEdgeInsets.zero
cell.preservesSuperviewLayoutMargins = false
cell.backgroundColor = UIColor.clear
let lblTitle : UILabel = cell.contentView.viewWithTag(101) as! UILabel
let imgIcon : UIImageView = cell.contentView.viewWithTag(100) as! UIImageView
imgIcon.image = UIImage(named: arrayMenuOptions[indexPath.row]["icon"]!)
lblTitle.text = arrayMenuOptions[indexPath.row]["title"]!
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let btn = UIButton(type: UIButtonType.custom)
btn.tag = indexPath.row
self.onCloseMenuClick(btn)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayMenuOptions.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
}
BASE VC: (FOR SLIDE DELEGATE)
class BaseViewController: UIViewController, SlideMenuDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func slideMenuItemSelectedAtIndex(_ index: Int32) {
let topViewController : UIViewController = self.navigationController!.topViewController!
print("View Controller is : \(topViewController) \n", terminator: "")
switch(index){
case 0:
print("HomeVC\n", terminator: "")
self.openViewControllerBasedOnIdentifier("HomeVC")
break
case 1:
print("AnotherVC\n", terminator: "")
self.openViewControllerBasedOnIdentifier("AnotherVC")
break
default:
print("default\n", terminator: "")
}
}
func openViewControllerBasedOnIdentifier(_ strIdentifier:String){
let destViewController : UIViewController = self.storyboard!.instantiateViewController(withIdentifier: strIdentifier)
let topViewController : UIViewController = self.navigationController!.topViewController!
if (topViewController.restorationIdentifier! == destViewController.restorationIdentifier!){
print("Same VC")
} else {
self.navigationController!.pushViewController(destViewController, animated: true)
}
}
//to add slide option button
func addSlideMenuButton(){
let btnShowMenu = UIButton(type: UIButtonType.system)
btnShowMenu.setImage(self.defaultMenuImage(), for: UIControlState())
btnShowMenu.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
btnShowMenu.addTarget(self, action: #selector(BaseViewController.onSlideMenuButtonPressed(_:)), for: UIControlEvents.touchUpInside)
let customBarItem = UIBarButtonItem(customView: btnShowMenu)
self.navigationItem.leftBarButtonItem = customBarItem;
}
func defaultMenuImage() -> UIImage {
var defaultMenuImage = UIImage()
UIGraphicsBeginImageContextWithOptions(CGSize(width: 30, height: 22), false, 0.0)
UIColor.black.setFill()
UIBezierPath(rect: CGRect(x: 0, y: 3, width: 30, height: 1)).fill()
UIBezierPath(rect: CGRect(x: 0, y: 10, width: 30, height: 1)).fill()
UIBezierPath(rect: CGRect(x: 0, y: 17, width: 30, height: 1)).fill()
UIColor.white.setFill()
UIBezierPath(rect: CGRect(x: 0, y: 4, width: 30, height: 1)).fill()
UIBezierPath(rect: CGRect(x: 0, y: 11, width: 30, height: 1)).fill()
UIBezierPath(rect: CGRect(x: 0, y: 18, width: 30, height: 1)).fill()
defaultMenuImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return defaultMenuImage;
}
func onSlideMenuButtonPressed(_ sender : UIButton){
if (sender.tag == 10)
{
// To Hide Menu If it already there
self.slideMenuItemSelectedAtIndex(-1);
sender.tag = 0;
let viewMenuBack : UIView = view.subviews.last!
UIView.animate(withDuration: 0.3, animations: { () -> Void in
var frameMenu : CGRect = viewMenuBack.frame
frameMenu.origin.x = -1 * UIScreen.main.bounds.size.width
viewMenuBack.frame = frameMenu
viewMenuBack.layoutIfNeeded()
viewMenuBack.backgroundColor = UIColor.clear
}, completion: { (finished) -> Void in
viewMenuBack.removeFromSuperview()
})
return
}
sender.isEnabled = false
sender.tag = 10
let menuVC : MenuViewController = self.storyboard!.instantiateViewController(withIdentifier: "MenuViewController") as! MenuViewController
menuVC.btnMenu = sender
menuVC.delegate = self
self.view.addSubview(menuVC.view)
self.addChildViewController(menuVC)
menuVC.view.layoutIfNeeded()
menuVC.view.frame=CGRect(x: 0 - UIScreen.main.bounds.size.width, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height);
UIView.animate(withDuration: 0.3, animations: { () -> Void in
menuVC.view.frame=CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height);
sender.isEnabled = true
}, completion:nil)
}
}
For innocuous data that you only want to hold during a single session, you can store it as an instance property in your App Delegate. To read/write you would just create a new reference to the AppDelegate
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.yourProperty = "saveStuff"
If you want the data to persist, and it is not a large amount of data, try NSUserDefaults. You just have to make sure your objects conform to the NSCoder protocol.
https://developer.apple.com/reference/foundation/userdefaults
If you want the data to persist, and it is not a large amount of data, and the data should be secure, you should use the keychain services.
https://developer.apple.com/reference/security/1658642-keychain_services

Table View Displays Below Screen

My issue is that the last cell in my TableView is below the screen view and to see it you must scroll up and hold your position. At a neutral position where you dont scroll up, you cant see the last cell. Everything seemed fine until i changed the size of the cells. Here is my code:
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
//MARK : Properties
var tableView = UITableView()
var items: [String] = ["Age", "Gender", "Smoking Hx", "Occup. -Ag", "Family Hx", "Chronic Lung Disease Radiology", "Chronic Lung Disease Hx", "Nodule Border", "Nodule Location", "Satellite Lesions", "Nodule Pattern Cavity", "Nodule Size"]
var navigationBar = NavigationBar()
var gender = GenderView()
override func viewDidLoad() {
super.viewDidLoad()
//Create TableView
tableView.frame = CGRectMake(0, self.view.bounds.height * 0.097, self.view.bounds.width, self.view.bounds.height);
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(tableView)
//Create Navigation Bar with custom class
self.navigationBar = NavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height * 0.097))
self.view.addSubview(navigationBar)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
//Cell wont turn grey when selected
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return self.view.bounds.height * 0.095
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
}
}
The only thing i could think of causing this issue is that instead of me creating a navigation bar, i created a "navigationBar" using a custom UIView() class. I then start the table view at the bottom of the navigation bar. Any idea how to fix this?
Here is the NavigationBar.swift:
class NavigationBar: UIView {
var navigationBar = UIView()
var header = UILabel()
var lineBorder = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = frame
setUpView()
}
func setUpView(){
//Create Navigation Bar
navigationBar.frame = CGRectMake(0, 0, self.bounds.width, self.bounds.height)
navigationBar.backgroundColor = UIColor.whiteColor()
self.addSubview(navigationBar)
//Create Line Border
lineBorder.frame = CGRectMake(0, self.bounds.height, self.bounds.width, self.bounds.height * 0.005)
lineBorder.backgroundColor = UIColor.grayColor()
self.addSubview(lineBorder)
header.frame = CGRectMake(0, 0, 200, 200)
header.font = UIFont(name: "Helvetica", size: 17)
header.text = "Nodule Risk Calculator"
//header.backgroundColor = UIColor.blackColor()
self.addSubview(header)
header.translatesAutoresizingMaskIntoConstraints = false
header.centerHorizontallyTo(navigationBar, padding: 0)
header.centerVerticallyTo(navigationBar, padding: 9)
}
func hide(){
self.removeFromSuperview()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
tableView.frame = CGRectMake(0, self.view.bounds.height * 0.097, self.view.bounds.width, self.view.bounds.height);
has problem. The origin.y is not 0, and you still feed the whole height. So the table view will have some area below the screen
Try this:
tableView.frame = CGRectMake(0, self.view.bounds.height * 0.097, self.view.bounds.width,
self.view.bounds.height - self.view.bounds.height * 0.097);
self.navigationBar = NavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height * 0.097))
self.view.addSubview(navigationBar)
var yAxis : Float = self.navigationBar.frame.height
var tblHeight : Float = self.view.bounds.height - yAxis
tableView.frame = CGRectMake(0, yAxis, self.view.bounds.width, tblHeight)
self.view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

Swift - tableViewCell issue when scrolling

I'm creating a chat app using swift. On my storyboard, I have one View Controller that is responsible for showing the messages. I have 3 swift files :
ChatingViewController : Class associated with the View Controller on the storyboard
CustomChatingTableViewController
CellChatingTableViewCell
Each message is displayed in a cell. I create the tableView programmatically.
- ChatingViewController
import UIKit
class ChatingViewController: UIViewController {
var messageController = [[String:String]]()
var tableViewController = CustomChatingTableViewController()
override func viewDidLoad() {
let sizeTableView = CGSize(width: view.frame.size.width - 2 * margin, height: view.frame.size.height - sizeTextField.height - 2 * margin - self.navigationController!.navigationBar.frame.size.height)
let originTableView = CGPoint(x: margin, y: 2 * margin + self.navigationController!.navigationBar.frame.size.height)
tableViewController.tableView.frame = CGRect(origin: originTableView, size: sizeTableView)
tableViewController.tableView.registerClass(CellChatingTableViewCell.self, forCellReuseIdentifier: "Cell")
tableViewController.data = messageController
tableViewController.tableView.separatorStyle = .None
view.addSubview(tableViewController.tableView)
}
- CustomChatingTableViewController
import UIKit
class CustomChatingTableViewController: UITableViewController {
var data:[[String:String]]!
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as CellChatingTableViewCell
cell.setCell(data[indexPath.row]["name"] as String!, date: data[indexPath.row]["date"] as String!, message: data[indexPath.row]["message"] as String!)
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 150
}
}
** -CellChatingTableViewCell**
import UIKit
class CellChatingTableViewCell: UITableViewCell {
var date = UILabel()
var message = UILabel()
func setCell(name:String,date:String,message:String){
let imageContainerMessage = UIImage(named: "orange.png")!.stretchableImageWithLeftCapWidth(24, topCapHeight: 15)
self.date.font = UIFont(name: "Arial", size: 10)
self.date.text = date
self.date.numberOfLines = 0
self.date.lineBreakMode = NSLineBreakMode.ByWordWrapping
let sizeDateLabelMax = CGSizeMake(self.frame.size.width, 9999)
let expectedSizeDate = self.date.sizeThatFits(sizeDateLabelMax)
self.date.frame = CGRect(origin: CGPoint.zeroPoint, size: expectedSizeDate)
self.message.font = UIFont(name: "Arial", size: 15)
self.message.text = message
self.message.numberOfLines = 0
self.message.lineBreakMode = NSLineBreakMode.ByWordWrapping
let sizeMessageLabelMax = CGSizeMake(self.frame.size.width, 9999)
let expectedSizeMessage = self.message.sizeThatFits(sizeMessageLabelMax)
self.message.frame = CGRect(origin: CGPoint(x: 15, y: 10), size: expectedSizeMessage)
var imageContainer = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: expectedSizeDate.height + 5), size:
CGSizeMake(expectedSizeMessage.width + 25, expectedSizeMessage.height + 25)))
imageContainer.image = imageContainerMessage
self.addSubview(self.date)
self.addSubview(imageContainer)
imageContainer.addSubview(self.message)
}
}
When I load the ViewController, everything does work fine but when I scroll the tableView, it turns horrible:
Before scrolling :
After scrolling :
Any suggestion?
Thanks in advance
In your cellForRowAtIndexPath, you are reusing cells. But then in your custom UITableViewCell, you are adding (self.addSubView) self.date and self.message multiple times (yikes), and adding new instances of imageContainer.
You should either clear the cells before re-adding them, or invalidate them with new data but do not do self.addSubView again.

Resources