Multiple inheritance in Swift - swift

So I have a problem where I get shown the error
multiple inheritance from classes UIViewController and UIFont
and being new to programming, I don't really understand what is wrong. So what does this error mean? Can't I add more protocols to the class? This is how my code looks
import UIKit
import Foundation
var items:[String] = []
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIFont {
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.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel?.text = items[indexPath.row]
return cell
}
func fontWithSize(fontSize: 130) -> UIFont {
return fontSize
}
override func viewWillAppear(animated: Bool) {
if var storeditems: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("items") {
items = []
for var i = 0; i<storeditems?.count; ++i {
items.append(storeditems?[i] as NSString)
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
items.removeAtIndex(indexPath.row)
NSUserDefaults.standardUserDefaults().setObject(items, forKey: "items")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
}
}
}
View Controller 2
import UIKit
class ViewController2: UIViewController, UITextFieldDelegate {
#IBOutlet weak var textField: UITextField!
#IBAction func button(sender: AnyObject) {
items.append(textField.text)
NSUserDefaults.standardUserDefaults().setObject(items, forKey: "items")
NSUserDefaults.standardUserDefaults().synchronize()
}
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.
}
*/
}

Swift and Objective-C only support single inheritance. They do support multiple protocols. Extension is principally achieved via composition.
UITableViewDelegate and UITableViewDataSource are protocols.
UIFont is not a protocol, it is a class.

Related

How to debug signal SIGABRT

I have been learning Swift for a while and signal SIGABRT has occurred quite randomly several times. I've tried some tutorials online but it didn't seem working all the time.
This time I was trying to set up a To Do List with two view controllers. One has a tableview and this other has a textfield to add new items. The followings are the code lines for both. Hope someone could help me solve the puzzle.
import UIKit
var toDoList = [String]()
class FirstViewController: UIViewController,UITableViewDelegate {
#IBOutlet var toDoListTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return toDoList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = toDoList[indexPath.row]
return cell
}
}
import UIKit
class SecondViewController: UIViewController {
#IBOutlet var newItem: UITextField!
#IBAction func addItem(sender: AnyObject) {
toDoList.append(newItem.text)
newItem.text = ""
}
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.
}
}
I have received this error when I accidentally renamed an outlet without updating the connection. Is it possible you made this error?

Dynamic datasource/delegates for UITableView in swift

I need to set up different objects based on certain conditions as the datasource & delegate for table view.
But I am not able to assign tableview's datasource/delegate as it throws some errors.
Cannot assign a value of type NSObject? to a value of type UITableViewDelegate?
I did check this Q&A but this did not work.
var dataSourceDelegate:NSObject?
class RootViewController: UIViewController {
...
override func viewDidLoad() {
dataSourceDelegate = TableDataSourceDelegate()
// Table View
tableView = UITableView()
tableView!.setTranslatesAutoresizingMaskIntoConstraints(false)
tableView!.dataSource = dataSourceDelegate
// Cannot assign a value of type NSObject? to a value of type UITableViewDataSource?
tableView!.delegate = dataSourceDelegate
// Cannot assign a value of type NSObject? to a value of type UITableViewDelegate?
view.addSubview(tableView!)
// Constraints
var views:[String:UIView] = ["table":tableView!]
var hTableConstraint = "H:|[table]|"
var vConstraint = "V:|[table]|"
view.addConstraintsToView([hTableConstraint, vConstraint], view: view, viewVariables: views)
}
...
}
This is the datasource/delegate class
class TableDataSourceDelegate:NSObject, UITableViewDataSource, UITableViewDelegate {
// MARK: Datasource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return UITableViewCell()
}
// MARK: Delegates
}
NSObject? doesn't conforms to UITableViewDelegate, neither to UITableViewDataSource. You should create your protocol like
protocol GeneralDataSource: UITableViewDataSource, UITableViewDelegate {}
And then all data sources should conform that protocol.
class MyDataSource: NSObject, GeneralDataSource {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return UITableViewCell()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
}
Then you can use it like this
var myDataSource: GeneralDataSource?
override func viewDidLoad() {
super.viewDidLoad()
self.myDataSource = MyDataSource()
self.tableView.delegate = self.myDataSource
}
This is how your TableDataSourceDelegate should look like:
import UIKit
class TableDataSourceDelegate: NSObject {
}
extension TableDataSourceDelegate: UITableViewDataSource {
#objc func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
#objc func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "defaultCell")
cell.textLabel?.text = "test"
return cell
}
}
extension TableDataSourceDelegate: UITableViewDelegate {
// your delegate implementation here
}
And view controller implementation
import UIKit
// The typealias definition
typealias TVDataSourceDelegate = protocol<UITableViewDataSource, UITableViewDelegate>
class ViewController: UIViewController {
var dataSourceDelegate: TVDataSourceDelegate?
var tableView: UITableView?
override func viewDidLoad() {
super.viewDidLoad()
dataSourceDelegate = TableDataSourceDelegate()
// Table View
tableView = UITableView()
tableView!.translatesAutoresizingMaskIntoConstraints = false
tableView!.dataSource = dataSourceDelegate
tableView!.delegate = dataSourceDelegate
view.addSubview(tableView!)
// other code ...
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Although, I would recommend to separate dataSource and delegate objects (e.g. put the delegate protocol conforming code into your view controller's code.

Swift - 2 tableviews and detailviewcontroller

have problem with 2 tableviews on 2 viewcontrollers and detailviewcontroller, everything is almost working ok, 2 tableviews working like a charm, but problem is in secondviewcontroller where i put array of images to segue to detailVC after each meal/food is clicked, i got same 3 images for mexican food, spanish food, italian, croatian and french. Images(15) does to correspond to 15 meals. Did i go wrong with putting array of images in second view controller? Thank you
ViewController
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
let textForFirstTableView = ["Italian food", "Mexican food", "Croatian food", "Spanish food", "French food"]
let namesOfFood = [["Bolognese", "Milagnese","Pizza"],
["Tortilla", "Chimichanga", "Paella"],
["Burek od mesa","Grah", "Janjetina"],
["Tapas", "Churros", "Flan"],
["Buche de Noel", "Cherry Cake", "Onion Soup"]]
var ObjectNamesOfFood = [String]()
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return textForFirstTableView.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
cell.textLabel?.text = textForFirstTableView[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]
self.performSegueWithIdentifier("Segue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let driver = segue.destinationViewController as! DrugiViewController
var whatToPass = self.ObjectNamesOfFood
driver.arrayToPass = whatToPass
}
}
SecondViewController
import UIKit
class DrugiViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var arrayToPass:[String] = [String]()
var picturesForEachMeal = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"]
var objectpicturesForEachMeal:String!
#IBOutlet weak var tableView2: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tableView2.delegate = self
self.tableView2.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayToPass.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell2") as! UITableViewCell
cell.textLabel?.text = arrayToPass[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.objectpicturesForEachMeal = self.picturesForEachMeal[indexPath.row]
self.performSegueWithIdentifier("toDetailViewController", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let driver = segue.destinationViewController as! DetailViewController
driver.picturesToRetrieve = objectpicturesForEachMeal
}
}
DetailViewController (displaying 15 images for 15 meals)
import UIKit
class DetailViewController: UIViewController {
#IBOutlet weak var backgroundImage: UIImageView!
#IBOutlet weak var slika: UIImageView!
var picturesToRetrieve:String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var connection = self.picturesToRetrieve
self.slika.image = UIImage(named: connection)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
In View Controller store the selected index and pass it to DrugiViewController
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]
self.selectedKitchen = indexPath.row
self.performSegueWithIdentifier("Segue", sender: self)
}
In DrugiViewController calculate each food image index
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var totalIndex = (indexPath.row) + (selectedKitchen * 3)
self.objectpicturesForEachMeal = self.picturesForEachMeal[totalIndex]
self.performSegueWithIdentifier("toDetailViewController", sender: self)
}
that's it
I must say this is not the best approach if you was starting an app from begining you should in your case use an dictionary of array that contain a key country and as result an array of dictionary dish and photo in the first view controller.
In the second view controller you should have an array of dictionary that contain the dish and photo.
In the third view just a simple dictionary that contain a dictionary with dish and photo.
But how you almost finish the easier solution for you is to pass the value from the first table to the second, so lets say if the user selected row 2 you pass and keep that values.
Now in the second table if the user selected row 3 you multiply the number of the row the user selected (3) by the number of the first row select in table view one (2) and you know you have to display the picture number 6 in your final array.
I hope this helps you, I am sure now that you know the idea you will be able to finish your project. Best of luck!
First view controller:
var rowSelected:Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]
self.rowSelected = indexPath.row
self.performSegueWithIdentifier("Segue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let driver = segue.destinationViewController as! DrugiViewController
var whatToPass = self.ObjectNamesOfFood
driver.arrayToPass = whatToPass
driver.rowSelected = rowSelected
}
Second view controller:
var rowSelected:Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.objectpicturesForEachMeal = self.picturesForEachMeal[indexPath.row * self.rowSelected]
self.performSegueWithIdentifier("toDetailViewController", sender: self)
}
This is the basic idea, sorry I couldn't test it as I am in a Windows computer now, but should just work

Swift - displaying data for each row in tableview after row is clicked

Having problems with displaying data for each row in tableview after row is clicked, and i want to use didselectRowAtIndexPath, not without it! :)
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
let textToDisplayInCell:[String] = ["item 1", "item 2", "item 3", "item 4"]
let textDataforEachRowWhenClicked:[String] = ["first item", "second item ", "third item", "fourth item"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.textToDisplayInCell.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Tablecell") as! UITableViewCell
cell.textLabel!.text = self.textToDisplayInCell[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("toDetailSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var whatToPass = self.textDataforEachRowWhenClicked
let detailVC = segue.destinationViewController as! DetailViewController
detailVC.array = whatToPass
}
}
DetailViewcontroller.swift
import UIKit
class DetailViewController: UIViewController {
#IBOutlet weak var label: UILabel!
var array:[String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var connection = self.array
self.label.text = String(stringInterpolationSegment: connection)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
and i got this after i clicked each row, the same data and i want when row - item 1 is clicked to display first item in label in detailviewcontroller and so on with other rows, help , ty
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
let textToDisplayInCell:[String] = ["item 1", "item 2", "item 3", "item 4"]
let textDataforEachRowWhenClicked:[String] = ["first item", "second item ", "third item", "fourth item"]
var selectedValue:String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.textToDisplayInCell.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Tablecell") as! UITableViewCell
cell.textLabel!.text = self.textToDisplayInCell[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedValue = textDataforEachRowWhenClicked[indexPath.row] as String
self.performSegueWithIdentifier("toDetailSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let detailVC = segue.destinationViewController as! DetailViewController
detailVC.message = selectedValue
}
}
In Detail View:
class DetailViewController: UIViewController {
#IBOutlet weak var label: UILabel!
var message:String!
override func viewDidLoad() {
super.viewDidLoad()
var message = self.message
self.label.text = message
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
In the prepareForSegue you passed the entire array to th child controller, not a single specific element. You need to set some variable (_rowToPass) in didSelectRowAtIndexPath to the element you want to pass, and in the prepareForSegue pass only that element.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
_rowToPass = indexPath.row
self.performSegueWithIdentifier("toDetailSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var whatToPass = self.textDataforEachRowWhenClicked[_rowToPass]
let detailVC = segue.destinationViewController as! DetailViewController
detailVC.array = whatToPass
}

Passing data from tableView to ViewController in Swift

I have an App that i'm trying to adapt exactly how i want
I have been following a Youtube tutorial of Seemu Apps to make it but I need to finish it adding an optional ViewController
This app has 2 tableViews showing vehicles and if we click in one row of the first tableView then second tableView will show us a list of selected vehicles.
Here is what we have until now: (image link , because i haven't got ten points reputation on stackOverFlow)
http://subefotos.com/ver/?65ba467040cb9280e8ec49644fd156afo.jpg
All is running perfect, but i want to be able to display information in an optional detailViewController (label with a detailed description of each vehicle and a bigger image of this ) depending of which vehicle we click in the secondTableViewControlle (or modelViewController in the App) exactly how i was following in the tutorial between tableViews
i know that we need to passing data through prepareForSegue method , i have understood this making the steps in the tutorial but when we have 2 tableviewControllers
For example : if we want to display a last viewController with information of Ferrari 458 and a great picture of this car
What do we need to do exactly to show information of each vehicle?
PD : I'm beginner in the programming world, maybe i would need to see it in a very simple way
The whole code:
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var selMake = String()
#IBOutlet var tableView : UITableView!
var transportData : [String] = ["Car", "Plane", "Motorcycle", "Truck" , "Train", "Bicycle" , "Helicopter"]
//////////////////////////////////////////
//viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
//Register custom cell
var nib = UINib(nibName: "customCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")
}
//Numbers of rows in Section
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.transportData.count
}
//cellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
///// Static Cell (no valid for custom cells)
/*
var cell : UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.transportData[indexPath.row]
return cell
*/
var cell:customCellTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as customCellTableViewCell
cell.lblTrans.text = transportData[indexPath.row]
cell.imgTrans.image = UIImage (named: transportData[indexPath.row])
return cell
}
//height
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 90
}
//didSelectRowAtIndexPath
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("Fila \(transportData[indexPath.row]) seleccionada")
selMake = transportData[indexPath.row]
performSegueWithIdentifier("modelView", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "modelView") {
var vc = segue.destinationViewController as modelViewViewController
vc.selMake = selMake
}
}
import UIKit
class customCellTableViewCell: UITableViewCell {
#IBOutlet weak var imgTrans: UIImageView!
#IBOutlet weak var lblTrans: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
import UIKit
class modelViewViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
//////////////////////////////////
var selMake = String()
var tableData : [String] = []
#IBOutlet var tableView: UITableView!
//////////////////////////////////
override func viewDidLoad() {
super.viewDidLoad()
//Register custom cell
var nib = UINib(nibName: "customCell2", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")
switch selMake {
case "Car" :
tableData = ["Ferrari 458", "La Ferrari"]
case "Plane" :
tableData = ["Iberia"]
case "Motorcycle" :
tableData = ["Kawasaki Ninja", "Yamaha Aerox"]
case "Truck" :
tableData = [ "Camion transporte"]
case "Train" :
tableData = [ "Ave" ]
case "Bicycle" :
tableData = ["BMX"]
case "Helicopter" :
tableData = ["HelicopteroCombate"]
default:
println("Sel Make \(selMake)")
}
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
/* var cell : UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.tableData[indexPath.row]
return cell*/
var cell:customCell2TableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as customCell2TableViewCell
cell.lbl2text.text = self.tableData[indexPath.row]
cell.img2image.image = UIImage (named: tableData[indexPath.row])
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("Row \(indexPath.row)selected")
performSegueWithIdentifier("detailView", sender: self)
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 90
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "detailView") {
var vc = segue.destinationViewController as DetailViewController
}
}
import UIKit
class customCell2TableViewCell: UITableViewCell {
#IBOutlet var lbl2text: UILabel!
#IBOutlet var img2image: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
import UIKit
class DetailViewController: UIViewController {
#IBOutlet var imgDetail: UIImageView!
#IBOutlet var lblDetail: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
Try this.
ModelViewViewController
var selectedImage:String?
var selectedLabel:String?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("Row \(indexPath.row)selected")
selectedImage = self.tableData[indexPath.row]
selectedLabel = self.tableData[indexPath.row]
performSegueWithIdentifier("detailView", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "detailView") {
var vc = segue.destinationViewController as DetailViewController
vc.img = selectedImage
vc.lblDetail = selectedLabel
}
}
class DetailViewController: UIViewController {
#IBOutlet var imgDetail: UIImage!
#IBOutlet var lblDetail: UILabel!
var img:String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
imgDetail = UIImage(named: img)
}
This should work.