How to perform a segue view from custom UITableViewCell(xib) to another ViewController - ios

I want to show a button on my custom UITableViewCell which takes the user to another screen on tapping on it.
I have tried following code but it doesn't work
Child view:
#IBAction func childScreenButton(sender: AnyObject) {
if let delegate = self.delegate {
delegate.childButtonClickedOnCell(self)
}
}
Protocol:
protocol childTableCellDelegate: class {
func childButtonClickedOnCell(cell: childViewCell)
}
Parent ViewController:
func childButtonClickedOnCell(cell: FeedChildViewCell) {
self.clickedIndexPath = self.feedsTableView.indexPathForCell(cell)
self.performSegueWithIdentifier("toNextScreen", sender: self)
}
while I'm testing the break point doesn't enter into "delegate.childButtonClickedOnCell(self)" on child view. Please let me know if am doing anything wrong here. Thanks!!

I suspect you've got a couple things out of place, or not defined just right.
I just ran a quick test with this, and the delegate call works fine... see if you notice anything not-quite-the-same...
//
// TestTableViewController.swift
//
// Created by DonMag on 4/7/17.
// Copyright © 2017 DonMag. All rights reserved.
//
import UIKit
protocol MyCellDelegate {
func pressedButtonForMyCell(theSender: MyCell)
}
class MyCell: UITableViewCell {
#IBOutlet weak var theLabel: UILabel!
#IBOutlet weak var theButton: UIButton!
var delegate: MyCellDelegate?
#IBAction func childScreenButton(sender: AnyObject) {
delegate?.pressedButtonForMyCell(theSender: self)
}
}
class TestTableViewController: UITableViewController, MyCellDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyCell
cell.theLabel.text = "\(indexPath)"
cell.delegate = self
return cell
}
func pressedButtonForMyCell(theSender: MyCell) {
print("delegate called", theSender)
}
}

Related

How to append to an array on another view controller

I have an array in a tableView that shows that contents of that array, and I am able to append values to the array. the problem is that whenever I go to another page, it automatically empties, but I want the values to stay there. Here is my code for the first view controller:
// AddFoodViewController.swift
// grosseries
//
// Created by Amish Tyagi on 6/2/20.
// Copyright © 2020 grosseries. All rights reserved.
//
import UIKit
class AddFoodViewController: UIViewController {
#IBOutlet weak var foodTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func doneTapped(_ sender: Any) {
}
func transitionToNext() {
let homeViewController = storyboard?.instantiateViewController(identifier: "TableViewController") as? TableViewController
view.window?.rootViewController = homeViewController
view.window?.makeKeyAndVisible()
}
// 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?) {
if (segue.identifier == "toTableViewController") {
let homeViewController = segue.destination as? TableViewController
homeViewController?.food.append(foodTextField.text!)
view.window?.rootViewController = homeViewController
}
}
}
Here is the code for my second view controller:
// TableViewController.swift
// grosseries
//
// Created by Amish Tyagi on 5/29/20.
// Copyright © 2020 grosseries. All rights reserved.
//
import UIKit
class TableViewController: UIViewController{
#IBOutlet var tableView: UITableView!
var food : [String]! = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view.
}
#IBAction func addItemTapped(_ sender: Any) {
transitionToNext()
}
func transitionToNext() {
let nextViewController = storyboard?.instantiateViewController(identifier: "AddFoodViewController") as? AddFoodViewController
view.window?.rootViewController = nextViewController
view.window?.makeKeyAndVisible()
}
}
extension TableViewController : UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("you tapped me :)")
}
}
extension TableViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return food.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = food[indexPath.row]
return cell
}
}
Here is a picture of my storyboard layout:
Any Help would be greatly appreciated!
In the storyboard, select your TableViewController storyboard and select Editor->Embed in->Navigation Controller:
Below your tableView (or anywhere else you'd like) add a button with a segue to show the AddFoodViewController.
Now, on your AddFoodViewController add a button to confirm the food you put on textField:
#IBAction func confirmAddedFood(_ sender: Any) {
guard let tableViewVC = navigationController?.viewControllers.first as? TableViewController else { return }
tableViewVC.food.append(foodTextField.text!)
navigationController?.popViewController(animated: true)
}
You don't need the food array on your AddFoodViewController anymore, you still need the one on TableViewController though.
Don't forget to reload the tableView when you go back to it after adding a food, in your TableViewController add:
override func viewWillAppear(_ animated: Bool) {
tableView.reloadData()
}
Your TableViewController:
class TableViewController: UIViewController{
#IBOutlet var tableView: UITableView!
var food: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
// In the case when not using prototype cell.
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.dataSource = self
}
override func viewWillAppear(_ animated: Bool) {
tableView.reloadData()
}
}
extension TableViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return food.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = food[indexPath.row]
return cell
}
}
And FoodViewController:
class AddFoodViewController: UIViewController {
#IBOutlet weak var foodTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func confirmAddedFood(_ sender: Any) {
guard let tableViewVC = navigationController?.viewControllers.first as? TableViewController else { return }
tableViewVC.food.append(foodTextField.text!)
navigationController?.popViewController(animated: true)
}
}
It's not very cool to just give the already done code, but I think in this case is just a silly mistake, whether from you or me.

I can't display TableView in added subview

I have a ViewController named LiveMatchVC. I want to show another ViewController(named LiveMatchMomentsVC and it has a TableView) in LiveMatchVC.
I added a subview and I displayed LiveMatchMomentsVC in LiveMatchVC, but I can't view the TableView of the LiveMatchMoments in LiveMatchVC.
My codes and screenshots are as below.
How can I solve this problem?
White area is my TableView in LiveMatchMomentsVC
LiveMatchVC Codes
class LiveMatchVC: UIViewController {
//---------Outlets-----------
#IBOutlet weak var imgLiveMatchScoreBG: UIImageView!
#IBOutlet weak var openLeft: UIBarButtonItem!
let titleCont1 = TZSegmentedControl(sectionTitles: ["Maç Raporu","Canlı Anlatım", "Önemli Olaylar", "İstatistikler", "Kadrolar" ])
override func viewDidLoad() {
super.viewDidLoad()
let liveMatchMomentsVC = storyboard?.instantiateViewController(withIdentifier: "LiveMatchMomentsVC") as! LiveMatchMomentsVC
titleCont1.indexChangeBlock = { (index) in
if self.titleCont1.selectedSegmentIndex == 1 {
self.view.addSubview(liveMatchMomentsVC.view)
liveMatchMomentsVC.view.frame = CGRect(x: 0, y: self.imgLiveMatchScoreBG.frame.height+self.titleCont1.frame.height, width: self.view.frame.width, height: self.view.frame.height-44)
self.addChildViewController(liveMatchMomentsVC)
self.view.addSubview(liveMatchMomentsVC.view)
self.didMove(toParentViewController: self)
}
}
}
}
LiveMatchMomentsVC Codes
import UIKit
class LiveMatchMomentsVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tblLiveMatch: UITableView!
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.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 15
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MatchEvent", for: indexPath) as! MatchEventTableViewCell
cell.lblMatchEventTime.text = "69"
cell.lblMatchEvent.text = "Goal. Juraj Kucka"
return cell
}
}

Can you help solve data.entry.init issue?

I'm getting the "thread 1: exc_bad_access (code=2, address=0x16fc0bfefe8) error. I have set up the iOS app to access a Table View Controller after logging in. It should display images. I have been following this tutorial http://shrikar.com/uitableview-and-uitableviewcell-customization-in-swift/ but no luck. This is my data.swift file where the error originates from:
import Foundation
class Data {
class Entry: Data {
let filename : String
init(fname : String) {
self.filename = fname
}
}
let products = [
Entry(fname: "placeholder1"), // <- Thread 1 error code shows in this line
Entry(fname: "placeholder2"),
Entry(fname: "placeholder3")
]
}
HomeTableViewController.swift
import UIKit
class HomeTableViewController: UITableViewController {
let data = Data()
override func viewDidLoad() {
super.viewDidLoad()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.products.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! HomeTableViewCell
let entry = data.products[indexPath.row]
let image = UIImage(named: entry.filename)
cell.bkImageView.image = image
return cell
}
HomeTableViewCell.swift
import UIKit
class HomeTableViewCell: UITableViewCell {
#IBOutlet weak var bkImageView: 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
}
}
Humbly asking for some assistance. Thanks in advance.
The issue is this line:
class Entry: Data {
Since Entry inherits from Data when you create an instance of Data the program goes into an infinite loop of initializing both the first member of products (an Entry) and its parent class, Data. Instead it should be this, as per the tutorial:
class Entry {

Swift tableview in popover doesn't show data

I am trying to setup a popover view containing a textfield and a tableview, but I couldn't make the tableview to show the data. It would be much appreciated if you could help me on this.
On the main ViewController, I put a label to trigger the segue of popover,
import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
#IBOutlet weak var textField: UITextField!
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.
}
#IBAction func popover(sender: AnyObject) {
self.performSegueWithIdentifier("ShowDetails", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowDetails" {
if let controller = segue.destinationViewController as? UIViewController {
controller.popoverPresentationController!.delegate = self
controller.preferredContentSize = CGSize(width: 320, height: 50)
}
}
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None
}
}
A PopoverCell is setup for the prototype cell,
import UIKit
class PopoverCellTableViewCell: UITableViewCell {
#IBOutlet weak var AreaCellLabel: 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
}
}
A PopoverControllerView is set for the popover itself.
import UIKit
class PopoverViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate {
var areas = [Area]()
// #IBOutlet weak var NewArea: UITextField!
// #IBOutlet weak var SaveNewArea: UIButton!
#IBOutlet weak var subtableView: UITableView!
override func viewDidLoad() {
subtableView.dataSource = self
subtableView.delegate = self
super.viewDidLoad()
LoadsampleArea()
// Do any additional setup after loading the view.
}
func LoadsampleArea () {
let area1 = Area(AreaName:"Moountain")!
let area2 = Area(AreaName:"ByHill")!
let area3 = Area(AreaName:"Yard")!
areas += [area1, area2, area3]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return areas.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "AreaCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! PopoverCellTableViewCell
let area = areas[indexPath.row]
cell.AreaCellLabel.text = area.AreaName
dismissViewControllerAnimated(true, completion:nil)
return cell
}
}
and a simple data file to put the data.
import UIKit
class Area {
var AreaName: String
init? (AreaName: String) {
self.AreaName = AreaName
if AreaName.isEmpty {
return nil
}
}
}
Where is your UITableView object in your PopoverViewController class ? I can't see any reference to it.
Maybe your didn't copy-paste it since the textfield is commented too, in this case I'll suggest to check if the delegate an datasource are set property.

How to use delegates to communicate data from a custom cell to a label in the parent view

I have figured out how to pass data between views with delegates in other situations but this one is stumping me.
In this example I am trying to send data resulting from pressing a button, up to the label using a delegate pattern but without any success. My guess is that I am missing something fundamental here and I haven't found any examples that deal with delegates in quite this way.
//
// ViewController.swift
// TableCellDelegate
//
// Created by Chris Cantley on 6/1/15.
// Copyright (c) 2015 Chris Cantley. All rights reserved.
//
import UIKit
class ViewController: UIViewController, CellInfoDelegate {
var cellViewController = CellViewController()
//The place to put the number into.
#IBOutlet weak var sumLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
cellViewController.delegate = self
}
//2)...to here.
func processThatNumber(theNumber: Int) {
println("out : \(theNumber)")
}
}
// Table View delegates
extension ViewController: UITableViewDataSource, UITableViewDelegate
{
//One row
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
// Load custom cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("thisCustomCell", forIndexPath: indexPath) as! CellViewController
return cell
}
}
//-------------------- Protocol for Delegate -----------------------
protocol CellInfoDelegate {
func processThatNumber(theNumber: Int)
}
//-------------------- Cell to Pass info to Parent -----------------------
class CellViewController: UITableViewCell{
var sumNumber: Int = 0
var delegate: CellInfoDelegate?
#IBAction func addButton(sender: AnyObject) {
// increment that number
self.sumNumber += 5
//1) I want to get it from here...... but delegate ends up nil
if let delegate = self.delegate {
delegate.processThatNumber(self.sumNumber)
}
//Shows that the number is incrementing
println(sumNumber)
}
}
The ViewController and CellViewController are connected to their respective classes
Thanks in advance.
You should set the delegate here:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("thisCustomCell", forIndexPath: indexPath) as! CellViewController
cell.delegate = self // <-- Set the delegate.
return cell
}
Thanks to i_am_jorf for the solution, here is the code that works.
//
// ViewController.swift
// TableCellDelegate
//
// Created by Chris Cantley on 6/1/15.
// Copyright (c) 2015 Chris Cantley. All rights reserved.
//
import UIKit
import Foundation
class ViewController: UIViewController, CellInfoDelegate {
//The place to put the number into.
#IBOutlet weak var sumLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
//2)...to here.
func processThatNumber(theNumber: Int) {
println("out : \(theNumber)")
self.sumLabel.text = toString(theNumber) as String
}
}
// Table View delegates
extension ViewController: UITableViewDataSource, UITableViewDelegate
{
//One row
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
// Load custom cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("thisCustomCell", forIndexPath: indexPath) as! CellViewController
//SOLUTION : put the Delgate HERE in the place where the cell is instantiated so that there is a connection back
// to this class from the Cell class
cell.delegate = self
return cell
}
}
//-------------------- Protocol for Delegate -----------------------
protocol CellInfoDelegate {
func processThatNumber(theNumber: Int)
}
//-------------------- Cell to Pass info to Parent -----------------------
class CellViewController: UITableViewCell{
var sumNumber: Int = 0
var delegate: CellInfoDelegate?
#IBAction func addButton(sender: AnyObject) {
// increment that number
self.sumNumber += 5
//1) I want to get it from here...... but delegate ends up nil
if let delegate = self.delegate {
delegate.processThatNumber(self.sumNumber)
}
//Shows that the number is incrementing
println(sumNumber)
}
}
Do you need to use Delegates?
What if you have this function output a number:
func processThatNumber(theNumber: Int) -> Int {
println("out : \(theNumber)")
return theNumber
}
Then set the text on the label using the button:
#IBAction func addButton(sender: AnyObject) {
self.sumNumber += 5
sumLabel.text = "\(processThatNumber(self.sumNumber))"
println(sumNumber)
}
Would that work for you?

Resources