Swift UITableView subview appears but controller never called - ios

I'm building a simple swift iOS app with a TableView that only takes up part of the screen. I have a FirstViewController that handles a long of info on the page and is the default landing page (it works as expected). A section of the page is a table view which I have created a new controller for BucketViewController.swift. That file looks like:
import UIKit
class BucketViewController: UITableViewController {
var buckets = [String]()
var newBucket: String = ""
override func viewDidLoad() {
print("INSIDE BUCKET")
super.viewDidLoad()
buckets = ["A", "B", "C"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return buckets.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print("TABLEVIEW")
let cell = tableView.dequeueReusableCellWithIdentifier("bucketCell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = buckets[indexPath.row]
return cell
}
}
In the storyboard, there is a Table View with the class bucketViewController. There is a prototype cell called bucketCell with a textLabel.
When I run the app the tableView appears, but it is blank. INSIDE BUCKET is never printed which makes me think the controller is never called. Any idea why this is?
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Related

Swift custom UITableView not showing up in build

I'm having a problem in which my custom UITableView will not show up when I build my application.
Currently, I've built what the custom table should look like in my storyboard and also have created a cocoa class that is linked to the UITableViewCell.
This is the ViewController code that deals with retrieving the input form an array and then pasting it onto the table
import UIKit
struct eventStruct
{
let eventHost : String
let eventStatus : String
let eventPrice : String
}
class eventsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var eventArrayData = [eventStruct]()
override func viewDidLoad() {
super.viewDidLoad()
eventArrayData = [eventStruct(eventHost: "Event1", eventStatus: "OPEN", eventPrice: "$5"),
eventStruct(eventHost: "Event2", eventStatus: "CLOSED", eventPrice: "$0")]
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (eventArrayData.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let eventCell = tableView.dequeueReusableCell(withIdentifier: "eventTemplate", for: indexPath) as! eventsTableViewCell
eventCell.eventHost.text = eventArrayData[indexPath.row].eventHost
eventCell.eventPrice.text = eventArrayData[indexPath.row].eventPrice
eventCell.eventStatus.text = eventArrayData[indexPath.row].eventStatus
return (eventCell)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Any extra information that is needed and/or help would be really appreciated!
You have not connected delegate to your view controller
you can connect your delegate in two ways
1) Using viewDidLoad() function
override func viewDidLoad() {
super.viewDidLoad()
yourTableVeiw.delegate = self
yourTableView.dataSource = self
}
2) Using storyboard
just drag and drop on your view controller
follow below image

Reload UITableview after back

I have simple table on my View. Another view have array with some values. When I back, I want to reload table with new values, but dont work.
My class:
let textCellIdentifier = "cell"
var menu:[[String]] = [[]]
var buscaEmp:BuscadorEmpresa = BuscadorEmpresa()
#IBOutlet weak var tablaVista: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
recuperaEmpresas()
tablaVista.delegate = self
tablaVista.dataSource = self
}
func recuperaEmpresas(){
menu = buscaEmp.getEmpresas()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menu.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! CustomTableViewCell
let row = indexPath.row
cell.nombreEmp.text = menu[row][0]
return cell
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
recuperaEmpresas()
tablaVista.reloadData()
}
I use viewWillAppear to reload table before reopen the view.
in viewWillAppear, you have to call recuperaEmpresas() and after it, call tableView.reloadData().
With this, it will refill the menu datasource with fresh data from recuperaEmpresas() and reload the table view with the new datasource values.
Edit :
And be sure that recuperaEmpresas() refresh data with new data, if it's always the same, you won't see any changes...

Append array from another Controller

I want to append new element to array. I'm using container view. I call addItem function to tableviewcontroller. But I clicked button nothing happen.
My TableViewController
import UIKit
class TableViewController: UITableViewController {
var myArray = ["1"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addItem () {
myArray.append("asd")
tableView.reloadData()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return myArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)
// Configure the cell...
cell.textLabel?.text = myArray[indexPath.row]
return cell
}
}
My TableViewController
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func addButton(sender: AnyObject) {
TableViewController().addItem()
}
}
The line TableViewController().addItem() means create a brand new instance of TableViewController and then call addItem() on it. You need to find a reference to the specific instance of TableViewController you want to manipulate. How does that ViewController relate to the one calling addItem?
It should be something like this:
class ViewController: UIViewController {
let tableViewController = TableViewController()
..
#IBAction func addButton(sender: AnyObject) {
tableViewController.addItem()
}
}

second TableView Cell dissapear in my TabBarController

I got an TabBar bassed app (using Swift) and i got 2 table views in different ViewControllers, and just the table that is in the first View, but isnt in other views.
I check the display of the cells and is ok.
If i put another view (without a table as first view) the other views within table views dont appear also.
some images of my problem
http://s12.postimg.org/7c58vxfe5/Captura_de_pantalla_2015_10_03_a_las_17_25_01.png
http://s13.postimg.org/meysu5j0n/Captura_de_pantalla_2015_10_03_a_las_17_24_48.png
Thanks in advance !
Edit:
Some code
import UIKit
class HomeViewController: UIViewController , UITableViewDataSource , UITableViewDelegate , UITabBarControllerDelegate {
let CellIdentifier = "CellTarea";
#IBOutlet var tareasTable: UITableView!
let tareas = ["Revisar etiquetado leche Nido","Reponer yoghurt sin lactosa","Reponer Chocolates Sahne Nuss","Revisar etiquetado de Chamito","Reponer Nescafe Clasico 200 gr."]
override func viewDidLoad() {
super.viewDidLoad()
self.tareasTable.delegate=self
self.tareasTable.dataSource=self
//Tabbar Config
self.tabBarController?.delegate = self;
for item in (self.tabBarController?.tabBar.items as NSArray!){
(item as! UITabBarItem).image = (item as! UITabBarItem).image?.imageWithRenderingMode(.AlwaysOriginal)
(item as! UITabBarItem).selectedImage = (item as! UITabBarItem).selectedImage?.imageWithRenderingMode(.AlwaysOriginal)
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - TableView
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:CustomTareasCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath) as! CustomTareasCell
cell.tareaLabel.text = tareas[indexPath.row];
cell.indiceLabel.text = String(indexPath.row+1);
return cell
}
Other view controller with tableview
import UIKit
class ReportesViewController: UIViewController, UITableViewDelegate , UITableViewDataSource, UITabBarControllerDelegate {
let personas = ["Alexis Parra","Ernesto Jímenez","Paulina Torres","Juan Soto","Julio Gallardo"]
let fechas = ["24 de Septimbre","22 de Septimbre", "21 de Septimbre", "20 de Septimbre","18 de Septimbre"]
let textCellIdentifier = "CellReporte";
#IBOutlet var reportesTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.reportesTable.delegate=self;
self.reportesTable.dataSource=self;
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(animated: Bool) {
let topOffest = CGPointMake(0, -(self.reportesTable.contentInset.top))
self.reportesTable.contentOffset = topOffest
}
//MARK: - TableView
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let reportesCell:CustomReportesCell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! CustomReportesCell
print(fechas[indexPath.row])
reportesCell.fecha.text = fechas[indexPath.row];
reportesCell.autor.text = personas[indexPath.row];
return reportesCell
}

Display 2nd 'column' value of array in separate screen based on selected row in swift

I just started to learn swift and trying to make a simple app in which I have created a UITableView with terms
('Term 1' till 'Term 15') and want to display a detail description of the term
('Detail 1' till 'Detail 15') when selected in the table in a separate view.
Using the available online tutorials, I've created the code below but have difficulties to complete and am only able to display a fixed string.
Any suggestions how best to proceed are welcome:
import UIKit
class FirstTableViewController: UITableViewController{
let Datasource = [("Term 1","Detail 1"),("Term 2","Detail 2"),("Term 3","Detail 3"),("Term 4","Detail 4"),("Term 5","Detail 5"),("Term 6","Detail 6"),("Term 7","Detail 7"),("Term 8","Detail 8"),("Term 9","Detail 9"),("Term 10","Detail 10"),("Term 11","Detail 11"),("Term 12","Detail 12"),("Term 13","Detail 13"),("Term 14","Detail 14"),("Term 15","Detail 15")]
//MARK -> Table Structure
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.Datasource.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var Cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
var (Id, Detail) = Datasource[indexPath.row]
Cell.textLabel?.text = Id
return Cell
}
//MARK -> Link to view term details on seperate screen
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestinationViewController = segue.destinationViewController as! DetailedTableViewController
DestinationViewController.DetailedTerm = "DISPLAY 2ND COLUMN OF DATASOURCE ARREY"
}
//MARK -> Standard methods
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Resources