Data being passed from UITableView to new Controller, SWIFT - ios

I've been at this for a while and I can not figure out how to pass data from the table view cell to a new "detail" controller. I understand the segue that connects it to a new controller but setting variables in that class to some values fails (doesn't show the values). This is what I currently have:
import UIKit
class BuyTreeViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {
var products : [Product] = []
var filteredProducts = [Product]()
override func viewDidLoad() {
self.products = [
Product(name:"Chocolate", price: 11, description: "i"),
Product(name:"Candy", price: 12, description: "h"),
Product(name:"Break", price: 13, description: "g"),
Product(name:"Apple", price: 14, description: "f"),
Product(name:"Computer", price: 15, description: "e"),
Product(name:"Laptop", price: 16, description: "d"),
Product(name:"Cup", price: 17, description: "c"),
Product(name:"Table", price: 18, description: "b"),
Product(name:"Chair", price: 19, description: "a")
]
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredProducts.count
} else {
return self.products.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//ask for a reusable cell from the tableview, the tableview will create a new one if it doesn't have any
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
var product : Product
// Check to see whether the normal table or search results table is being displayed and set the Candy object from the appropriate array
if tableView == self.searchDisplayController!.searchResultsTableView {
product = filteredProducts[indexPath.row]
} else {
product = products[indexPath.row]
}
// Configure the cell
cell.textLabel!.text = product.name
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
self.filteredProducts = self.products.filter(
{
( product : Product) -> Bool in
var categoryMatch = (scope == "All") || (product.category == scope)
var stringMatch = product.name.rangeOfString(searchText)
return categoryMatch && (stringMatch != nil)
})
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
let scopes = self.searchDisplayController!.searchBar.scopeButtonTitles as [String]
let selectedScope = scopes[self.searchDisplayController!.searchBar.selectedScopeButtonIndex] as String
self.filterContentForSearchText(searchString, scope: selectedScope)
return true
}
func searchDisplayController(controller: UISearchDisplayController!,
shouldReloadTableForSearchScope searchOption: Int) -> Bool {
let scope = self.searchDisplayController!.searchBar.scopeButtonTitles as [String]
self.filterContentForSearchText(self.searchDisplayController!.searchBar.text, scope: scope[searchOption])
return true
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("productDetail", sender: tableView)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "productDetail" {
// let productDetailViewController = segue.destinationViewController as UIViewController
let productDetailViewController = segue.destinationViewController as ProductDetailViewController
productDetailViewController.testLabel.text = "123345"
if sender as UITableView == self.searchDisplayController!.searchResultsTableView {
let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()!
let destinationTitle = self.filteredProducts[indexPath.row].name
productDetailViewController.title = destinationTitle
} else {
let indexPath = self.tableView.indexPathForSelectedRow()!
let destinationTitle = self.products[indexPath.row].name
productDetailViewController.title = destinationTitle
}
productDetailViewController.productPriceText = "10"
productDetailViewController.productTitleText = "100"
productDetailViewController.productDescriptionText = "100"
}
}
And this is what I have in the ProductDetailViewController:
import UIKit
class ProductDetailViewController: UIViewController {
#IBOutlet weak var productDescription: UITextView!
#IBOutlet weak var productPrice: UITextField!
#IBOutlet weak var productTitle: UITextField!
#IBOutlet weak var connectUserButton: UIButton!
#IBOutlet weak var testLabel: UILabel!
// var productDescriptionText: String
// var productPriceText: String
// var productTitleText: String
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// productDescription.text = productDescriptionText
// productPrice.text = productPriceText
// productTitle.text = productTitleText
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
#IBAction func contactUserClicked(sender: AnyObject) {
println("conentUserButton clicked")
}

For your prepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "productDetail" {
let indexPath = self.tableView.indexPathForSelectedRow()
let theSelectedRow = filteredProducts[indexPath!.row]
let theDestination = segue.destinationViewController as ProductDetailViewController
theDestination.productPriceText = "10"
theDestination.productTitleText = "100"
theDestination.productDescriptionText = "100"
} else
//only needed if more than one segue
}
}
Your detailViewController looked good to me. If you still have issues, I will post a more complete example.

Related

iOS master detail sec

I'm working on a Contacts app which I'm developing using swift. I recently implemented sections and now the detail controller is not working properly. Whenever I click on a contact, it shows details for some other contact. I think the main problem is in prepareforsegue function but I can't figure it out. Help Pls!
//
// ContactListViewController.swift
// TechOriginators
//
// Created by Xcode User on 2017-10-09.
// Copyright © 2017 Xcode User. All rights reserved.
//
import UIKit
import Foundation
class ContactListViewController : UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
#IBOutlet var contactsTableView : UITableView!
var contactViewController: ContactViewController? = nil
var contacts : [Contact] = [] {
didSet{
self.contactsTableView.reloadData()
}
}
//Variables to implement sections in UITableView
var sectionLetters: [Character] = []
var contactsDict = [Character: [String]]()
var contactsName = [String]()
//Search Controller
let searchController = UISearchController(searchResultsController: nil)
//Variable to store filtered contacts through search
var filteredContacts = [Contact]()
//Function to show details of a contact record
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail"{
if let indexPath = contactsTableView.indexPathForSelectedRow{
let object = contacts[indexPath.row]
//let object = contactsDict[sectionLetters[indexPath.section]]![indexPath.row]
let controller = segue.destination as! ContactViewController
controller.detailItem = object
}
}
}
func createDict(){
contactsName = contacts.map{$0.FirstName}
//print(contactsName)
sectionLetters = contactsName.map{ (firstName) -> Character in
return firstName[firstName.startIndex]
}
sectionLetters.sorted()
sectionLetters = sectionLetters.reduce([], { (list, firstName) -> [Character] in
if !list.contains(firstName){
return list + [firstName]
}
return list
})
for entry in contactsName{
if contactsDict[entry[entry.startIndex]] == nil {
contactsDict[entry[entry.startIndex]] = [String]()
}
contactsDict[entry[entry.startIndex]]!.append(entry)
}
for (letter, list) in contactsDict{
contactsDict[letter] = list.sorted()
}
print(sectionLetters)
print(contactsDict)
}
// //Function to load contacts
func loadContacts()
{
self.contacts = getContacts()
}
/*private let session: URLSession = .shared
func loadContacts()
{
//let url = URL(string: "http://127.0.0.1:8080/api/Contacts")!
let url = URL(string: "http://10.16.48.237/api/Contacts")!
let task = session.dataTask(with: url) { (data, response, error) in
print("dataRecieved \(data)")
print("error \(error)")
print ("response \(response)")
guard let data = data else { return }
do {
self.contacts = try parse(data)
}
catch {
print("JSONParsing Error: \(error)")
}
}
task.resume() // firing the task
}*/
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44
}
func numberOfSections(in tableView: UITableView) -> Int {
return sectionLetters.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if self.searchController.isActive {
return nil
} else {
return String(sectionLetters[section])
}
//return String(sectionLetters[section])
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.isActive && searchController.searchBar.text != "" {
return filteredContacts.count
}
//return self.contacts.count
print(contactsDict[sectionLetters[section]]!.count)
return contactsDict[sectionLetters[section]]!.count
}
//Function to display cells in UITableView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell" , for : indexPath)
let contact = contacts[indexPath.row]
let object: Contact
if searchController.isActive && searchController.searchBar.text != ""{
let contact = self.filteredContacts[indexPath.row]
cell.textLabel?.text = contact.FirstName + " " + contact.LastName
}else{
//contact = contactsDict[sectionLetters[indexPath.section]]![indexPath.row]
//cell.textLabel?.text = contact.FirstName + " " + contact.LastName
cell.textLabel?.text = contactsDict[sectionLetters[indexPath.section]]![indexPath.row]
}
return cell
}
//Function to update search results when the user types in search bar
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
func updateSearchResultsForSearchController(searchController: UISearchController){
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
//Function to find matches for text entered in search bar
func filterContentForSearchText(searchText: String){
filteredContacts = contacts.filter{p in
var containsString = false
if p.FirstName.lowercased().contains(searchText.lowercased()){
containsString = true
}else if p.LastName.lowercased().contains(searchText.lowercased()){
containsString = true
}else if p.Division.lowercased().contains(searchText.lowercased()){
containsString = true
}else if p.Department.lowercased().contains(searchText.lowercased()){
containsString = true
}else if p.BusinessNumber.lowercased().contains(searchText.lowercased()){
containsString = true
}else if p.HomePhone.lowercased().contains(searchText.lowercased()){
containsString = true
}else if p.CellularPhone.lowercased().contains(searchText.lowercased()){
containsString = true
}else if p.Role.lowercased().contains(searchText.lowercased()){
containsString = true
}
return containsString
}
contactsTableView.reloadData()
}
//Function to sorts contacts by First Name
func sortContacts() {
contacts.sort() { $0.FirstName < $1.FirstName }
contactsTableView.reloadData();
}
override func viewDidLoad() {
super.viewDidLoad()
loadContacts()
sortContacts()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
self.definesPresentationContext = true
createDict()
contactsTableView.tableHeaderView = searchController.searchBar
//contactsTableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
/*
// 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.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
//
// ContactViewController.swift
// TechOriginators
//
// Created by Xcode User on 2017-10-09.
// Copyright © 2017 Xcode User. All rights reserved.
//
import UIKit
class ContactViewController: UIViewController {
//#IBOutlet weak var detailDescriptionLabel: UILabel!
#IBOutlet weak var firstNameLabel: UILabel?
#IBOutlet weak var lastNameLabel: UILabel?
#IBOutlet weak var divisionLabel: UILabel?
#IBOutlet weak var departmentLabel: UILabel?
#IBOutlet weak var businessPhoneButton: UIButton?
#IBOutlet weak var homePhoneButton: UIButton?
#IBOutlet weak var cellularPhoneButton: UIButton?
#IBOutlet weak var roleLabel: UILabel?
/*#IBOutlet weak var firstNameLabel: UILabel?
#IBOutlet weak var lastNameLabel: UILabel?
#IBOutlet weak var phoneButton: UIButton?
#IBOutlet weak var emailButton: UIButton?*/
func configureView() {
// Update the user interface for the detail item.
if let detail = self.detailItem {
self.title = detail.FirstName + " " + detail.LastName
firstNameLabel?.text = detail.FirstName
lastNameLabel?.text = detail.LastName
divisionLabel?.text = detail.Division
departmentLabel?.text = detail.Department
businessPhoneButton?.setTitle(detail.BusinessNumber, for: .normal)
homePhoneButton?.setTitle(detail.HomePhone, for: .normal)
cellularPhoneButton?.setTitle(detail.CellularPhone, for: .normal)
roleLabel?.text = detail.Role
}
}
#IBAction func businessPhoneButtonPressed(sender: UIButton){
if let bPhone = detailItem?.BusinessNumber{
if let url = URL(string: "tel://\(bPhone)"), UIApplication.shared.canOpenURL(url){
if #available(iOS 10, *){
UIApplication.shared.open(url)
}else{
UIApplication.shared.openURL(url)
}
}
}
}
#IBAction func homePhoneButtonPressed(sender: UIButton){
if let hPhone = detailItem?.HomePhone{
if let url = URL(string: "tel://\(hPhone)"), UIApplication.shared.canOpenURL(url){
if #available(iOS 10, *){
UIApplication.shared.open(url)
}else{
UIApplication.shared.openURL(url)
}
}
}
}
#IBAction func cellularPhoneButtonPressed(sender: UIButton){
if let cPhone = detailItem?.CellularPhone{
/*if let url = NSURL(string: "tel://\(phone)"){
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}*/
if let url = URL(string: "tel://\(cPhone)"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
}
}
/*#IBAction func emailButtonPressed(sender: UIButton){
if let email = detailItem?.email{
if let url = URL(string:"mailto:\(email)"){
UIApplication.shared.open(url as URL)
}
}
}*/
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
configureView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var detailItem: Contact? {
didSet {
// Update the view.
self.configureView()
}
}
/*
// 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.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
I suggest using the same data structure for all your table operations to be consistent. Try populating your dictionary as:
var contactsDict = [Character: [Contact]]()
That way, you can always use the section to find the right array and the row to find the right offset in the array.
Separating the names from the contacts in the table's data is inviting them to get out of synch.

Not able to display values in another viewcontroller from TableView in Swift

I want to display the details of one one table row onto another viewController. But it shows an error saying ' fatal error: unexpectedly found nil while unwrapping an Optional value'
The code for my VC is as follows:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate,UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate {
#IBOutlet var nameForUser: UITextField!
#IBOutlet var loginButton: UIButton!
#IBOutlet var tableView: UITableView!
let allEvents = Events.allEvents
var nextScreenRow: Events!
override func viewDidLoad() {
super.viewDidLoad()
//nameForUser.text! = "Please Enter Name Here"
// Do any additional setup after loading the view, typically from a nib.
}
func textFieldDidBeginEditing(textField: UITextField) {
textField.text = ""
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.allEvents.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("eventsCell")!
let event = self.allEvents[indexPath.row]
cell.textLabel?.text = event.eventName
cell.imageView?.image = UIImage(named: event.imageName)
cell.detailTextLabel?.text = event.entryType
//cell.textLabel?.text = allEvents[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
nextScreenRow = allEvents[indexPath.row]
performSegueWithIdentifier("tryToConnect", sender:self)
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.allEvents.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let sec = collectionView.dequeueReusableCellWithReuseIdentifier("eventsSec", forIndexPath: indexPath) as! GridCollectionViewCell
let event = self.allEvents[indexPath.row]
sec.imageView.image = UIImage(named: event.imageName)
sec.caption.text = event.entryType
return sec
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("tryToConnect2", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "successfulLogin"){
segue.destinationViewController as! TabController
//let userName = nameForUser.text
//controller.userName = userName
}
else if (segue.identifier == "tryToConnect"){
let dest = segue.destinationViewController as! DetailedEventViewController
dest.deatiledEvent.text = nextScreenRow.eventName
dest.eventType.text = nextScreenRow.entryType
dest.imageView.image = UIImage(named: nextScreenRow.imageName)
}
}
#IBAction func loginButtonWhenPressed(sender: UIButton) {
let userName = nameForUser.text
if userName == "" {
let nextController = UIAlertController()
nextController.title = "Error!"
nextController.message = "Please enter a name"
let okAction = UIAlertAction(title: "okay", style: UIAlertActionStyle.Default) {
action in self.dismissViewControllerAnimated(true, completion: nil)
}
nextController.addAction(okAction)
self.presentViewController(nextController, animated: true, completion: nil)
}
}
}
When I run this, it shows the error. I have also assigned the delegates for the table view. The code for 'DetailedEventVC' is:
import UIKit
class DetailedEventViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var deatiledEvent: UILabel!
#IBOutlet weak var eventType: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Why does it show that the values are nil?
Help would be greatly appreciated.
Thanks in advance.
The 'EventsDetails.swift' file which has the details of events are a structure. Is there anything wrong in the way I'm calling the values?
import Foundation
import UIKit
struct Events {
let eventName: String
let entryType: String
let imageName: String
static let EventKey = "NameKey"
static let EntryTypeKey = "EntryType"
static let ImageNameKey = "ImageNameKey"
init(dictionary:[String : String]) {
self.eventName = dictionary[Events.EventKey]!
self.entryType = dictionary[Events.EntryTypeKey]!
self.imageName = dictionary[Events.ImageNameKey]!
}
}
extension Events {
static var allEvents: [Events] {
var eventsArray = [Events]()
for d in Events.localEventsData(){
eventsArray.append(Events(dictionary: d))
}
return eventsArray
}
static func localEventsData()-> [[String: String]] {
return [
[Events.EventKey:"Metallica Concert in Palace Grounds", Events.EntryTypeKey: "Paid Entry", Events.ImageNameKey:"Metallica"],
[Events.EventKey:"Saree Exhibition in Malleswaram Grounds", Events.EntryTypeKey: "Free Entry", Events.ImageNameKey:"SareeExhibition"],
[Events.EventKey:"Wine tasting event in Links Brewery", Events.EntryTypeKey: "Paid Entry", Events.ImageNameKey:"WineTasting"],
[Events.EventKey:"Startups Meet in Kanteerava Stadium", Events.EntryTypeKey: "Paid Entry", Events.ImageNameKey:"StartupMeet"],
[Events.EventKey:"Summer Noon Party in Kumara Park", Events.EntryTypeKey: "Paid Entry", Events.ImageNameKey:"SummerNoonParty"],
[Events.EventKey:"Rock and Roll nights in Sarjapur Road", Events.EntryTypeKey: "Paid Entry", Events.ImageNameKey:"RockNRollNight"],
[Events.EventKey:"Barbecue Fridays in Whitefield", Events.EntryTypeKey: "Paid Entry", Events.ImageNameKey:"BBQFriday"],
[Events.EventKey:"Summer workshop in Indiranagar", Events.EntryTypeKey: "Free Entry", Events.ImageNameKey:"SummerWorkshop"],
[Events.EventKey:"Impressions & Expressions in MG Road", Events.EntryTypeKey: "Free Entry", Events.ImageNameKey:"ImpressionAndExpression"],
[Events.EventKey:"Italian carnival in Electronic City", Events.EntryTypeKey: "Free Entry", Events.ImageNameKey:"ItalianCarnival"]
]
}
}
Create properties in your destination viewController and use them like below
import UIKit
class DetailedEventViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var deatiledEvent: UILabel!
#IBOutlet weak var eventType: UILabel!
var myImage = UIImage?
var eventDetails = ""
var typeOfEvent = ""
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = myImage
deatiledEvent.text = eventDetails
eventType.text = typeOfEvent
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
and then in your first viewController you can access them like
let dest = segue.destinationViewController as! DetailedEventViewController
dest.eventDetails = nextScreenRow.eventName
dest.typeOfEvent = nextScreenRow.entryType
dest.myImage = UIImage(named: nextScreenRow.imageName)

passing object data between viewcontrollers in swift

I'm learning how to pass object data between viewcontrollers, and my source is:
firstly the class which represents the objects Flyer
class Flyer{
var title: String!
var desc: String!
var imageUrl: String!
init(dictionary: [String: AnyObject]) {
title = dictionary["title"] as? String
desc = dictionary["desc"] as? String
imageUrl = dictionary["imageUrl"] as? String
}
}
now the mainViewController class:
class MainViewController: UIViewController, UITableViewDataSource {
#IBOutlet var tableview:UITableView!
let apiClient = ApiClient()
var flyers: [Flyer]!
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
apiClient.mainService.getList() { flyers, error in
if flyers != nil {
self.flyers = flyers
self.tableview.reloadData()
} else {
print("error: \(error)")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.flyers?.count ?? 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("mainObjectCell") as! MainTableViewCell
cell.mainObject = self.flyers?[indexPath.row]
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "detailSegue" {
let detailViewController = segue.destinationViewController as! DetailMainViewController
if let indexPath = tableView.indexPathForCell(sender as! MainTableViewCell) {
detailViewController.detailString = self.flyers?[indexPath.row]
}
}
}
}
the output of this class is an error, ambiguous use of tableview(_:numbersOfRowsInSection:) in the let indexPath
the detailViewController class:
class DetailMainViewController: UIViewController {
#IBOutlet weak var detailMainLabel: UILabel!
var detailString: String?
var receivedCellIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
detailMainLabel.text = detailString
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I would like to know how to solve the error in the mainViewController class and how to pass the desc property of the flyer object to the detailViewController and show it in the label field

Swift: Send data from a UITableView to UIViewController

I have a list of Cities in a TableView and want to pass the data from the cell in TableView to a UIViewController. Now when I pass the data I also want to pass the Latitude and Longitude of those Cities to the UIViewController. Here is the TableViewController code.
class MasterTableViewController: UITableViewController
{
let fruits = ["London", "Melbourne", "Singapore", "Brazil", "Germany", "Monza", "Dallas", "Auckland", "Brussels", "Shanghai", "Sepang", "Barcelona"]
override func awakeFromNib() {
super.awakeFromNib()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Segues
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
let fruit = fruits[indexPath.row]
(segue.destinationViewController as! ViewController).detailItem = fruit
}
}
}
// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fruits.count
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if (indexPath.row == 0){
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
let fruit = fruits[indexPath.row]
cell.textLabel!.text = fruit
return cell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
}
I can pass City but how would you pass the latitude of the longitude of the cities to the UIViewController. Here is the code for the UIViewController.
class ViewController: UIViewController {
#IBOutlet weak var currentTemperatureLabel: UILabel?
#IBOutlet weak var currentHumidityLabel: UILabel?
#IBOutlet weak var currentPrecipitationLabel: UILabel?
#IBOutlet weak var currentWeatherIcon: UIImageView?
#IBOutlet weak var currentWeatherSummary: UILabel?
#IBOutlet weak var refreshButton: UIButton?
#IBOutlet weak var activityIndicator: UIActivityIndicatorView?
#IBOutlet weak var detailDescriptionLabel: UILabel?
// Location coordinates
let coordinate: (lat: Double, lon: Double) = (37.8267,-122.423)
// TODO: Enter your API key here
private let forecastAPIKey = ""
var detailItem: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}
func configureView() {
if let detail: AnyObject = self.detailItem {
if let label = self.detailDescriptionLabel {
label.text = detail.description
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.configureView()
retrieveWeatherForecast()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func retrieveWeatherForecast() {
let forecastService = ForecastService(APIKey: forecastAPIKey)
forecastService.getForecast(coordinate.lat, lon: coordinate.lon) {
(let currently) in
if let currentWeather = currently {
dispatch_async(dispatch_get_main_queue()) {
if let temperature = currentWeather.temperature {
self.currentTemperatureLabel?.text = "\(temperature)º"
}
if let humidity = currentWeather.humidity {
self.currentHumidityLabel?.text = "\(humidity)%"
}
if let precipitation = currentWeather.precipProbability {
self.currentPrecipitationLabel?.text = "\(precipitation)%"
}
if let icon = currentWeather.icon {
self.currentWeatherIcon?.image = icon
}
if let summary = currentWeather.summary {
self.currentWeatherSummary?.text = summary
}
self.toggleRefreshAnimation(false)
}
}
}
}
#IBAction func refreshWeather() {
toggleRefreshAnimation(true)
retrieveWeatherForecast()
}
func toggleRefreshAnimation(on: Bool) {
refreshButton?.hidden = on
if on {
activityIndicator?.startAnimating()
} else {
activityIndicator?.stopAnimating()
}
}
}
Add a property for the location to ViewController.
You can then pass the city's location in MasterTableViewController prepareForSegue, similar to how you're passing the city (detailItem) now.
Update:
To pass another parameter, you would add it to your ViewController
var coordinateItem: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}
and then pass it in prepareForSegue
(segue.destinationViewController as! ViewController).coordinateItem = location
Update 2:
Yes, you can initialize an array of coordinates, and pass a latitude and longitude coordinate.
var locations : [[Double]] = [[51.50722, -0.12750], [-37.8136, 144.9631], ...]
In this case, your coordinateItem would be a [Double].
You can access the two doubles by coordinateItem[0] and coordinateItem[1]
The general concept is the same, regardless of whether you're passing an array or a String.

Passing object from one scene to next

I'm trying to pass an object in Swift from one scene to the next, and I'm getting an error with the code below saying:
Cannot invoke indexPathForSelectedRow with no arguments.
Is this a new requirement?
It seems to me the code below should work, but I'm confused as to why it isn't.
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var myTableView: UITableView!
var propertyArray: [Property] = [Property]()
override func viewDidLoad() {
super.viewDidLoad()
self.setUpProperties()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setUpProperties() {
var property1 = Property(cardImage: "image1.png", name: "Name1", location: "Hollywood")
var property2 = Property(cardImage: "image2.png", name: "Name2", location: "Astoria")
var property3 = Property(cardImage: "image3.png", name: "Name3", location: "Ft. Greene")
propertyArray.append(property1)
propertyArray.append(property2)
propertyArray.append(property3)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return propertyArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("FeedCell") as! CustomCell
let property = propertyArray[indexPath.row]
println(property.name + " " + property.location)
cell.setProperty(property.cardImage, name: property.name, location: property.location)
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
var detailPage = segue.destinationViewController as! DetailViewController
if let indexPath = self.tableView.indexPathForSelectedRow() {
let selectedProperty = propertyArray[indexPath.row]
detailPage.currentProperty = selectedProperty
}
}
}
}
The problem is that in this line:
if let indexPath = self.tableView.indexPathForSelectedRow() {
you have not correctly used the name of your view controller's property. Its name is myTableView. So change it to this:
if let indexPath = self.myTableView.indexPathForSelectedRow() {
Problem solved!

Resources