how to handle two collection view with push segues - swift

I'm having two UICollectionView
#IBOutlet weak var collectionView1: UICollectionView!
#IBOutlet weak var collectionview2: UICollectionView!
i'm getting indexPath for each collection view separately with functions.
func getIndexPathForSelectedCell() -> NSIndexPath?
{
var indexPath:NSIndexPath?
if collectionview1.indexPathsForSelectedItems()!.count > 0 {
indexPath = collectionview1.indexPathsForSelectedItems()![0]
}
return indexPath
}
func getIndexPathForSelectedCell2() -> NSIndexPath?
{
var indexPath2:NSIndexPath?
if collectionView2.indexPathsForSelectedItems()!.count > 0 {
indexPath2 = collectionView2.indexPathsForSelectedItems()![0]
}
return indexPath2
}
I'm Performing segue for cell touch as follows.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if let indexPath = getIndexPathForSelectedCell()
{
let DealsdetailViewController = segue.destinationViewController as! DealsDetailViewController
DealsdetailViewController.Dealsdata = Dealsdata[indexPath.row]
}
else if let indexPath2 = getIndexPathForSelectedCell2()
{
let ContainerviewController = segue.destinationViewController as! ContainerViewController
ContainerviewController.BTdata = BTdata[indexPath2.row]
}
}
if i click on a cell in first collection view segue performs correctly, when i click on a cell in second collection view
i got error
in
let DealsdetailViewController = segue.destinationViewController as! DealsDetailViewController
which is first if statement condition value, i'm stuck here
please help me, how to handle performing both segue on cell click on each collection view.

Use the method from UICollectionView protocol
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = cellForItemAtIndexPath(indexPath)!
if collectionView == self.collectionView1 {
self.performSegueWithIdentifier("segue1", sender: cell)
} else if collectionView == self.collectionView2 {
self.performSegueWithIdentifier("segue2", sender: cell)
}
}
func prepareForSegue(segue: UIStoryBoardSegue, sender: AnyObject?) {
if segue.identifer == "segue1" {
let detailVC:DetailViewController = segue.destinationViewController as DetailViewController
// Your sender is cell. You have indexPath of them and can get his identity in dataSource.
//detailVC.name = ...
//detailVC.surname = ...
} else if segue.identifier == "segue2" {
//...
}
}

Related

How to input the value from UI button in Teble View cell to the variable declared in Table View Controller?

I want to save the value of filled button(●) into the array "q.answer[indexPath.row]" about each question "q.question[indexPath.row]).
currentQuizButtonIndex is currently renewed every time when ◯ changes to ● by tapping. However, I have no idea how to save in to variable q which is declared in TableViewController.
View Controller display
Code about QuizCell.swift (TableCell which is about 5 buttons and UIlabel.)
import UIKit
import Foundation
protocol QuizCellDelegate {
func quizCellDidChangeCurrentButtonIndex(_ cell: QuizCell, index: Int)
}
class QuizCell: UITableViewCell {
var currentQuizButtonIndex: Int = 0 {
didSet {
let value = self.currentQuizButtonIndex
self.updateCurrentQuizButton(value)
if let delegate = self.delegate {
delegate.quizCellDidChangeCurrentButtonIndex(self, index: value)
}
}
}
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet var answerButtons: [UIButton]!
var delegate: QuizCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
//print("ここまできてるか確認")
// Initialization code
}
#IBAction func didTapQuizButton(_ sender: UIButton) {
if let index = self.answerButtons.firstIndex(of: sender){
self.currentQuizButtonIndex = index
delegate?.quizCellDidChangeCurrentButtonIndex(self, index: index)
print(index)
}
}
private func updateCurrentQuizButton(_ currentIndex: Int){
for (index, answerButton) in self.answerButtons.enumerated(){
if index == currentIndex {
answerButton.setTitle("●", for: .normal)
} else {
answerButton.setTitle("○", for: .normal)
}
}
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Code about View Controller
import UIKit
class AnswerQuizViewController: UIViewController, UITableViewDelegate {
var q: QuestionSeries!
#IBOutlet weak var quizTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
quizTableView.dataSource = self
quizTableView.delegate = self
// cell xibファイルを使うときは書く必要があるやつ。
// quizTableView.register(UINib(nibName: K.Cells.QuizCellNibName, bundle: nil), forCellReuseIdentifier: K.Cells.QuizCellIdentifier)
quizTableView.register(UINib(nibName: "QuizCell", bundle: nil), forCellReuseIdentifier: "QuizCellIdentifier")
// Do any additional setup after loading the view.
}
// 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.destination.
// // Pass the selected object to the new view controller.
//// if segue.identifier == K.Segue.checkResult {
//// let resultViewController = segue.destination as! ResultViewController
//// answerQuizViewController.q =
//// print(answerQuizViewController.q)
// }
}
// MARK: - quizTableViewのアレンジ
extension AnswerQuizViewController: UITableViewDataSource, QuizCellDelegate {
func quizCellDidChangeCurrentButtonIndex(_ cell: QuizCell, index: Int) {
if let indexPath = self.quizTableView.indexPath(for: cell){
self.q.question[indexPath.row].answer = index
print(index)
}else{
print("ここきてます")
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return q.question.count
//print(q.question.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let question = q.question[indexPath.row]
let cell = quizTableView.dequeueReusableCell(withIdentifier: K.Cells.QuizCellIdentifier, for: indexPath) as! QuizCell
cell.questionLabel.text = question.text
// print(question.text)
return cell
}
}
It is also helpful if you have any idea of implementing this by alternative way.
Thanks.
How about you create a static array and store your data into that array.
when the button is tapped you can append it into that static array.
Create a new file. Just a basic "Swift file".
struct structName {
static var qArray: [String] = []
}
Then append data by:
structName.q.append()
Finally get your data trough:
structName.q[index]

TextFields don't get updated with values in unwind function Swift

I have textfields that should get a value displayed once returning from a TableViewController where user selects a cell. I get that value in unwindfunction, but textfieldsdon't get updated. When printing the value it prints correctly on unwinding, so unwindshould be set correctly, but it just don't get displayed in it's textfield. I also tried prepare(for unwind:in TableViewControllerbut with same results. Can you see what I'm doing wrong?
As always many thanks.
unwind function:
#IBAction func unwindToDetailsVc(segue: UIStoryboardSegue) {
//Insert function to be run upon dismiss of VC2
print("unwindSegue triggered")
if let vc = segue.source as? CityTableViewController {
print("segue source is city vc : \(String(describing: vc.city!))")
self.cityTextField.text = vc.city
}
if let vc = segue.source as? RegionTableViewController {
print("segue source is region vc : \(String(describing: vc.region!))")
self.regionTextField.text = vc.region
}
if let vc = segue.source as? CountryTableViewController {
print("segue source is country vc : \(String(describing: vc.country!))")
self.countryTextField.text = vc.country
}
}
didSelect in TableView:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CityTableViewCell
self.city = cell.cityLabel.text ?? ""
performSegue(withIdentifier: "unwindSegue", sender: self)
// self.dismiss(animated: true, completion: nil)
}
prepare for unwind:
override func prepare(for unwind: UIStoryboardSegue, sender: Any?) {
if unwind.identifier == "unwindSegue" {
if let detailsVc = unwind.destination as? ShopDetailsTableViewController {
detailsVc.cityTextField.text! = city
}
}
}
textfield delegate function:
func textFieldDidBeginEditing(_ textField: UITextField) {
print("Editing textfield")
if textField.accessibilityIdentifier == "city" {
print("Editing city textfield")
performSegue(withIdentifier: "citySegue", sender: self)
} else if textField.accessibilityIdentifier == "region" {
print("Editing regio textfield")
performSegue(withIdentifier: "regionSegue", sender: self)
} else if textField.accessibilityIdentifier == "country" {
print("Editing country textfield")
performSegue(withIdentifier: "countrySegue", sender: self)
}
}
You can simply use a closure to solve this kind of problem statement,
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
#IBAction func openTableVC(_ sender: UIButton) {
if let controller = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TableViewController") as? TableViewController {
controller.handler = {[weak self](city) in
DispatchQueue.main.async {
self?.textField.text = city
}
}
self.navigationController?.pushViewController(controller, animated: true)
}
}
}
class TableViewController: UITableViewController {
var handler: ((String)->())?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let city = "Delhi"
self.handler?(city)
self.navigationController?.popViewController(animated: true)
}
}
The above code is generic and will work in every case from where you want to open TableViewController.
I finally found out what I was doing wrong, I was calling the functions loading from CoreDatauser details and displaying them in viewWillAppear. Once I moved them in saveButtonafter the saving to CoreDatafunction call, it all works as expected. Textfield get updated with select values from tableviews.
Many thank to #PGDev for sharing a more convenient way of coding this, without all the if elsestatements and unwinds. Great example of higher level coding.

how to deal with alamofire's async?

I am trying to save the data I got from the JSON fire however, because of Alamofire's async nature I dont get the data I need instantly but only when I tap on the tableviewcell again (and the data is wrong too)
I am wondering what I should do here so that when I tap the tableviewcell it will get the data I needed (instead of empty arrays)
Here's my code:
class CurrencyExchangeViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
let exchange = currencyExchangeModel()
var past30DaysDateValueToPass = [String]()
var past30DaysPriceValueToPass = [Double]()
var valueToPass = ""
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath) as? CurrencyExchangeTableViewCell
valueToPass = Array(self.exchange.currencyToGetExchangesDictionary.keys)[indexPath.row]
self.getPastData(currency: valueToPass)
performSegue(withIdentifier: "currencyHistorySegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "currencyHistorySegue") {
var viewController = segue.destination as? CurrencyHistoricalDataViewController
viewController?.historicalCurrency = valueToPass
viewController?.past30DaysPrice = self.exchange.currencyPast30DaysPriceArray
viewController?.past30DaysDate = self.exchange.currencyPast30DaysDatesArray
}
}
func getPastData(currency: String){
Alamofire.request("https://api.coindesk.com/v1/bpi/historical/close.json?currency=\(currency)").responseJSON{ (responseData) in
if responseData.result.value != nil {
let responseJSON = JSON(responseData.result.value)["bpi"].dictionaryObject
self.exchange.currencyPast30DaysDatesArray = Array(responseJSON!.keys)
self.exchange.currencyPast30DaysPriceArray = Array(responseJSON!.values) as! [Double]
}
}
}
}
If you want performSegue execute after getting data from Alamofire then remove
performSegue(withIdentifier: "currencyHistorySegue", sender: self)
this line to from didselect and place at getPastData.
func getPastData(currency: String){
Alamofire.request("https://api.coindesk.com/v1/bpi/historical/close.json?currency=\(currency)").responseJSON{ (responseData) in
if responseData.result.value != nil {
let responseJSON = JSON(responseData.result.value)["bpi"].dictionaryObject
self.exchange.currencyPast30DaysDatesArray = Array(responseJSON!.keys)
self.exchange.currencyPast30DaysPriceArray = Array(responseJSON!.values) as! [Double]
performSegue(withIdentifier: "currencyHistorySegue", sender: self)
}
}
This will help.

Search bar will only show first Cell in detail view

I have created a TableView with a Search Bar in Swift.
I have created 3 searchable objects. (131, 132, 137)
When i search for 137 and press 137(which is the only option to press), i still get to 131 detailView. Anyone know why? Thanks
Here is my code:
import UIKit
class TableViewController: UITableViewController, UISearchResultsUpdating {
let TitleList = ["131","132","137"]
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = self.resultSearchController.searchBar
self.tableView.reloadData()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell
cell.LabelTitle.text = TitleList[indexPath.row]
cell.CellDescription.text = DescriptionList[indexPath.row]
let imagename = UIImage(named: ImageList[indexPath.row])
cell.CellImage.image = imagename
if self.resultSearchController.active {
cell.LabelTitle?.text = self.filteredfarger[indexPath.row]
}
else {
cell.LabelTitle?.text = self.TitleList[indexPath.row]
}
return cell
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
self.filteredfarger.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %#", searchController.searchBar.text!)
let array = (self.TitleList as NSArray).filteredArrayUsingPredicate(searchPredicate)
self.filteredfarger = array as! [String]
self.tableView.reloadData()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "DetailView") {
let VC = segue.destinationViewController as! DetailedViewController
if let indexpath = self.tableView.indexPathForSelectedRow {
let Title = TitleList[indexpath.row] as String
VC.SentData1 = Title
}
Because when you do the search the row for 137 is 0, and then in prepareForSegue you use the tableView.indexPathForSelectedRow (which means indexPath.row == 0) to get the title data. Try doing something like in your tableView.cellForRowAtIndexPath function where you check if resultSearchController is active or not, and if it is use filteredfarger instead of TitleList. Something like this maybe:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "DetailView") {
let VC = segue.destinationViewController as! DetailedViewController
if let indexpath = self.tableView.indexPathForSelectedRow {
if self.resultSearchController.active {
let Title = self.filteredfarger[indexpath.row]
VC.SentData1 = Title
}
else {
let Title = TitleList[indexpath.row] as String
VC.SentData1 = Title
}
}
}
}
Better yet, you already give the cell some info, why not just use that, kinda like this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "DetailView") {
let VC = segue.destinationViewController as! DetailedViewController
if let cell = sender as? TableViewCell {
VC.SentData1 = cell.LabelTitle.text
}
}
}

From collectionView to Detail

Im passing data from one CollectionViewController to a detail ViewController. Everything seems to be working, but the "prepareForSegue is happening before the "didSelectItemAtIndexPath" so it's passing the data of the item I selected the last time not the one I'm selecting right now.
This is my wrong code for the collectionViewController
var cellSelected : Int = 0
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
cellSelected = indexPath.row
println("Did select!")
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "identifierName" {
var secondScene = segue.destinationViewController as detailVC
secondScene.authorDetail = data[cellSelected].userOfImg
secondScene.likesDetail = data[cellSelected].likesInImg
secondScene.imageDetail = data[cellSelected].imgPath
println("Prepare for segue")
}
}
The result in the debug area is:
Prepare for segue
Did select!
I've found a solution for this. It's to use shouldSelectItemAtIndexPath instead of didSelectItemAtIndexPath. Doing this the shouldSelectItemAtIndexPath will be executed before the prepareForSegue and will fix the problem of sending the data of the item you are selecting and not the one selected the last time.
Right solution
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "identifierName" {
var secondScene = segue.destinationViewController as detailVC
let cell = sender as UICollectionViewCell
let indexPath = collectionView.indexPathForCell(cell)
secondScene.authorDetail = data[indexPath.row].userOfImg
secondScene.likesDetail = data[indexPath.row].likesInImg
secondScene.imageDetail = data[indexPath.row].imgPath
println("Prepare for segue")
}
}