I am very very new to Swift programming and I am growing to dislike it very much. I am not grasping it aa easily as other languages.
I have a project I am working on and I cannot figure out what is wrong or why its isn’t working.
In one view, I have a table view that has a cell. I am using an array to store all the values that I want to be stored in the corresponding elements in the table view.
When the user clicks on an individual cell in the table view, it will bring them to another view displaying other elements of the movie (runtime, image, director and year).
I have a template that I am using to code this and I think I have done everything correctly, but when I run the app, nothing shows.
I just want the table cells to show on startup, when I run the app. I can even troubleshoot myself if I can just have the table cells show.
Since I am so new to this language and XCode, I am having trouble navigating the IDE to find my issues. On top of much I am already struggling with Swift.
I could really use the help, if possible!
Here is all the code I have done:
import UIKit
class ViewController: UIViewController,
UITableViewDelegate,
UITableViewDataSource {
let movieList = ["Step Brothers", "Pulp Fiction", "Ali", "Harry Potter"]
let yearList = ["2008", "1994", "2001", "2001"]
let images = ["step_brothers", "pulp_fiction", "ali", "harry_potter3"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return movieList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tempCell: TableViewCell = tableView.dequeueReusableCell(withIdentifier:
"cell") as! TableViewCell
tempCell.movieTitleLabel.text = movieList[indexPath.row]
tempCell.movieYearLabel.text = yearList[indexPath.row]
tempCell.movieImage.image = UIImage(named: images[indexPath.row] + ".jpeg")
return tempCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let detailVC:MovieDetailViewController = self.storyboard?.instantiateViewController(withIdentifier: "MovieDetailViewController") as! MovieDetailViewController
// assign the values to the local variable declared in ProductDetailViewController Class
detailVC.movieImage = UIImage(named: images[indexPath.row] + ".jpeg")!
// make it navigate to ProductDetailViewController
self.navigationController?.pushViewController(detailVC, animated: true)
}
}
This is for the individual cell in the table view:
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet weak var movieTitleLabel: UILabel!
@IBOutlet weak var movieYearLabel: UILabel!
@IBOutlet weak var movieImage: UIImageView!
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
}
}
This is the MovieDetailViewController:
class MovieDetailViewController: UIViewController {
@IBOutlet weak var movieDetailImage: UIImageView!
@IBOutlet weak var runtimeLabel: UILabel!
@IBOutlet weak var yearDetailLabel: UILabel!
@IBOutlet weak var directorDetailLabel: UILabel!
var runtime: String! // holds the product name
var year: String! // holds the price
var movieImage: UIImage! // holds the product image
var director: String!
override func viewDidLoad() {
super.viewDidLoad()
movieDetailImage.image = movieImage
runtimeLabel.text = runtime
yearDetailLabel.text = year
directorDetailLabel.text = director
// Do any additional setup after loading the view.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
This is the error shown in the terminal, but there are no actual errors in the code:
2022-11-14 17:39:28.232645-0500 Exercise01[25678:1217794] [Storyboard] Unable to find method -[(null) TableViewCell] 2022-11-14 17:39:28.259975-0500 Exercise01[25678:1217794] [Assert] UINavigationBar decoded as unlocked for UINavigationController, or navigationBar delegate set up incorrectly. Inconsistent configuration may cause problems. navigationController=<UINavigationController: 0x141012400>, navigationBar=<UINavigationBar: 0x142106160; frame = (0 47; 0 50); opaque = NO; autoresize = W; layer = <CALayer: 0x600001d72280>> delegate=0x141012400
I can throw in the AppDelegate and SceneDelegate if you need it, just let me know.
Thank you everyone, again! I greatly appreciate the help!
3
Answers
I have tried your code, and I didn’t get any errors, I hope you have set the data source and delegate of table view. Just add this to your viewDidLoad()
I’d first recommend to get rid of Storyboards, they’re error prone and debugging is a hell.
Your problem: I don’t see you connecting the UITableView to the ViewController (with an IBOutlet if you use storyboards).
What you need to do is setting the UITableViewDelegate and the UITableViewDataSource for that UITableView
I think that the delegate and datasource aren’t the only errors…
This is an example (complete code) for how you can do it programmatically:
this is your cell:
this is your movieDetailController:
And This is the result (there isn’t any array for directorDetailLabel)