Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2.1 - Using UITableView for a single cell

I want to achieve something similar to the following example image where I want to present a selected or default category on the first screen and when it's clicked it moves to the next screen with other category options to choose.

In this example, where it says Entertainment and Project 01 represent different data entities (data store) and it looks like it's using UITableView with each UITableViewCell connected to different data store. I first want to know if my analysis so far is correct.

In my case, I just need to do that Entertainment part and in the next screen, show all category options like the second screen in the example. And after selection is made, the first screen should reflect the selection from the second screen.

Is UITableView the right choice to show this single field (cell) and segue to another ViewController?

All the demo app examples I see, don't demonstrate the usage of UITableView for this purpose so I'm not sure what is the best option in my case.

enter image description here

like image 322
Seong Lee Avatar asked Mar 22 '26 19:03

Seong Lee


1 Answers

Yes UITableView is a good option. Just create a delegate method in "choose category" class. Call the delegate on didSelectRowAtIndexPath method of the UITableView in Category class. Then assign Expense class as the delegate for the category class.

Basically you'll be performing a segue once you click on entertainment. User will be presented with "Choose Category controller" Once the user selects the table row. Delegate will be called. Implement that delegate in the expense class. And in the implementation just reload the table with the new value.

UPDATE:

Your choose category class will be something similar to :

import UIKit

protocol ChooseCategoryControllerDelegate: class {
func categoryController(controller: ChooseCategoryTableViewController,  didSelectCategory category: String)
}

class ChooseCategoryTableViewController: UITableViewController {

 let categories = ["category1","category2","category3","category4"]
var selectedcategory: String!
weak var delegate: ChooseCategoryControllerDelegate!

// MARK: - Table view data source
 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categories.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
 let cell = tableView.dequeueReusableCellWithIdentifier("TypeCell", forIndexPath: indexPath) 

 cell.textLabel?.text = categories[indexPath.row]

 return cell
}

// MARK: - Table view delegate
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
 selectedcategory = categories[indexPath.row] as? String
 delegate.categoryController(self, didSelectCategory: selectedcategory)
}
}

In your expense controller add the following code outside your class declaration:

extension ExpenseController: ChooseCategoryControllerDelegate {
func typesController(controller: ChooseCategoryTableViewController,  didSelectCategory category: String) {
let selectedCategory = category
dismissViewControllerAnimated(true, completion: nil)

//update your table category with the new category!!
//reload your table here
}
}

Re-Update:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "CategorySegue" {

  let controller = segue.destinationViewController as! ChooseCategoryTableViewController

  controller.delegate = self
}
}

Also name your segue as "CategorySegue"

like image 199
Amrit Sidhu Avatar answered Mar 25 '26 07:03

Amrit Sidhu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!