import UIKit
class ViewController : UIViewController, UITableViewDelegate , UITableViewDataSource{
@IBOutlet var tableView: UITableView!
var tasks = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Tasks"
tableView.delegate = self
tableView.dataSource = self
if !UserDefaults().bool(forKey: "setup"){
UserDefaults().set(true, forKey: "setup")
UserDefaults().set(0, forKey: "count")
}
updateTasks()
}
func updateTasks() {
tasks.removeAll()
guard let count = UserDefaults().value(forKey: "count") as? Int else{
return
}
for x in 0 ..< count {
if let task = UserDefaults().value(forKey: "task_(x+1)") as? String {
tasks.append(task)
}
}
tableView.reloadData()
}
@IBAction func didTapAdd() {
let vc = storyboard?.instantiateViewController(identifier: "entry") as! EntryViewController
vc.title = "New Task"
vc.update = {
DispatchQueue.main.async {
self.updateTasks()
}
}
navigationController?.pushViewController(vc, animated: true)
}
}
extension ViewController: UITableViewDelegate { //error:Redundant conformance of 'ViewController' to protocol 'UITableViewDelegate'
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.deselectRow(at: indexPath, animated: true)
let vc = storyboard?.instantiateViewController(identifier: "tasks") as! TaskViewController
vc.title = "New Tasks"
vc.task = tasks(indexPath.row) //errors:Cannot call value of non-function type '[String]'
navigationController?.pushViewController(vc, animated: true)
}
}
extension ViewController: UITableViewDataSource { //error:Redundant conformance of 'ViewController' to protocol 'UITableViewDataSource'
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
}
Question posted in Xcode
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
2
Answers
In the first declaration of the
View Controller
class, you have created it to conform toUITableViewDelegate
andUITableViewDataSource
. To fix the code, removeUITableViewDelegate
andUITableViewDataSource
from the class declaration.In conclusion, just make your second line of code
class ViewController : UIViewController{
Also, replace
vc.task = tasks(indexPath.row)
withvc.task = tasks[indexPath.row]
because when accessing array elements, you use brackets ([]
), not parenthesis.If you are using viewController and will adapt it to
tableViewDelegate
&DataSource
Then the below will work but if you are usingtableViewController
you will need to remove the extension, you don’t need it, and regarding the tableView functions you can create them in yourViewContoller