I am trying to use a UITableView by implementing the code I saw on many tutorials but it doesn't work correctly when I run it. What am I doing wrong? I added a cell to the view and added a corresponding identifier. My storyboard look like that
and code is the following:
import UIKit
import Firebase
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var ref: DatabaseReference?
var data = ["m","2","3"]
#IBOutlet weak var tableV: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableV.delegate = self
tableV.dataSource = self
// 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.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableV.dequeueReusableCell(withIdentifier: "PostCell")
cell?.textLabel?.text = data[indexPath.row]
return cell!
}
}
And
what it looks like.
Please help me to solve this problem.
You should use this way to correct errors.
change this:
let cell = tableV.dequeueReusableCell(withIdentifier: "PostCell")
with:
let cell = tableV.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath)
and you need to check cell identifier.
I have a tableview and stepper.
stepper is touched, random stepper values is changing when tableview is moved.how do i prevent it. Thanks
Code
UIViewController
import UIKit
class ViewController:UIViewController,UITableViewDelegate,UITableViewDataSource{
#IBOutlet weak var tablee: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "test", for: indexPath) as! TesttViewCell
return cell
}
}
TesttViewCell
import UIKit
class TesttViewCell: UITableViewCell {
#IBOutlet weak var adet: UIStepper!
#IBOutlet weak var lblTest: 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
}
#IBAction func changee(_ sender: UIStepper) {
lblTest.text = String(sender.value)
}
}
import UIKit
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
#IBOutlet var search: UISearchBar!
#IBOutlet var Tableview: UIView!
var items = ["hello"]
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.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell: Tableview.deqeueReusableCellWithIdentifier;("cell")
//Error = Undeclared type 'Tableview'
cell.textLabel?.text = self.items[indexPath.row]
return cell;
}
}
Replace the colon (:) with the equal sign and you'll be ok:
var cell = Tableview.deqeueReusableCellWithIdentifier("cell")
On another note, even though Xcode won't give you an error about it, you shouldn't begin your variable with a capital. The convention is that starting capitals should be to indicate class names.
have problem with 2 tableviews on 2 viewcontrollers and detailviewcontroller, everything is almost working ok, 2 tableviews working like a charm, but problem is in secondviewcontroller where i put array of images to segue to detailVC after each meal/food is clicked, i got same 3 images for mexican food, spanish food, italian, croatian and french. Images(15) does to correspond to 15 meals. Did i go wrong with putting array of images in second view controller? Thank you
ViewController
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
let textForFirstTableView = ["Italian food", "Mexican food", "Croatian food", "Spanish food", "French food"]
let namesOfFood = [["Bolognese", "Milagnese","Pizza"],
["Tortilla", "Chimichanga", "Paella"],
["Burek od mesa","Grah", "Janjetina"],
["Tapas", "Churros", "Flan"],
["Buche de Noel", "Cherry Cake", "Onion Soup"]]
var ObjectNamesOfFood = [String]()
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return textForFirstTableView.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
cell.textLabel?.text = textForFirstTableView[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]
self.performSegueWithIdentifier("Segue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let driver = segue.destinationViewController as! DrugiViewController
var whatToPass = self.ObjectNamesOfFood
driver.arrayToPass = whatToPass
}
}
SecondViewController
import UIKit
class DrugiViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var arrayToPass:[String] = [String]()
var picturesForEachMeal = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"]
var objectpicturesForEachMeal:String!
#IBOutlet weak var tableView2: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tableView2.delegate = self
self.tableView2.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayToPass.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell2") as! UITableViewCell
cell.textLabel?.text = arrayToPass[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.objectpicturesForEachMeal = self.picturesForEachMeal[indexPath.row]
self.performSegueWithIdentifier("toDetailViewController", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let driver = segue.destinationViewController as! DetailViewController
driver.picturesToRetrieve = objectpicturesForEachMeal
}
}
DetailViewController (displaying 15 images for 15 meals)
import UIKit
class DetailViewController: UIViewController {
#IBOutlet weak var backgroundImage: UIImageView!
#IBOutlet weak var slika: UIImageView!
var picturesToRetrieve:String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var connection = self.picturesToRetrieve
self.slika.image = UIImage(named: connection)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
In View Controller store the selected index and pass it to DrugiViewController
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]
self.selectedKitchen = indexPath.row
self.performSegueWithIdentifier("Segue", sender: self)
}
In DrugiViewController calculate each food image index
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var totalIndex = (indexPath.row) + (selectedKitchen * 3)
self.objectpicturesForEachMeal = self.picturesForEachMeal[totalIndex]
self.performSegueWithIdentifier("toDetailViewController", sender: self)
}
that's it
I must say this is not the best approach if you was starting an app from begining you should in your case use an dictionary of array that contain a key country and as result an array of dictionary dish and photo in the first view controller.
In the second view controller you should have an array of dictionary that contain the dish and photo.
In the third view just a simple dictionary that contain a dictionary with dish and photo.
But how you almost finish the easier solution for you is to pass the value from the first table to the second, so lets say if the user selected row 2 you pass and keep that values.
Now in the second table if the user selected row 3 you multiply the number of the row the user selected (3) by the number of the first row select in table view one (2) and you know you have to display the picture number 6 in your final array.
I hope this helps you, I am sure now that you know the idea you will be able to finish your project. Best of luck!
First view controller:
var rowSelected:Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]
self.rowSelected = indexPath.row
self.performSegueWithIdentifier("Segue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let driver = segue.destinationViewController as! DrugiViewController
var whatToPass = self.ObjectNamesOfFood
driver.arrayToPass = whatToPass
driver.rowSelected = rowSelected
}
Second view controller:
var rowSelected:Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.objectpicturesForEachMeal = self.picturesForEachMeal[indexPath.row * self.rowSelected]
self.performSegueWithIdentifier("toDetailViewController", sender: self)
}
This is the basic idea, sorry I couldn't test it as I am in a Windows computer now, but should just work
So I have a problem where I get shown the error
multiple inheritance from classes UIViewController and UIFont
and being new to programming, I don't really understand what is wrong. So what does this error mean? Can't I add more protocols to the class? This is how my code looks
import UIKit
import Foundation
var items:[String] = []
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIFont {
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.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel?.text = items[indexPath.row]
return cell
}
func fontWithSize(fontSize: 130) -> UIFont {
return fontSize
}
override func viewWillAppear(animated: Bool) {
if var storeditems: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("items") {
items = []
for var i = 0; i<storeditems?.count; ++i {
items.append(storeditems?[i] as NSString)
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
items.removeAtIndex(indexPath.row)
NSUserDefaults.standardUserDefaults().setObject(items, forKey: "items")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
}
}
}
View Controller 2
import UIKit
class ViewController2: UIViewController, UITextFieldDelegate {
#IBOutlet weak var textField: UITextField!
#IBAction func button(sender: AnyObject) {
items.append(textField.text)
NSUserDefaults.standardUserDefaults().setObject(items, forKey: "items")
NSUserDefaults.standardUserDefaults().synchronize()
}
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.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Swift and Objective-C only support single inheritance. They do support multiple protocols. Extension is principally achieved via composition.
UITableViewDelegate and UITableViewDataSource are protocols.
UIFont is not a protocol, it is a class.