skip to Main Content
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 {
    
    }
    
    
}

2

Answers


  1. In the first declaration of the View Controller class, you have created it to conform to UITableViewDelegate and UITableViewDataSource. To fix the code, remove UITableViewDelegate and UITableViewDataSource from the class declaration.

    In conclusion, just make your second line of code class ViewController : UIViewController{

    Also, replace vc.task = tasks(indexPath.row) with vc.task = tasks[indexPath.row] because when accessing array elements, you use brackets ([]), not parenthesis.

    Login or Signup to reply.
  2. If you are using viewController and will adapt it to tableViewDelegate & DataSource Then the below will work but if you are using tableViewController you will need to remove the extension, you don’t need it, and regarding the tableView functions you can create them in your ViewContoller

    import UIKit
    class ViewController : UIViewController {
    
    @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)
                
            }
            
        }
    
        DispatchQueue.main.async {
           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, UITableViewDataSource { 
    
    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] 
        navigationController?.pushViewController(vc, animated: true)
        
    }
        
      }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           return tasks.count
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search