I was working on the to-do list app. I added the prototype cell and its identifier as a cell. but the cells are not showing in the table view while running in the simulator. Can anyone tell me what mistakes I am making? The following is code from the view controller.
import UIKit
class ViewController: UIViewController {
@IBOutlet var tableView : UITableView!
let Tasks : [String] = []
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func didTapAdd(){
}
}
extension ViewController : UITextViewDelegate , UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Tasks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = Tasks[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath : IndexPath){
tableView.deselectRow(at: indexPath, animated: true)
}
}
Post
Replies
Boosts
Views
Activity
I made a Tasks app. It runs completely fine without any error in code and the new task is also saved by clicking the save button but the newly added task does not display on the table view cell.
First view controller for data display code:
import UIKit
class todolistVC: UIViewController , UITableViewDelegate , UITableViewDataSource {
@IBOutlet var table: UITableView!
@IBOutlet var label : UILabel!
private var tasks = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Tasks"
table.delegate = self
table.dataSource = self
//for saving data even after closing app
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)
}
}
table.reloadData()
}
@IBAction func didTapAdd() {
let vc = storyboard?.instantiateViewController(withIdentifier: "entry") as! entryVC
vc.title = "New Task"
vc.completion = {
DispatchQueue.main.async {
self.updateTasks()
self.label.isHidden = true
self.table.isHidden = false
}}
navigationController?.pushViewController(vc, animated: true)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell" , for : indexPath)
cell.textLabel?.text = tasks[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
guard let vc = storyboard?.instantiateViewController(withIdentifier: "detail") as? DetailsVC else {
return
}
vc.title = "Task"
vc.task = tasks[indexPath.row]
navigationController?.popToViewController(vc, animated: true)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tasks.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath],with: .automatic)
}
}
}
the entry view controller code:
import UIKit
class entryVC: UIViewController, UITextFieldDelegate{
@IBOutlet var field: UITextField!
var completion : (() -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
field.delegate = self
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .done, target: self, action: #selector(saveTask))
}
func textFieldShouldReturn(_ textField: UITextField)-> Bool {
saveTask()
return true
}
@objc func saveTask(){
guard let text = field.text, !text.isEmpty else{
return
}
guard let count = UserDefaults().value(forKey: "count") as? Int else {
return
}
let newCount = count+1
UserDefaults().set(newCount, forKey: "count")
UserDefaults().set(text, forKey: "Task_\(newCount)")
completion?()
navigationController?.popViewController(animated: true)
}
}
This is a app for weather in which i used weatherkit.I have added "privacy - Location when in Usage Description in info of project and also enabled location in features of simulator to Apple.After doing all this ,still the permission message for location is not appearing in app in simulator.Following is my code:
import CoreLocation
import UIKit
//final means this class will not be subclassed
final class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
//to access weather data
let service = WeatherService()
override func viewDidLoad() {
super.viewDidLoad()
setupView()
getUserLocation()
}
func getUserLocation(){
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func getWeather(location : CLLocation){
//asynchoronus task
Task {
do {
//if it throws error
//await tells to wait until function gives results
let result = try await service.weather(for: location)
print("Current : "+String(describing: result.currentWeather))
print("Daily : "+String(describing: result.dailyForecast))
print("Hourly : "+String(describing: result.hourlyForecast))
} catch {
//it will be excecuted
print(error.localizedDescription)
}
}
}
func setupView(){
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else {
return
}
locationManager.stopUpdatingLocation()
getWeather(location: location)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .denied:
print("location permission denied")
case .authorizedWhenInUse, .authorizedAlways :
manager.startUpdatingLocation()
print("Location Permission Granted")
case .notDetermined :
print("location Permission not determined")
default:
break
}
}
}