Pass Data from Tableviewcontroller to Another Tableviewcontroller in Swift

Passing data from view controller to table view controller in ios swift 2

You have to declare String variable in your 3rd View Controller (Table View Controller) like

var myString: String = ""

override func viewDidLoad() {
super.viewDidLoad()
}

You can assign value to myString variable from your first view controller like

let DestViewController:tableviewcontroller = segue.destinationViewController as! tableviewcontroller
DestViewController.myString = labelText.text!

And your your tableview view method will be like

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell1 = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath)
cell1.textLabel!.text = myString
return cell1
}

The Above is the solution, below i am explaining what was the problem

There is no such text method for UITableview, hence you were getting error at

DestViewController.passdata = tblLable.text! //In first View Controller
cell1.textLabel!.text = tblLable.text! //In second View controller

Passing data from tableView to ViewController in Swift

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.

Pass data between TableView and another ViewController in Swift

Your prepareForSegue must override the UIViewController method which is

func prepare(for segue: UIStoryboardSegue, sender: Any?)

Therefore your method in NewsfeedViewController should be

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondViewController = segue.destination as? EventViewController
secondViewController?.data = sender as? eventStruct
}

it would also be a good idea to add some error checking

how to pass data from one Tableview to another Tableview when button is pressed in cell?

You can pass the values in closures.

So, in your Cell class you could change your closure var to:

 var addActionHandler: ((Int) -> Void)?

Then, in your addToCart button action, something along these lines:

@IBAction func atcBtn(_ sender: UIButton) {

// pass back the user selected values
var i = 0
switch lastSelectedButton {
case optionBtn1:
i = 1
case optionBtn2:
i = 2
default:
i = 3
}
self.addActionHandler?(i)

}

Create a .selectedOption property of your Items class, you should add one (of type Int). You can use that to track the user's selection.

Modify cellForAt in Cell:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? Cell else { return UITableViewCell() }

// use var to make item mutable
var item = itemSetup[indexPath.row]
cell.configure(withItem: item)
cell.addActionHandler = { (option: Int) in
print("Option selected = \(option)")
item.selectedOption = option

Cart.currentCart.items.append(item)
}

return cell
}

In your CartCell you can make the labels like this:

    if items.selectedOption == 1 {
lblSubTotal.text = "$\(formatter.string(for: items.price1)!)"
lblWeight.text = "\(items.weight1)"

} else if items.selectedOption == 2 {
lblSubTotal.text = "$\(formatter.string(for: items.price2)!)"
lblWeight.text = "\(items.weight2)"

} else if items.selectedOption == 3 {
lblSubTotal.text = "$\(formatter.string(for: items.price3)!)"
lblWeight.text = "\(items.weight3)"

}

passing data from button in cell to another tableview?

The first thing I would do is get rid of CartItem - It doesn't seem to be doing anything except wrapping an Items instance, and you have some confusion in your code as to whether you are using CartItem or Items (I would probably also rename Items to Item - singular).

class Cart {
static let currentCart = Cart()
var cartItems = [Items]()
}

To get the "add to cart" action from your cell you can use a delegation pattern or provide a closure to handle the action. I will use a closure

class MenuCell: UITableViewCell {

@IBOutlet weak var name: UILabel!
@IBOutlet weak var category: UILabel!
@IBOutlet weak var productImage: UIImageView!

@IBOutlet weak var weight: UILabel!
@IBOutlet weak var price: UILabel!

@IBOutlet weak var addToCart: RoundButton!

var addActionHandler: (() -> Void)?

func configure(withItems items: Items) {
name.text = items.name
category.text = items.category
image.sd_setImage(with: URL(string: items.image))
price.text = items.price
weight.text = items.weight
}

@IBAction func addTapped(_ sender: UIButton) {
self.addActionHandler?()
}

}

Now, in your menu cellForRowAt you can provide the action handler:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell") as! MenuCell // Just crash at this point if there isn't a valid cell identifier configured
let item = itemSetup[indexPath.row]
cell.configure(withItem: item)
cell.addActionHandler = {
Cart.currentCart.items.append(item)
}
return cell
}

And that should be all you need to do - When you segue to the cart view controller, it will show the current contents of the cart.

Note that you could improve your cart data model somewhat by allowing it to have a quantity for each item and providing an add(item:) function that incremented the quantity if the item was in the cart



Related Topics



Leave a reply



Submit