Reputation: 11
I'm a little newbie and I have a doubt, I have a TableViewController
and another ViewController
that I have as a detailViewController
, what I try to do is that when a cell is selected in the tableview
, it presents the corresponding data stored in core data for that cell in the detailViewcontroller
.
This is the file that controls the tableViewcontroller
:
import UIKit
class CostumerTableViewController: UITableViewController {
var costumerArray:[Costumer] = []
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.reloadData()
self.fetchData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return costumerArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let name = costumerArray[indexPath.row]
cell.textLabel?.text = name.costumerName!
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
if editingStyle == .delete {
let costumerDelete = costumerArray[indexPath.row]
context.delete(costumerDelete)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
do {
costumerArray = try context.fetch(Costumer.fetchRequest())
} catch {
print(error)
}
}
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let DvC = Storyboard.instantiateViewController(withIdentifier: "costumerDetailViewController") as! costumerDetailViewController
let n = costumerArray[indexPath.row]
let Cn = n.costumerName!
DvC.getCostumerName = Cn
self.navigationController?.pushViewController(DvC, animated: true)
}
func fetchData() {
// se crea el context
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do { // se hace el request del array
costumerArray = try context.fetch(Costumer.fetchRequest())
} catch {
print(error)
}
}
}
In the compilation does not give me any problem, some everything goes well the problem is that it does not present anything in the detail viewController
label that in this case I try to send the data from this code.
This is the detailViewController
code :
import UIKit
class costumerDetailViewController: UIViewController {
var getCostumerName = String()
@IBOutlet weak var labelName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
labelName.text! = getCostumerName
}
}
Upvotes: 0
Views: 55
Reputation: 2324
First Check Cn
has value or "" on this line.
let Cn = n.costumerName
Change your code in class costumerDetailViewController
for declare getCostumerName
var getCostumerName = "" //for avoid crash. if value goes nil.
Use Print()
in viewDidLoad and debug it.
Hope this will help you.
Upvotes: 1