I am trying to pass data from my Cheats struct into a new tableviewcontroller to populate the new table with the relevant info.
I have done research but am new to swift.
I do have experience in php but transferring my knowledge is proving quite difficult...
In my prepare for segue class i receive a few errors:
Definition conflicts with previous value:
var DataPass = CheatsArray[indexPath.row]
Cannot assign value type 'String' to '[String]':
DestViewController.CheatsArray = DataPass.name
Here is a copy of my current 3 files
Structs and arrays:
struct Game {
var name : String
var cheats : [Cheat]
}
struct Cheat {
var name : String
var code : String
var description : String
}
// Create Our Game Info And Cheats / Codes For Each Game!
//-------------------------------------------------------
let COD4 = Game(name: "Call Of Duty 4", cheats: [Cheat(name: "Cheat", code: "Code", description: "Description")])
let GTAV = Game(name: "Grand Theft Auto 5", cheats: [Cheat(name: "Cheat", code: "Code", description: "Description")])
// Place Our New Games Inside This Array!
//---------------------------------------
let ArrayOfGames = [COD4,GTAV]
GameListController:
import Foundation
import UIKit
class GamesListViewController: UITableViewController {
var CheatsArray = [Game]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ArrayOfGames.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = ArrayOfGames[indexPath.row].name
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
let DestViewController = segue.destinationViewController as! CheatsListViewController
let DataPass : Game
var DataPass = CheatsArray[indexPath.row]
DestViewController.CheatsArray = DataPass.name
}
}
CheatListController:
class CheatsListViewController: UITableViewController {
var CheatsArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return CheatsArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath)
cell.textLabel?.text = CheatsArray[indexPath.row]
return cell
}
}
The first error is because you defined DataPass twice, one row after another. You can't do that in the same scope. Change the name of one.
The second error is because you cannot assign value type 'String' to '[String]'. Just looking at CheatsArray and name variables, it's clear the first is an array and the second is most likely a string. You might want to append the name to the array.
Related
My application fetches data from a mock API.
Using a custom cell, I display the names of authors on my landing page viewController.
When I click on a cell, it takes that author's book information to display on a 2nd TableViewController.
But even though the implementation is the same as for the landing page. My app freezes until I get a EXC_BAD_ACCESS error
It seems like it's stuck in an infinite loop, but without a proper error, it's hard to know why.
Infinite Loop?
I can get this to work without using a custom cell, but then I cannot display all the information I want (only book title or release date), so the data is there.
import UIKit
class BooksTableViewCell: UITableViewCell {
#IBOutlet weak var name: UILabel!
#IBOutlet weak var pages: UILabel!
#IBOutlet weak var release: UILabel!
// #IBOutlet var coverImage: UIImageView!
static let cellIdentifier = "BooksTableViewCell"
//
override func awakeFromNib() {
super.awakeFromNib()
}
static func nib() -> UINib {
return UINib(nibName: "BooksTableViewCell", bundle: nil)
}
//MARK: configure
public func configure(with viewModel: BooksCellViewModel) {
name.text = viewModel.name
pages.text = String(viewModel.pages)
release.text = viewModel.release
// coverImage.image = viewModel.image
}
}
import UIKit
class BooksTableViewController: UITableViewController {
var books: [Book] = []
var authorName: String = ""
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(BooksTableViewCell.nib(), forCellReuseIdentifier: BooksTableViewCell.cellIdentifier)
tableView.delegate = self
tableView.dataSource = self
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return authorName
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return books.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("Hello1")
let cell = tableView.dequeueReusableCell(withIdentifier: BooksTableViewCell.cellIdentifier, for: indexPath) as! BooksTableViewCell
print("Hello2")
let model = books[indexPath.row]
cell.configure(with: BooksCellViewModel(name: model.title, pages: model.pages, release: model.releaseDate))
return cell
}
}
The landing page controller and cell is similar but works with no problems
import UIKit
class LandingTableViewController: UITableViewController {
let parser = DataAPI()
var authors = [Author]()
var books = [Book]()
var authorName = ""
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(AuthorTableViewCell.nib(), forCellReuseIdentifier: AuthorTableViewCell.cellIdentifier)
tableView.delegate = self
tableView.dataSource = self
parser.getData {
data in
self.authors = data
//Reload UI on Main thread:
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "List of Authors"
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return authors.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: AuthorTableViewCell.cellIdentifier, for: indexPath) as! AuthorTableViewCell
let model = authors[indexPath.row]
cell.configure(with: AuthorCellViewModel(name: model.authorName))
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
books = authors[indexPath.row].books
authorName = authors[indexPath.row].authorName
performSegue(withIdentifier: "Show Books", sender: nil)
}
// MARK: - 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 == "Show Books") {
let showBooksViewController: BooksTableViewController = segue.destination as! BooksTableViewController
showBooksViewController.books = books
showBooksViewController.authorName = authorName
}
}
}
I was able to fix the issue by correctly naming my variables. I needed to be using releaseDate not release as per my model object.
I need to output the second element from a two-dimensional array from one class to a TableViewController. I've tried a couple things, such as data.reduce into:, with no luck. The FirstViewController is supposed to be populated with the three genres "Action, Suspense and Romance", which can then be clicked on to show all the movies that belong to that genre.
First Class:
import Foundation
protocol MovieModelDelegate: class {
func didRecieveDataUpdate(data: [[String]])
}
class MovieModel {
weak var delegate: MovieModelDelegate?
var genreArray: [String] = []
let data: [[String]] = [
["Avatar", "Action"],
["Limitless", "Suspense"],
["Die Hard", "Action"],
["The Avengers", "Action"],
["The Notebook", "Romance"],
["Lakehouse", "Romance"],
["Gone Girl", "Suspense"],
["Deadpool", "Action"],
["Passengers", "Suspense"],
["Inception", "Suspense"],
["Mission Impossible", "Action"]
]
func requestData() {
let movie: [[String]] = data
delegate?.didRecieveDataUpdate(data: movie)
}
}
**TableView Class:**
class FirstTableView: UITableViewController, MovieModelDelegate {
var model = MovieModel()
var selectedGenre: String = ""
override func viewDidLoad() {
super.viewDidLoad()
model.delegate = self
model.requestData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.genreArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "GenreCells", for: indexPath) as! TableViewCell
cell.genreLabel?.text = model.genreArray[indexPath.item]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedGenre = model.data[indexPath.row]
selectedGenre = ""
for indexPath in model.data{
if indexPath[1] == selectedGenre{
selectedGenre.append(indexPath[0])
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "pushToMovies") {
if let VC = segue.destination as? FilteredSelection {
VC.selectedMovies = [selectedGenre]
}
}
}
func didRecieveDataUpdate(data: [[String]]) {
}
deinit{
}
}
You need to change the data into a form that can then be indexed by a tableview. I would change it into a dictionary using the following code.
let genreDictionary = data.reduce(into: [:]) { d, element in
d[element[1], default: []].append(element[0])
}
This will create the following Dictionary
["Suspense": ["Limitless", "Gone Girl", "Passengers", "Inception"],
"Romance": ["The Notebook", "Lakehouse"],
"Action": ["Avatar", "Die Hard", "The Avengers", "Deadpool", "Mission Impossible"]]
Then in your tableview functions you use the dictionary as follows
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.genreDictionary.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "GenreCells", for: indexPath) as! TableViewCell
let d = model.genreDictionary
cell.genreLabel?.text = d[d.keys.index(d.startIndex, offsetBy: indexPath.row)].key
return cell
}
When you need the array of films for a certain tableview row you can use the following
let d = model.genreDictionary
let arrayOfFilms = d[d.keys.index(d.startIndex, offsetBy: indexPath.row)].value
You can use a Set to get all Genres without to have duplicates.
I recognized you want to display the genres in your TableView with model.genreArray but your genreArray is always empty because you never append data to it.
The second thing I recoginzed: In your TableViewController you
implemented the didReciveDataUpdate function, but you didnt do
anything with it.
First Class:
import Foundation
protocol MovieModelDelegate: class {
func didRecieveDataUpdate(data: [[String]])
}
class MovieModel {
weak var delegate: MovieModelDelegate?
let data: [[String]] = [
["Avatar", "Action"],
["Limitless", "Suspense"],
["Die Hard", "Action"],
["The Avengers", "Action"],
["The Notebook", "Romance"],
["Lakehouse", "Romance"],
["Gone Girl", "Suspense"],
["Deadpool", "Action"],
["Passengers", "Suspense"],
["Inception", "Suspense"],
["Mission Impossible", "Action"]
]
private var genres = [String]()
public init() {
for dataSet in data {
self.genres.append(dataSet[1])
}
//this will generate a array with only uniqe genres
var genreArray = Array(Set(self.genres))
}
func requestData() {
let movie: [[String]] = data
delegate?.didRecieveDataUpdate(data: movie)
}
}
FirstTableView Class:
(view didReciveDataUpdate function)
class FirstTableView: UITableViewController, MovieModelDelegate {
var model = MovieModel()
var selectedGenre: String = ""
override func viewDidLoad() {
super.viewDidLoad()
model.delegate = self
model.requestData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.genreArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "GenreCells", for: indexPath) as! TableViewCell
cell.genreLabel?.text = model.genreArray[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//To get the Genre
selectedGenre = model.data[indexPath.row][1]
for indexPath in model.data{
if indexPath[1] == selectedGenre{
selectedGenre.append(indexPath[0])
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "pushToMovies") {
if let VC = segue.destination as? FilteredSelection {
VC.selectedMovies = [selectedGenre]
}
}
}
func didRecieveDataUpdate(data: [[String]]) {
//=================here you need to update your tableView===================
}
deinit{
}
}
I'm trying to segue from one UITableView to another UITableView. I want to segue and pass the myCell.textLabel?.text value of the selected cell to the second UITableView.
The code for my first UITableView (MenuTypeTableViewController and the code for my second UITableView (TypeItemsTableViewController) is also below.
I'm fully aware this involves the prepareForSegue function which currently I've not created, purely because I'm unsure where I override it and how to pass in the textLabel value to it.
Hope my question makes sense, I will update with suggestions and edits.
class MenuTypeTableViewController: UITableViewController, MenuTypeServerProtocol {
//Properties
var cellItems: NSArray = NSArray()
var menuType: MenuTypeModel = MenuTypeModel()
override func viewDidLoad() {
super.viewDidLoad()
let menuTypeServer = MenuTypeServer()
menuTypeServer.delegate = self
menuTypeServer.downloadItems()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellItems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier: String = "cellType"
let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
let item: MenuTypeModel = cellItems[indexPath.row] as! MenuTypeModel
myCell.textLabel?.text = item.type
return myCell
}
func itemsDownloaded(items: NSArray) {
cellItems = items
tableView.reloadData()
}
}
class TypeItemsTableViewController: UITableViewController, TypeItemsServerProtocol {
//Properties
var cellItems: NSArray = NSArray()
var typeItemList: TypeItemsModel = TypeItemsModel()
override func viewDidLoad() {
super.viewDidLoad()
let typeItemsServer = TypeItemsServer()
typeItemsServer.delegate = self
typeItemsServer.downloadItems()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellItems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier: String = "cellTypeItem"
let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
let item: TypeItemsModel = cellItems[indexPath.row] as! TypeItemsModel
myCell.textLabel?.text = item.name
return myCell
}
func itemsDownloaded(items: NSArray) {
cellItems = items
tableView.reloadData()
}
}
Hi try the following set of code, I have added few additional changes in your code make use of it, I hope it will solve your issue.
I have added only the extra codes which you needed
class TypeItemsTableViewController: UITableViewController, TypeItemsServerProtocol {
// Add this variable in this class and use it whereever you needed it in this class
var selectedItem: String?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Get the selected cell
let selectedCell = tableView.cellForRow(at: indexPath)
// Now maintain the text which you want in this class variable
selectedItem = selectedCell?.textLabel?.text
// Now perform the segue operation
performSegue(withIdentifier: "TypeItemsTableViewController", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "TypeItemsTableViewController" {
let destinationVC = segue.destination as? TypeItemsTableViewController
destinationVC?.selectedItem = self.selectedItem // Pass the selected item here which we have saved on didSelectRotAt indexPath delegate
}
}
In Second class:
class TypeItemsTableViewController: UITableViewController, TypeItemsServerProtocol {
// Add this variable in this class and use it whereever you needed it in this class
var selectedItem: String?
What you can do is to make a variable in your second UITableView
var String: labelSelected?
then in you prepare for segue method just set the labelSelected to the value of the cell.
refToTableViewCell.labelSelected = youCell.textlabel?.text
If you set up a segue in storyboards from one storyboard to another, you can use the code below in your prepareForSegue method. You'll need to add a testFromMenuTableViewController property to your TypeItemsTableViewController.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? TypeItemsTableViewController,
let path = self.tableView.indexPathForSelectedRow,
let cell = self.tableView.cellForRow(at: path),
let text = cell.textLabel?.text {
destination.textFromMenuTypeTableViewController = text
}
}
For more info check this SO answer.
I would like to ask you guys how to make a data structure for multi-level drill-down TableView in Swift. As of myself, I know how to do it and pass data this way TableView -> TableView -> DetailViewController using NSObject for creating Swift models. But several weeks now I struggle with finding of how to do this way: TableView -> TableView -> DetailViewController -> TableView again.
As the app I am doing now is education related, the first TableView would contain sections of topics and leesons in them, then when the user selects the lesson, the segue performs and shows content as ViewController, then the user press the button and he is redirected to another TableView with tasks related to the lesson in it.
So in all it should be like this: LessonsTableView -> LessonDetailViewController -> TasksTableView -> TaskDetailViewController.
I can say that the main problem is to get the tableView of tasks listed from LessonDetailViewController. As I know how to make 2 levels of data, but can't do 3 or more.
I have searched all the internet, I have found examples in Obj-C, but simply I don't understand that programming language (tried to convert the code in XCode). Could anyone guide me of how to achieve this? Perhaps there's a link with brief tutorial that you guys know.
Update
I've managed to do the first TableView and ViewController, but stuck with passing data from LessonViewController to TasksTableView, getting this Error: Expression type '[Lesson]' is ambiguous without more context.
Perhaps anyone could help me with this, my Model:
import Foundation
struct task {
var name: String
}
struct Lesson {
var name: String
var info: String
var tasks: [task]
}
class LessonList {
var name: String
var lessons = [Lesson]()
init(name: String, lessons: [Lesson]) {
self.name = name
self.lessons = lessons
}
class func lessonsSection() -> [LessonList] {
return [self.intro(), self.can()]
}
private class func intro() -> LessonList {
let tasks: [task] = [task(name: "hello"), task(name: "Hey")]
let tasks1: [task] = [task(name: "is He?"), task(name: "are they?")]
var lessons = [Lesson]()
lessons.append(Lesson(name: "I am", info: "This class is I am", tasks: tasks))
lessons.append(Lesson(name: "He is", info: "This class is He is", tasks: tasks1))
return LessonList(name: "Intro", lessons: lessons)
}
private class func can() -> LessonList{
let tasks: [task] = [task(name: "bye"), task(name: "can have")]
let tasks1: [task] = [task(name: "Can he?"), task(name: "They can't")]
var lessons = [Lesson]()
lessons.append(Lesson(name: "I can", info: "This class is I can", tasks: tasks))
lessons.append(Lesson(name: "He can't", info: "This class is He can't", tasks: tasks1))
return LessonList(name: "Can", lessons: lessons)
}
}
The CourseTableView (first)`import UIKit
class CourseTableVC: UITableViewController {
let lessonLists : [LessonList] = LessonList.lessonsSection()
override func viewDidLoad() {
super.viewDidLoad()
title = "Horizon English"
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return lessonLists.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lessonLists[section].lessons.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "lessonCell", for: indexPath)
let lessonList = lessonLists[indexPath.section]
let lessons = lessonList.lessons
let lesson = lessons[indexPath.row]
cell.textLabel?.text = lesson.name
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let lessonList = lessonLists[section]
return lessonList.name
}
//MARK: - UITableViewdelegate, navigation segue
var selectedLesson: Lesson?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let lessonLine = lessonLists[indexPath.section]
var lesson = lessonLine.lessons[indexPath.row]
selectedLesson = lesson
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "lessonDetailView" {
let lessonDetailVC = segue.destination as! LessonViewController
lessonDetailVC.lesson = selectedLesson
}
}
}`
LessonViewController where I get error (in prepare for segue)`import UIKit
class LessonViewController: UIViewController {
#IBOutlet weak var infoLabel: UILabel!
#IBAction func toTasksButton(_ sender: Any) {
}
var lesson: Lesson?
override func viewDidLoad() {
super.viewDidLoad()
title = lesson?.name
infoLabel.text = lesson?.info
}
// MARK: - navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showTasks" {
var taskTV = segue.destination as! TasksTableVC
taskTV.tasksList = lesson?.tasks as! [Lesson]
// Error: Expression type '[Lesson]' is ambiguous without more context
}
}
}`
and TasksTableView: `import UIKit
class TasksTableVC: UITableViewController {
var tasksList = [Lesson]()
override func viewDidLoad() {
super.viewDidLoad()
title = "Tasks"
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasksList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath)
let taskList = tasksList[indexPath.section]
let tasks = taskList.tasks
let task = tasks[indexPath.row]
cell.textLabel?.text = String(describing: task)
return cell
}
}`
Thanks in advance.
I can say that the main problem is to get the tableView of tasks listed from LessonDetailViewController.
I don't know what steps you already did so I'll say how I'd do it:
1 - Have the structure implemented. That's my suggestion:
struct Task {
var name = ""
}
struct Lesson {
var name = ""
var tasks: [Tasks] // list of tasks for this lesson
}
OR
struct Task {
var name = ""
var lesson: Lesson // the leasson which the task belongs to
}
struct Lesson {
var name = ""
}
2 - I suppose you know how to pass data between view controllers, so when the user select the cell of the lesson he wants, you'd pass the lesson object to the LessonDetailVC and show the data. Something like
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedLesson = lessons[indexPath.row]
// code to pass to the next vc here or in prepareForSegue
}
3 - To create the tableview for the tasks you just have to filter(or query, or fetch, I don't know how you're getting the data) in order to obtaining the tasks. Let's suposed you used the first code from item 1 so you just simply show the lesson.tasks array in your table view. If you chose the second code from item 1 you will have to perform some query with a filter passing the lesson your in, something like query.where("lesson", lesson) and you will have the tasks array.
UPDATE
Since you're new on Swift here's an idea on how to model your view controller:
// Your models
struct Task {
var name = ""
var lesson: Lesson // the leasson which the task belongs to
}
struct Lesson {
var name = ""
}
// This is just a DataManager to help you get your data
class DataManager {
func getLessons() -> [Lesson]? {
return [Lesson(name: "Lesson 1"), Lesson(name: "Lesson 2"), Lesson(name: "Lesson 3")]
}
func getTasks(for lesson: Lesson) -> [Task]? {
guard let lessons = getLessons() else {
return nil
}
let allTasks = [
Task(name: "T1", lesson: lessons[0]),
Task(name: "T2", lesson: lessons[0]),
Task(name: "T3", lesson: lessons[0]),
Task(name: "T1", lesson: lessons[1]),
Task(name: "T2", lesson: lessons[1]),
Task(name: "T1", lesson: lessons[2]),
Task(name: "T2", lesson: lessons[2]),
Task(name: "T3", lesson: lessons[2]),
Task(name: "T4", lesson: lessons[2])
]
return allTasks.filter{ $0.lesson == lesson }
}
}
class LessonsViewController: UIViewController, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
private var lessons: [Lesson]? {
didSet {
tableView.reloadData()
}
}
private var selectedLesson: Lesson?
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
lessons = DataManager().getLessons()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lessons?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "lessonCell", for: indexPath)
let lesson = lessons[indexPath.section]
cell.textLabel?.text = lesson.name
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var lesson = lessons[indexPath.row]
selectedLesson = lesson
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "lessonDetailView" {
let lessonDetailVC = segue.destination as! LessonViewController
if selectedLesson != nil {
lessonDetailVC.lesson = selectedLesson!
}
}
}
}
class TasksViewController: UIViewController, UITableViewDataSource {
var lesson: Lesson!
private var tasks: [Task]? {
didSet {
tableView.reloadData()
}
}
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tasks = DataManager().getTasks(for: lesson)
}
}
I need to create a drill down effect with my table views that will expand four table views deep for each of my original cells in my master table view. So far i was successful in populating the master view, and second table view accordingly with this Object Oriented Method, here is the code in my master table view:
class FirstTableViewController: UITableViewController {
let aSport:[Sport] = {
var basketball = Sport()
basketball.name = "Basketball"
basketball.sportCategories = {
var temp = ["International Basketball","Wheelchair Basketball","Beach Basketball","Deaf Basketball","Dwarf Basketball"]
temp.sort(<)
return temp
}()
var golf = Sport()
golf.name = "Golf"
golf.sportCategories = {
var temp = ["Miniature Golf","Dart Golf","Sholf","Disc Golf","Footgolf"]
temp.sort(<)
return temp
}()
var football = Sport()
football.name = "Football"
football.sportCategories = {
var temp = ["Flag Football","Indoor Football","Arena Football","Non-Tackle Football","Paper Football"]
temp.sort(<)
return temp
}()
var swimming = Sport()
swimming.name = "Swimming"
swimming.sportCategories = {
var temp = ["Competitive Swimming","Synchronized Swimming","Duo Swimming","Relay Swimming"]
temp.sort(<)
return temp
}()
return [basketball,golf,football,swimming]
}()
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return aSport.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = aSport[indexPath.row].name
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let cell = sender as! UITableViewCell
let row = tableView.indexPathForCell(cell)?.row
let detail = segue.destinationViewController as! SecondTableViewController
detail.selectedSport = aSport[row!]
}
}
class Sport {
var name: String = "sport name"
var sportCategories: NSArray = ["variations of selected sport"]
var detailText: NSArray = ["little description of sport"]
}
here is the code in my second table view controller:
class SecondTableViewController: UITableViewController {
var selectedSport = Sport();
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return selectedSport.sportCategories.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Custom", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = selectedSport.sportCategories[indexPath.row] as? String
cell.detailTextLabel!.text = selectedSport.detailText[indexPath.row] as? String
return cell
}
}
here are screenshots from my simulator so you get a visual:
https://40.media.tumblr.com/6ee47f49c2b223b514f8067c68ac6af1/tumblr_nqbe74HYGo1tupbydo1_500.png
when basketball is selected:
https://41.media.tumblr.com/ced0ee2ff111a557ec3c21f1fb765adf/tumblr_nqbe74HYGo1tupbydo2_500.png
Now as you can see, i was able to populate the first two views by creating a custom class, creating custom objects of that class and using the properties within the class. Now my dilemma is, each of the "sportCategories" properties have their OWN table views to expand to which will consist of a whole list of names of players in that respective sport. Now what method should i go about doing this? should i create a whole new class in my second table view controller to give the sportsCategories their own properties? or is there a way i can already expand off the work I've already done? a more efficient way?