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
}
Related
I have two custom TableViewCells. So first TableViewCell is like a recent list, it can become longer. but second cell is always stays at bottom. so i need to add UILabel that hold's secondTableViewCell. the result that i need.
import UIKit
class bagPage: UIViewController, UITableViewDelegate, UITableViewDataSource {
var itemsName: [String] = []
var itemsPhoto: [UIImage] = []
// these arrays will defined in other view controller
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row < itemsName.count{
return 165
}else{
return 50
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6 + itemsName.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < itemsName.count{
let cell = tableView.dequeueReusableCell(withIdentifier: "bagTableViewCell") as? bagTableViewCell
cell?.itemName.text = itemsName.last
cell?.itemPhoto.image = itemsPhoto.last
return cell!
}
if indexPath.row == itemsName.count {
let cellTwo = tableView.dequeueReusableCell(withIdentifier: "extraBagPageTableView") as? extraBagPageTableView
// here i'm hiding views in first row
cellTwo?.textLabel?.text = "These are products that u can add to your cell"
return cellTwo!
}else{
let cellTwo = tableView.dequeueReusableCell(withIdentifier: "extraBagPageTableView") as? extraBagPageTableView
return cellTwo!
}
}
}
This is another attempt at providing a solution. Is this what you are looking for?
Here is a link to the project
https://drive.google.com/file/d/1XlSF4fGiNzOqQHauiNo7TuFhKUTrFbsJ/view?usp=sharing
Let me know if you need more help
Good evening, so I followed your image and made the code follow your convention of 1 tableview section, 2 difference cells. I tried to use your naming convention. Note I had to swap around the height values, you had them wrong way round.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var itemsName: [String] = ["Helmet", "Gloves", "Bindings", "Goggles", "Kneepads", "Boots", "Snowboard"]
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 + itemsName.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < itemsName.count{
let bagTableViewCell = tableView.dequeueReusableCell(withIdentifier: "BagTableViewCell") as? BagTableViewCell
bagTableViewCell?.productsLabel.text = itemsName[indexPath.row]
return bagTableViewCell!
} else {
let extraBagPageTableView = tableView.dequeueReusableCell(withIdentifier: "ExtraBagPageTableView") as? ExtraBagPageTableView
return extraBagPageTableView!
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row < itemsName.count {
return 50
} else {
return 165
}
}
}
And the tableview cells. Please note the second cell has a bad name.
class BagTableViewCell: UITableViewCell {
#IBOutlet weak var additionalProductsLabel: UILabel!
#IBOutlet weak var productsLabel: UILabel!
#IBOutlet weak var productImage: UIImageView!
}
class ExtraBagPageTableView: UITableViewCell {
}
And altogether it looks like this
And the project can be found here. (I set the permissions correctly this time :) )
TableCellWithLabel Project
There are some rows in table view. Each row opens a new view. Now I want to show some sub categories on click of Particular row. each of this sub category opens different view. I am using only 1 section. Any ideas?
Simply do this.
class CategoriesListViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
var filterCategories = [CategoryViewModel]()
override func viewDidLoad() {
super.viewDidLoad()
tfSearch.placeholder = LocalizeKey.search.localized
getCategories()
// Do any additional setup after loading the view.
}
extension CategoriesListViewController : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filterCategories.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CategoryCellTableView
let category = filterCategories[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let controller : SubCategoriesListViewController = ViewControllerManager.shared.find() {
controller.selectedCategory = self.filterCategories[indexPath.row]
self.pushViewController(controller : controller)
}
}
Sub-Categories will be subclassed from the main categories and override the table view selection methods and do whatever you want to do.
class SubCategoriesListViewController: CategoriesListViewController {
#IBOutlet weak var tableView: UITableView!
var filterCategories = [CategoryViewModel]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let controller = DifferentViewController()
self.pushViewController(controller : controller)
}
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
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?
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
}