Segue from TableView into another Tableview using gesture - ios

I have FirstTableView (TableView) which is populated by cells that have an image and label. If users tap the label, I want to segue into another tableview (SecondTableView), which will use the label from the previous tableview as a path(foldername) to search for images within that folder and display them as cells.
I have bits and pieces of the code written together, but I'm not quite sure where to pass the perform segue and prepare for segue functions.
Here is the extension of my FirstTableViewController, which populate the cells of the FirstTableView:
FirstTableView
extension FirstTableViewController: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataCells.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let folderCell = dataCells[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "FirstTableCell") as! FirstTableViewCell
cell.setCell(cell:folderCell)
return cell
}
}
Here is the code for the FirstTAbleViewCell. Notice that I've created the UITapGestureRecognizer constant and a protocol. If the FolderLabel is clicked, I want to segue into the next tableview.
func setCell is just the function to populate the FirstTableViews.
FirstTableViewCells
import UIKit
protocol LabelCellDelegate {
func func1(sender:Any)
}
class FirstTableViewCell: UITableViewCell {
#IBOutlet weak var ImageView: UIImageView!
#IBOutlet weak var FolderLabel: UILabel!
var delegate: LabelCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
FolderLabel.isUserInteractionEnabled = true
let recognizer = UITapGestureRecognizer(target: self, action:Selector(("handleTap:")))
recognizer.numberOfTapsRequired = 1
self.FolderLabel.addGestureRecognizer(recognizer)
}
func handleTap(recognizer: UITapGestureRecognizer){
delegate!.func1(sender: self)
}
func setCell (cell : FolderCell){
self.ImageView.image = cell.image
self.FolderLabel.text = cell.label
}
}
Next is the SecondTableViewController.
Somehow, after the tap of the cell from the FirstTableView I want to get the label name that was tapped, and use that name as a directory to search.
Grab all the images within the directory and display them as images in cells in SecondTableView. I'm not quite sure where I should be calling the gesture function and which TableViewController should be the delegate of the cell protocol.
SecondTableView
import UIKit
class SecondTableViewController: UIViewController {
var Imagefiles: [String] //Imagefiles String. This are all images from tapped folder label.
var CellObjects: [folderCell] // Cellobjects to fill the cells. contains images and image names.
// need to get folder name from FirstTableViewCell how?
override func viewDidLoad() {
super.viewDidLoad()
imageFiles = getImageObjects(folder: Foldercell) //how to get access to folderCell???????
cellObjects = createArray(imageFiles: imageFiles)
}
func getImageObjects(folder: FolderCell) -> imageFiles: String {
//FUNCTION To search tapped label and grab all image file paths.
func createArray(imageFiles: string)-> tempArray: [folderCell]{
//We use this function to take the image files from above function and
//create cell objects to populate our cells
//in SecondTableView
}
}
}
}
extension ViewController: LabelCellDelegate{
func1(folderLabel : String) {
return folderLabel
}
}
extension LablesTableView:UITableViewDataSource,UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return CellObjects.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellFolderObject = folderObjects[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "SecondCell") as! LabelCell
let folderCell = CellObjects[indexPath.row]
cell.setCell(cell: folderCell)
return cell
As you can see, I haven't called perform segue and prepare for segue. I'm not sure where I should be calling them.
I'm also unsure about where the delegate to the gesture protocol should be.
Thank you.

Inside cellForRowAt set
cell.delegate = self
//
then inside this delegate function of the VC ( conform to the protocol LabelCellDelegate )
extension FirstTableViewController:LabelCellDelegate {
func func1(sender: FirstTableViewCell) {
self.performSegue(withIdentifier: "goToSecond", sender:sender.FolderLabel.text!)
}
}
//
the VC method is triggered from here
func handleTap(recognizer: UITapGestureRecognizer){
delegate!.func1(sender: self)
}
//
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let secondController = segue.destination as? SecondTableViewController {
secondController.sendedStr = sender as! String
}
}

Related

how to make sure that my for in loop enables me to perform only one segue?

I have a tableview inside a view controller, that is connected to 3 different view controller by a segue. Each one has an identifier.
In order to perform a segue to the corresponding view controller, I have created an array that contains the identifiers for each segue. like so :
var tableauSegues = ["versChapitre1", "versChapitre2", "versChapitre3"]
In order to manage the segue according to the index of the array, I chose to use a for in loop inside of an iB action, like this:
#IBAction func versHistoire(_ sender: UIButton) {
for i in tableauSegues {
performSegue(withIdentifier: i, sender: self)
}
}
the problem is that all 3 segues are performed. I only want to perform one segue at the time.
any idea ?
Obviously, you want to avoid looping, but instead want to just use tableauSegues[row]. But the trick is how to get the row.
Bottom line, you should let the cell handle the tapping of the cell, and have the cell tell the table view controller when its button was tapped. But the trick is that when it does that, it should supply a reference to itself when it does that, so that the view controller can determine which row had its button tapped.
Thus, remove your existing #IBAction directly between the button and the view controller, and instead, hook it up to an #IBAction in the cell’s base class.
// CustomCell.swift
import UIKit
protocol CustomCellDelegate: class {
func didTapButton(_ sender: Any, in cell: CustomCell)
}
class CustomCell: UITableViewCell {
#IBOutlet weak var customLabel: UILabel!
static let preferredReuseIdentifier = "CustomCell"
weak var delegate: CustomCellDelegate?
func configure(text: String, delegate: CustomCellDelegate) {
customLabel.text = text
self.delegate = delegate
}
#IBAction func didTapButton(_ sender: Any) {
delegate?.didTapButton(sender, in: self)
}
}
Note, when I configure that cell, I’m going to have the table view provide not only whatever values need to be displayed in the cell, but also indicate that it is the delegate for that cell:
// ViewController.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
var tableauSegues = ["versChapitre1", "versChapitre2", "versChapitre3"]
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableauSegues.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomCell.preferredReuseIdentifier, for: indexPath) as! CustomCell
cell.configure(text: "...", delegate: self)
return cell
}
}
extension ViewController: CustomCellDelegate {
func didTapButton(_ sender: Any, in cell: CustomCell) {
let row = tableView.indexPath(for: cell)!.row
let identifier = tableauSegues[row]
performSegue(withIdentifier: identifier, sender: self)
}
}

Table view cell elements not able to click and get data

I have one table view and inside that i placed one main view. And inside that main view i placed one button.And when ever use click on my cell button. I need to get the cell title label.This is what i need. But i tried following below code. Not sure what i am missing out. It not at all calling my cell.add target line.
Code in cell for row at index:
cell.cellBtn.tag = indexPath.row
cell.cellBtn.addTarget(self, action:#selector(self.buttonPressed(_:)), for:.touchUpInside)
#objc func buttonPressed(_ sender: AnyObject) {
print("cell tap")
let button = sender as? UIButton
let cell = button?.superview?.superview as? UITableViewCell
let indexPath = tableView.indexPath(for: cell!)
let currentCell = tableView.cellForRow(at: indexPath!)! as! KMTrainingTableViewCell
print(indexPath?.row)
print(currentCell.cellTitleLabel.text)
}
I even added a breakpoint, still it not at calling my cell.addTarget line
Tried with closure too. In cell for row at index:
cell.tapCallback = {
print(indexPath.row)
}
In my table view cell:
var tapCallback: (() -> Void)?
#IBAction func CellBtndidTap(_ sender: Any) {
print("Right button is tapped")
tapCallback?()
}
Here that print statement is getting print in console.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var list = [String]()
#IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
cell.saveButton.tag = indexPath.row
//cell.saveButton.accessibilityIdentifier = "some unique identifier"
cell.tapCallback = { tag in
print(tag)
}
return cell
}
}
class MyTableViewCell: UITableViewCell {
// MARK: - IBOutlets
#IBOutlet weak var saveButton: UIButton!
// MARK: - IBActions
#IBAction func saveTapped(_ sender: UIButton) {
tapCallback?(sender.tag)
}
// MARK: - Actions
var tapCallback: ((Int) -> Void)?
}
Actually this is not a good programming practice to add the button (which contains in table view cell) target action in view controller. We should follow the protocol oriented approach for it. Please try to under stand the concept.
/*This is my cell Delegate*/
protocol InfoCellDelegate {
func showItem(item:String)
}
/*This is my cell class*/
class InfoCell: UITableViewCell {
//make weak reference to avoid the Retain Cycle
fileprivate weak var delegate: InfoCellDelegate?
//Outlet for views
#IBOutlet var showButton: UIButton?
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
//This is the public binding function which will bind the data & delegate to cell
func bind(with: DataModel?, delegate: InfoCellDelegate?, indexPath: IndexPath) {
//Now the bind the cell with data here
//.....
//Assign the delegate
self.delegate = delegate
}
//Button action
#IBAction func rowSelected(sender: UIButton) {
self.delegate?.showItem(item: "This is coming from cell")
}
}
/*Now in your ViewController you need to just confirm the InfoCellDelegate & call the bind function*/
class ListViewController: UIViewController {
//Views initialisation & other initial process
}
//Table view Delegate & Data source
extension ListViewController: UITableViewDataSource, UITableViewDelegate {
/**
Configure the table views
*/
func configureTable() {
//for item table
self.listTable.register(UINib.init(nibName: "\(InfoCell.classForCoder())", bundle: nil), forCellReuseIdentifier: "\(InfoCell.classForCoder())")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "InfoCell") as! InfoCell
cell.bind(with: DataModel, delegate: self, indexPath: indexPath)
return cell
}
}
extension ListViewController: InfoCellDelegate {
func showItem(item) {
print(item)
}
}

how to add label text to tableViewCell

I'm practicing creating an app where I have a label that gets its text from an UITextField when the user presses a button. Now, I added another button and a tableview and I want to be able to "save" the label's text to the table cells with the same mechanism of stopwatch's laps.
So, to be clear, I want the button to transfer the label's text to the table view cells each time I press it.
After your save button, you need to store the texts somewhere and reload the table. (Or insert it with animation)
class ViewController: UIViewController {
#IBOutlet private var textField: UITextField!
#IBOutlet private var tableView: UITableView!
var texts: [String] = [] {
didSet { tableView.reloadData() }
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "SimpleCell")
tableView.dataSource = self
}
#IBAction func saveButtonTapped(_ sender: UIButton) {
guard let newText = textField.text else { return }
self.texts.append(newText)
}
}
And in tableView dataSource methods:
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return texts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SimpleCell", for: indexPath)!
cell.textLabel?.text = texts[indexPath.row]
return cell
}
}

Swift: How to use "prepareForSegue" inside UICollectionViewCell?

I have a UICollectionView inside a UITableView, I want to link another ViewController when the UICollectionViewCell is tapped by user. Inside the UITableViewCell, I downloaded all the data from the Internet and stored in an array. I created a Segue by control drag the CollectionViewCell to the new ViewController and chose "Push". Inside the new ViewController, I added an IBOutlet of UIImageView to show the image of the CollectionViewCell when user tapped the thumbnail. Now when I tapped the CollectionViewCell, it goes to the new ViewController, but there is no pics showed. here is my code, any suggestions?
In the UITableViewConroller:
import UIKit
class HomePageTableViewController: UITableViewController {
let sections: NSArray = ["latest news", "good news"]
var posts1: [Posts] = [Posts]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section < sections.count{
return sections[section] as? String
}
return nil
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sections.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 && indexPath.section == 0 {
let tableViewCell = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as! TableViewCell
tableViewCell.getCategories()
// tableViewCell.scrollToNextCell()
// tableViewCell.startTimer()
return tableViewCell
}
and inside TableViewCell, here is the code:
import UIKit
import Alamofire
import AlamofireImage
class TableViewCell: UITableViewCell, UICollectionViewDataSource,
UICollectionViewDelegate {
var posts1: [Posts] = [Posts]()
var posts2: [Posts] = [Posts]()
var categories: [Category] = [Category]()
var selectedPost1: Posts?
#IBOutlet weak var theCollectionView: UICollectionView!
I download all the data from JSON and put them inside these arrays. inside this class, I also tried to call the function :
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("selected")
self.selectedPost1 = self.posts1
}
the print works when I tapped a cell, but I can't use "prepareForSegue" inside this function. I checked and found out that this function can only be used in ViewController, but how can I do that?
And inside the new ViewController, here is the code:
import UIKit
class DetailViewController: UIViewController {
#IBOutlet weak var postImageView: UIImageView!
var posts: [Posts] = [Posts]()
var selectedPost: Posts?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
if let post = self.selectedPost{
let thumbnailUrlString = post.postThumbnailUrlString
let imageUrl: NSURL = NSURL(string: thumbnailUrlString)!
print(thumbnailUrlString)
postImageView.af_setImageWithURL(imageUrl)
}
}
}
Since when I download the data, I put them into 2 different array, posts1 and posts2. I want to show the corresponding images in the new ViewController. But I just created one "selectedPost" array, I am not sure is that the problem? and I am not sure if I reload the data to the "selectedPost" array, maybe thats why there is no images showing? really hope anyone can give me some suggestions. thanks
You should use prepareForSegue:. What you need to do is have a var selectedIndexPath which is set whenever you select a cell. Then in prepareForSegue: you can access the cell and it's properties i.e:
HomePageTableViewController: UITableViewController {
var selectedIndexPath: NSIndexPath?
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
selectedIndexPath = indexPath
}
public override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
super.prepareForSegue(segue, sender: sender)
let cell = theCollectionView.cellForItemAtIndexPath(selectedIndexPath)
//do what you need to do
}
}

How do I send specific data based on which cell is clicked?

I have a tableview with a bunch of concerts, and when x cell is clicked, I want the artist of that concert to populate the new tableView. Below is the code for the first view controller (the view controller with all the concerts).
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView1: UITableView!
var arrayOfConcerts: [concert] = [concert]()
override func viewDidLoad()
{
super.viewDidLoad()
self.setUpConcerts()
self.tableView1.rowHeight = 145.0
}
func setUpConcerts()
{
var ACL = concert(imageName: "ACL2015.png")
let Landmark = concert(imageName: "Landmark.png")
let BostonCalling = concert(imageName: "BostonCalling.png")
let Lolla = concert(imageName: "Lolla.png")
arrayOfConcerts.append(ACL)
arrayOfConcerts.append(Landmark)
arrayOfConcerts.append(BostonCalling)
arrayOfConcerts.append(Lolla)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayOfConcerts.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = self.tableView1.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
let concerts = arrayOfConcerts[indexPath.row]
cell.setCell(concerts.imageName)
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("concertartist", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
}
}
Below is the code for the Artist View Controller (the second
viewcontroller). This tableView should be populated with specific
artists.
How would I go about doing that?
class ArtistConcerts: UIViewController, UITableViewDataSource, UITableViewDelegate {
var arrayOfArtists: [artist] = [artist]()
#IBOutlet weak var tableViewArtists: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
self.setUpArtists()
}
func setUpArtists()
{
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayOfArtists.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableViewArtists.dequeueReusableCellWithIdentifier("", forIndexPath: indexPath) as! CustomCell
let artists = arrayOfArtists[indexPath.row]
cell.setCell(artists.imageNameArtist)
return cell
}
}
You need to do this in the prepareForSegue method.
let vc = segue!.destinationViewController as! ArtistConcerts
if let rowSelected = tableView.indexPathForSelectedRow()?.row {
vc.arrayOfArtists = artistsPerforming[0]//this is an array
}
then, set whatever data you need to actually populate the tableView in that new view controller. I am not 100% sure on what you are trying to do though. Does my answer make sense?
you can send data once the cell is pressed to another viewcontroller by using didSelectRowAtIndexPath or prepareForSegue or didSelectRowAtIndexPath + prepareForSegue
let's say you are using prepareForSegue, things you will need
the segue identifier name
cast the destinationController to the controller you want to send the data
the indexPath.row where the cell was selected
then set the variable or variables or data structure that should receive data
Create a segue when you pressed CTRL + Drag from your cell into another viewcontroller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "Name_Of_Segue"
{
let destination = segue.destinationViewController as! NAME_Of_ViewController
let indexPath = mytableview.indexPathForSelectedRow!
let data = arrayOfArtists[indexPath.row]
destination.artist = data
}
mytableview is an IBOutlet from your tableview
artist is a variable that is declared in your destination controller
destination.artist = data this line of code is passing data from the specific cell then send it to your destination cell

Resources