Table View Cells on second View - ios

I have a TableView with cells on my first View, and it works well. But I want to click on some cell then should appear new View with new Table View & cells. But after click, I can see only TableView without any cells.
code of SecondViewController
import UIKit
import WebKit
import Kanna
import Gloss
class SecondViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
#IBOutlet weak var menuTable: SecondViewControllerTableViewCell!
public func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return 3
}
public func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"lte_secondPage", for: indexPath) as! ViewControllerTableViewCell
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
code of FirstViewController
class ViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
let logos = ["ks", "vf", "ls"]
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return logos.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"lte_firstPage", for: indexPath) as! ViewControllerTableViewCell
#code#
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
IndexPath) {
performSegue(withIdentifier: "segue", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
}
Code of First Table Cell Controller
import UIKit
class ViewControllerTableViewCell: UITableViewCell {
#IBOutlet weak var logoImage: UIImageView!
#IBOutlet weak var regionQuantity: UILabel!
#IBOutlet weak var localityQuantity: UILabel!
#IBOutlet weak var BSQuantity: UILabel!
#IBOutlet weak var updateDate: UILabel!
#IBOutlet weak var namesOfBS: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Code of Second Table Cell Controller
import UIKit
class SecondViewControllerTableViewCell: UITableViewCell {
#IBOutlet weak var label: UILabel!
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 when I tried to print something in
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { print(3) return 3 } it printed nothing and
2019-01-04 22:54:07.057626+0200 BaseStation[12620:273434] <UIView: 0x7f9f52c1e640; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x600001a62a80>>'s window is not equal to <BaseStation.SecondViewController: 0x7f9f52c239e0>'s view's window!
in debuger apeared after click onto cells in first View

I think what you are asking is .. When you click on a cell in your tableview, you navigate to a new view controller. And this new view controller has a tableview that is not being populated with any cells.
Essentially your question is.. Why is your tableview empty.
1) Have you checked if you correctly set the table views datasource?
2) Is the datasource empty?
3) Does your tableview have a visible height/width?

Related

ScrollView delegate not called inside custom tableviewCell class

I'm using UITableViewCell custom class
Using Scrollview for horizontal scroll inside the tableviewcell
Write Tableview delegates in UIViewcontroller
Writing scrollview delegate in UITableview cell class
Delegate method not called.
My Code:
class ViewController: UIViewController {
#IBOutlet weak var matchesTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
matchesTableView.delegate = self
matchesTableView.dataSource = self
tableViewSetup()
}
func tableViewSetup() {
matchesTableView.rowHeight = UITableView.automaticDimension
matchesTableView.estimatedRowHeight = self.view.bounds.height
matchesTableView.tableFooterView = UIView()
matchesTableView.separatorStyle = UITableViewCell.SeparatorStyle.none
}
}
// MARK: UITableviewdelegate
extension ViewController : UITableViewDelegate, UITableViewDataSource {
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: "TableViewCell", for: indexPath) as! TableViewCell
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.view.bounds.height
}
}
UITableView Cell:
import UIKit
class TableViewCell: UITableViewCell, UIScrollViewDelegate {
#IBOutlet weak var bannerScrollView: UIScrollView!
#IBOutlet weak var matchesPageControl: UIPageControl!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code+
self.bannerScrollView.delegate = self
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
matchesPageControl.currentPage = Int(pageNumber)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}

How to load data into table view

How do I load data into my UITableView? Can anyone please kindly help me with this issue:
why not load to cellForRowAt
pPlandDetailId ==>Optional(1)
Driver ==> Driver Name:Boonma,Pongpat
carRegistrtation ==> Car Registrtation:60-7620
StoreLocation ==> ["S.Surathani"]
pDepartureDateTime ==> ["18/01/2019 20:00"]
import UIKit
class StoreListMainViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{
// TableView
#IBOutlet weak var tableView: UITableView!
var StoreLocation: [String] = []
var getDepartureTime: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
createTableiew()
}//Main Method
// TableView
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return StoreLocation.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? cellTableViewCell
cell!.Location.text = StoreLocation[indexPath.row]
cell!.DepartureTime.text = getDepartureTime[indexPath.row]
print("cell!.Location.text ==>\(String(describing: cell!.Location.text) )")
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "gotoTempMonitorStoreList") as? StoreListTempMonitorViewController
self.navigationController?.pushViewController(vc!, animated: true)
}
// Create UI Table view
func createTableiew() -> Void {
tableView.delegate = self
tableView.dataSource = self
}//createTableiew
import UIKit
class cellTableViewCell: UITableViewCell {
#IBOutlet weak var Location: UILabel!
#IBOutlet weak var DepartureTime: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
reload tableview tableView.reload() where you have received data.
I suggest you to first fetch the data and later try to reload your table view.
Once you set the array to table values automatically they will get displayed in table provided you code object's properties to cell's Outlet values.
Since you are not using a UITableViewController you need to set a delegate and a dataSource to your tableView object.
Looks something like this:
override func viewDidLoad() {
tableView.dataSource = self
tableView.delegate = self
}
That's it. Enjoy

TableView is not displayed how it's supposed to

I created a table view in Xcode and when I run the project it's displayed awfully (pic related).
Don't know what could be the problem.
What I did was:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let fruit = ["Apple", "Prune", "Grapes", "Watermelon", "Melon", "Cherry"]
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fruit.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! FruitTableViewCell
cell.fruitLable.text = fruit[indexPath.row]
cell.fruitImage.image = UIImage(named: fruit[indexPath.row])
return cell
}
}
import UIKit
class FruitTableViewCell: UITableViewCell {
#IBOutlet weak var fruitView: UIView!
#IBOutlet weak var fruitImage: UIImageView!
#IBOutlet weak var fruitLable: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
You need to implement heightForRowAt
func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}

Swift , Identing Table Views each row gets more indented

What it looks like
I am trying to make it so each name of the characters indents more and more with each row so from left then the next row goes a little more to the right and then the next row goes a little more to the right.
I simply can not figure out why I can't indent my labels one by one. Any help is appreciated.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var Dwarves = ["Sleepy", "Sneezy", "Bashful", "Dopy", "Grumpy", "Doc", "Happy", "Sad"]
var imagess = ["Sleepy", "Sneezy", "Bashful", "Dopy", "Grumpy", "Doc", "Happy", "Sad"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Dwarves.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! CustomTableViewCell
cell.CellView.layer.cornerRadius = cell.CellView.frame.height / 2
cell.Label.text = Dwarves[indexPath.row]
cell.CharcterImage.image = UIImage(named:
imagess[indexPath.row])
cell.CharcterImage.layer.cornerRadius = cell.CharcterImage.frame.height / 2
return cell
}
func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath.) -> Int {
}
}
I tried to use the indentationLevelforRowAt but it doesn't seem to work.
import UIKit
class CustomTableViewCell: UITableViewCell {
#IBOutlet weak var CellView: UIView!
#IBOutlet weak var CharcterImage: UIImageView!
#IBOutlet weak var Label:UILabel!
#IBOutlet weak var TableView:UITableView!
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
}
}
A primitive way to do it would be to prefix the string with spaces. Here I use an indentation level of two spaces.
cell.Label.text = "\(String(repeating: " " , count: indexPath.row*2))\(Dwarves[indexPath.row])")
indentationLevelforRowAt allows you to display the table rows in a hierarchal / tree-view type format. But you want to indent just a part of your cell. Therefore I think you should play with your constraints.
One of possible solutions is create a leading constraint for label and in cellForRowAt method write something like this:
cell.labelLeadingConstraint.constant = 5 * indexPath.row

Black screen when tab bar is selected

I am having a problem with a black screen when i switch to the "friends" tab on my application. This does happen because of my friendsviewcontroller. I know this because I removed the link to the viewcontroller and it presented me with the normal screen. Hope someone can see what I have done wrong
FriendsViewController:
import UIKit
import FBSDKLoginKit
class FriendsViewController: UITabBarController, UITableViewDelegate,UITableViewDataSource{
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var friendTypeSwitch: UISegmentedControl!
#IBOutlet weak var friendSearchBar: UITextField!
var user:User = User()
override func viewDidLoad() {
super.viewDidLoad()
print("Friends tab")
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
print("view did appear")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 30
}
//What to do with tableview
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "friendCell", for: indexPath) as! friendsCustomCell
user.username = "kulgut123"
cell.friendName.text = user.username
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
}
class friendsCustomCell: UITableViewCell{
#IBOutlet weak var friendImg: UIImageView!
#IBOutlet weak var friendName: UILabel!
}
As ronatory suggests, you should subclass FriendsViewController from UIViewController instead of UITabBarController.
class FriendsViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
//type your code here
}

Resources