Cells on UITableView disappearing after scrolling or touching the table - ios

I am implementing a SwipeMenuViewController and when the user starts scrolling or tapping the cell disappears. I am not sure if it is due to the tableView being implemented not properly or the SwipeMenuViewController not being implemented properly.
Before swiping/touching the table, as you can see the table was loaded with the data
However. Once we start swiping or even touching the table, the previous cell disappears:
I have implemented the table view into a ViewController as follows
var currentUser: User!
var requests = [User]()
var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
myTableView = UITableView(frame: CGRect(x: 0, y: 0, width: tableSize.width, height: tableSize.height))
myTableView.register(UINib(nibName: "FriendRequestTableViewCell", bundle: nil), forCellReuseIdentifier: "friendRequest")
myTableView.dataSource = self
myTableView.delegate = self
//myTableView.separatorStyle = .none
myTableView.rowHeight = 103
myTableView.backgroundColor = Colours.flatColour.main.offWhite
self.view.addSubview(myTableView)
}
The protocol methods:
// MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.requests.count == 0) {
self.myTableView.setEmptyMessage("You are up to date!\nYou dont have any notifications 😄")
} else {
self.myTableView.restore()
}
return requests.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("FriendRequestTableViewCell", owner: self, options: nil)?.first as! FriendRequestTableViewCell
cell.currentUser = currentUser
cell.requestFriendDelegate = self
cell.user = requests[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 103
}
Does anyone have an idea as to where I have gone wrong, I am not sure if I am missing any code. The example of SwipeMenuViewController does not show this way of implementation. I have followed the Read me on implementing SwipeMenuView. I have two ViewControllers, one which holds the SwipeMenuView and one which holds the table for each tab which is the controller I think the issue is coming from. Thank you for any help
The notificationsViewController class:
class NotificationsViewController: UIViewController, SwipeMenuViewDelegate {
#IBOutlet weak var swipeMenuView: SwipeMenuView! {
didSet {
swipeMenuView.delegate = self
swipeMenuView.dataSource = self
var options: SwipeMenuViewOptions = .init()
options.tabView.style = .flexible
options.tabView.margin = 20.0
options.tabView.additionView.backgroundColor = Colours.flatColour.yellow.lighter //UIColor.black//UIColor.customUnderlineColor
options.tabView.itemView.textColor = Colours.flatColour.main.gray
options.tabView.itemView.selectedTextColor = Colours.flatColour.yellow.lighter //UIColor.black//UIColor.customSelectedTextColor
options.tabView.itemView.font = UIFont(name: "Biotif-Medium", size: 14)!
swipeMenuView.reloadData(options: options)
}
}
var tabTitles: [String] = ["All", "Friend Requests", "Items"]
var requests = [User]()
}
I reloadData again when populating the requests array and the array isnt empty.
The data source methods:
extension NotificationsViewController: SwipeMenuViewDataSource {
//MARK - SwipeMenuViewDataSource
func numberOfPages(in swipeMenuView: SwipeMenuView) -> Int {
return tabTitles.count
}
func swipeMenuView(_ swipeMenuView: SwipeMenuView, titleForPageAt index: Int) -> String {
return tabTitles[index]
}
func swipeMenuView(_ swipeMenuView: SwipeMenuView, viewControllerForPageAt index: Int) -> UIViewController {
let vc = NotificationsContentTable()
vc.currentUser = currentUser
vc.tableSize = swipeMenuView.bounds.size
if index == 0 {
vc.requests = requests
}else if index == 1 {
vc.requests = []
}else if index == 2 {
vc.requests = []
}
print("the requests are in = \(vc.requests)")
return vc
}
}

I've figured out where I was going wrong. Before returning the viewController in viewControllerForPageAt, you must add that viewController as a child with addChild(viewController).

Related

Swift xcode IOS, return to tableview after selecting a search result

I am trying to create a joke app. The search bar works. But when I select the search results, it won't take me to the tableview( the jokes). How can I fix it? Thanks everyone in advance.
import UIKit
class JokeTableViewController: UITableViewController, UISearchResultsUpdating {
var jokes = [ "chiken", "Walk into A Bar", "Olives", "Racer", "love"]
var filteredJokes = [String]()
var searchController : UISearchController!
var resultsController = UITableViewController()
override func viewDidLoad() {
super.viewDidLoad()
self.resultsController.tableView.dataSource = self
self.resultsController.tableView.delegate = self
self.searchController = UISearchController(searchResultsController: self.resultsController)
self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self
//self.searchController.dimsBackgroundDuringPresentation = false
}
func updateSearchResults(for searchController: UISearchController) {
self.filteredJokes = self.jokes.filter { (jokee:String) -> Bool in
if jokee.lowercased().contains(self.searchController.searchBar.text!.lowercased()){
return true
}else {
return false
}
}
//Update the results TableView
self.resultsController.tableView.reloadData()
}
//WHEN SELECTED TO TO THE JOKES
// HOW MANY?
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.tableView{
return self.jokes.count
} else {
return self.filteredJokes.count
}
}
//WHAT GOES INSIDE?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if tableView == self.tableView{
cell.textLabel?.text = self.jokes[indexPath.row]
} else{
cell.textLabel?.text = self.filteredJokes[indexPath.row]
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let selectedJoke = jokes[indexPath.row]
performSegue(withIdentifier: "moveToJokeDefinition", sender: selectedJoke)
}
override func prepare( for segue: UIStoryboardSegue, sender: Any?){
if let jokeVC = segue.destination as? JokeDefinitionViewController{
if let selectedJoke = sender as? String {
jokeVC.joke = selectedJoke
}
//select the jokes
}
}
}
You should use one global variable.
var globaldata = String
self.globaldata = self.jokes .... initData............
you should use this data in tableView Delegate functions.

What is a better way of handling this type of issue?

I'm sort of new to iOS development using Swift. So, I might be missing a simple solution in Swift that I'm not aware of.
I am working on a tvOS app where I display a list of video content that the user can select from. The app also contains a settings tab that allows the user to configure 5 different types of settings. Once they select a specific category, it displays a new table view with the corresponding options which are in the options array. This is where the "issue" is that I need help.
I have this struct which I'm using as a singleton:
struct BMUserSettings
{
internal static var shared = BMUserSettings()
var categories = [String]()
var options = [[String]]()
var currOptionsSelected: [Int] = [0,0,0,0,0] // This array corresponds to the categories array. It tells us what option within that group was selected.
init()
{
self.categories = ["Brand", "Environment","UI Language", "Playback Language", "Geo Location Permission"]
let brandOptionsGroup: [String] = ["CTV", "CTVHUB", "TSN", "Snackable", "RDS", "CP24", "BNN", "CTVNews", "Crave", "BRAVO", "E_BRAND", "SE", "VIDIQA"]
let environmentOptionsGroup: [String] = ["Staging", "Prod"]
let uiLanguageOptionsGroup: [String] = ["en", "fr"]
let playbackLanguageOptionsGroup: [String] = ["en", "fr"]
let geoLocationOptionsGroup: [String] = ["Allow", "Don't Allow"]
options.append(brandOptionsGroup)
options.append(environmentOptionsGroup)
options.append(uiLanguageOptionsGroup)
options.append(playbackLanguageOptionsGroup)
options.append(geoLocationOptionsGroup)
}
// MARK: - Custom Methods
func displayUserSettings() -> String
{
let displayText: String = "Brand=\(options[0][currOptionsSelected[0]]) Environment=\(options[1][currOptionsSelected[1]]) UI Language=\(options[2][currOptionsSelected[2]]) Playback Language=\(options[3][currOptionsSelected[3]]) Geo Location=\(options[4][currOptionsSelected[4]])"
return displayText
}
// MARK: - User Defaults
func saveToUserDefaults()
{
UserDefaults.standard.set(BMUserSettings.shared.currOptionsSelected, forKey: "currentoptions")
}
func loadFromUserDefaults(){
if let currentOptionsSelected = UserDefaults.standard.object(forKey: "currentoptions") as? [Int]{
BMUserSettings.shared.currOptionsSelected = currentOptionsSelected
}
else{
BMUserSettings.shared.currOptionsSelected = [0,0,0,0,0]
}
}
}
As you can see, the "currOptionsSelected" integer array is holding the option that the user selected for each of the categories. For example, if the user chooses the brand "Snackable", then the first element of the currOptionsSelected array will hold a 3 as a value.
I'm saving and loading the currOptionsSelected to/from UserDefaults so that I know what the user's current settings are.
The problem with this approach is:
1) Even if I know the index of the specific option that the user chose, I will still need a set if if-else or switch conditions to make sure I can actually get the correct string value from the corresponding "options" array
2) If any other developer needs to add categories and corresponding options, then they need to make sure they keep everything in order
3) I just don't know if this is the best way of handling this type of issue
What is a better way of doing this?
Here's how I'm trying to use it in a table view:
import UIKit
final class BMSettingsViewController: UIViewController
{
// MARK: - Instance Variables
private static let reuseIdentifier = String(describing: BMContentCell.self)
private let tableview = UITableView(backgroundColor: .white, autoResizingMask: false)
private let tabBarBannerHeight: CGFloat = 150
private var selectedCategoryIndex: Int = 0
private var settingsDetailVC: BMDetailSettingsViewController?
private var categoryNames: [String] = [String]()
private var categoryOptions: [String] = [String]()
// MARK: - View Lifecycle Methods
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
categoryNames = BMCategory.allValues
categoryOptions = BMUserSettings.shared.currOptionsSelected.map { $0.value }
self.tableview.reloadData()
}
override func viewWillDisappear(_ animated: Bool)
{
BMUserSettings.shared.saveToUserDefaults()
}
override func loadView()
{
super.loadView()
self.tableview.dataSource = self
self.tableview.delegate = self
self.tableview.register(BMUserSettingsCell.self, forCellReuseIdentifier: BMSettingsViewController.reuseIdentifier)
displayContent()
}
// MARK: - Custom Methods
private func displayContent()
{
view.addSubview(tableview)
tableview.anchor(
top: self.view.topAnchor,
leading: self.view.leadingAnchor,
bottom: self.view.bottomAnchor,
trailing: self.view.trailingAnchor,
padding: UIEdgeInsets(top: tabBarBannerHeight, left: 0, bottom: 0, right: 0)
)
}
}
// MARK: - UITableView Datasource & Delegate Extension
extension BMSettingsViewController: UITableViewDataSource, UITableViewDelegate
{
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
return "Select an option below to configure it's settings..."
}
func numberOfSections(in tableView: UITableView) -> Int
{
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return categoryNames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: BMSettingsViewController.reuseIdentifier, for: indexPath) as! BMUserSettingsCell
cell.configureCell(categoryName: categoryNames[indexPath.row], optionDetailDescription: categoryOptions[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let selectedCategory = categoryNames[indexPath.row]
settingsDetailVC = BMDetailSettingsViewController()
guard let settingsVC = settingsDetailVC else {return}
settingsVC.options = BMUserSettings.shared.options[BMCategory.init(rawValue: selectedCategory)!]!
settingsVC.delegate = self
settingsVC.selectedCategoryIndex = indexPath.row
BMViewControllerManager.shared.getTopViewController()?.present(settingsVC, animated: true)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 120
}
}
// MARK: - Protocol Extension
extension BMSettingsViewController: OptionsSelector
{
func didFinishSelectingOption(selectedCategoryIndex: Int, selectedOptionIndex: Int)
{
self.selectedCategoryIndex = selectedCategoryIndex
// BMUserSettings.shared.currOptionsSelected[self.selectedCategoryIndex] = selectedOptionIndex
}
}
Here's the settings detail controller that lists just the options within that specific category:
import UIKit
// MARK: - Protocol (used to notify Settings view controller when an option was selected)
protocol OptionsSelector
{
func didFinishSelectingOption(selectedCategoryIndex: Int, selectedOptionIndex: Int)
}
final class BMDetailSettingsViewController: UIViewController
{
// MARK: - Instance Variables
private let cellId = "cellId"
private let tabBarBannerHeight: CGFloat = 150
private var selectedOptionIndex: Int = 0
private let tableview = UITableView(backgroundColor: .white, autoResizingMask: false)
var options: [String] = [String]()
var selectedCategoryIndex: Int = 0
var delegate: OptionsSelector?
// MARK: - View Life Cycle Methods
override func loadView()
{
super.loadView()
self.tableview.dataSource = self
self.tableview.delegate = self
self.tableview.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
displayContent()
}
// MARK: - Custom Methods
private func displayContent()
{
view.addSubview(tableview)
tableview.anchor(
top: self.view.topAnchor,
leading: self.view.leadingAnchor,
bottom: self.view.bottomAnchor,
trailing: self.view.trailingAnchor,
padding: UIEdgeInsets(top: tabBarBannerHeight, left: 0, bottom: 0, right: 0)
)
}
}
// MARK: - UITableView Datasource & Delegate Extension
extension BMDetailSettingsViewController: UITableViewDataSource, UITableViewDelegate
{
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return "Select an option below..." }
func numberOfSections(in tableView: UITableView) -> Int { return 1 }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return options.count }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
cell.textLabel?.text = "\(options[indexPath.row])"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
BMViewControllerManager.shared.getTopViewController()?.dismiss(animated: true)
self.selectedOptionIndex = indexPath.row
self.delegate?.didFinishSelectingOption(selectedCategoryIndex: self.selectedCategoryIndex, selectedOptionIndex: self.selectedOptionIndex)
}
}
Thank you!
First of all create an enum Category,
enum Category: String {
case brand = "Brand"
case environment = "Environment"
case uiLanguage = "UI Language"
case playbackLanguage = "Playback Language"
case geoLocationPermission = "Geo Location"
}
Next,
Create options of type [Category:[String]], currOptionsSelected of type [Category:String] and defaultOptions of type [Category:String].
Also, instead of displayUserSettings, conform struct BMUserSettings to CustomStringConvertible and implement the description to return the relevant String value.
And, to create the Singleton, mark init() as private.
There is no need to create a separate array for categories.
So the whole struct BMUserSettings will be like,
struct BMUserSettings: CustomStringConvertible {
static var shared = BMUserSettings()
let options: [Category:[String]]
let defaultOptions: [Category:String]
var currOptionsSelected: [Category:String]
let categories: [Category]
private init() {
options = [
.brand : ["CTV", "CTVHUB", "TSN", "Snackable", "RDS", "CP24", "BNN", "CTVNews", "Crave", "BRAVO", "E_BRAND", "SE", "VIDIQA"],
.environment : ["Staging", "Prod"],
.uiLanguage : ["en", "fr"],
.playbackLanguage : ["en", "fr"],
.geoLocationPermission : ["Allow", "Don't Allow"]
]
defaultOptions = self.options.mapValues{ $0.first! }
currOptionsSelected = self.defaultOptions
categories = [.brand, .environment, .uiLanguage, .playbackLanguage, .geoLocationPermission]
}
var description: String {
return self.currOptionsSelected.reduce("") { (result, option) -> String in
return "\(result) \(option.key.rawValue) = \(option.value)\n"
}
}
// MARK: - User Defaults
func saveToUserDefaults() {
var dict = [String:String]()
currOptionsSelected.forEach { dict[$0.key.rawValue] = $0.value }
UserDefaults.standard.set(dict, forKey: "currentoptions")
}
mutating func loadFromUserDefaults() {
if let currentOptionsSelected = UserDefaults.standard.object(forKey: "currentoptions") as? [String:String] {
var dict = [Category:String]()
currentOptionsSelected.forEach {
if let category = Category(rawValue: $0.key) {
dict[category] = $0.value
}
}
self.currOptionsSelected = dict
}
else {
self.currOptionsSelected = self.defaultOptions
}
}
}
Use it in the following way,
BMUserSettings.shared.currOptionsSelected[.brand] = "Snackable"
BMUserSettings.shared.saveToUserDefaults()
BMUserSettings.shared.loadFromUserDefaults()
print(BMUserSettings.shared)
BMUserSettings.shared.categories.forEach {
print($0.rawValue, ":", BMUserSettings.shared.currOptionsSelected[$0]!)
}

Data from firebase not populating in my table view

So I recently asked a question regarding firebase - but have refactored my code quite a bit as I wanted all of my firebase methods to be done in the same place. I am having the following issue after the refactor...
Data from my firebase database is not populating my tableview. I'm not too sure why this would be, as it was working fine before I moved the method to a separate file from my table view(for cleaner code). All I did was move the method that populates the array to a separate file, return an array and then reload the tableview after calling the method. Below is the code in question:
In my FireBaseMethods class
//-------------- POPULATE TABLE ARRAY -----------------//
public func populateConsumableTableArray() -> [Consumable]{
var tableArray = [Consumable]()
//let the object populate itself.
ref.child("Consumables").observe(.childAdded, with: { snapshot in
let dataChange = snapshot.value as? [String:AnyObject]
let aRequest = Consumable(aDict: dataChange!)
tableArray.append(aRequest)
})
return tableArray
}
In my ListOfConsumablesViewController table view class
class ListOfConsumablesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
private var methods:MethodsForController = MethodsForController()
private var fireBaseMethods:FireBaseMethods = FireBaseMethods()
private var consumableArray = [Consumable]()
let picker = UIImagePickerController()
#IBOutlet weak var consumableTable: UITableView!
//-------------------- VIEW DID LOAD -----------------------//
override func viewDidLoad() {
super.viewDidLoad()
//Trying to populate the table view here...
consumableArray = fireBaseMethods.populateConsumableTableArray()
consumableTable.reloadData()
self.consumableTable.dataSource = self
self.consumableTable.delegate = self
}
...
//---------------------- FUNCTIONS FOR TABLE VIEW CELLS & TABLE ----------------------//
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(consumableArray.count)
return consumableArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "consumableCell", for: indexPath) as! ConsumablesCell
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor.lightGray.cgColor
cell.backgroundColor = UIColor.white
cell.adapterType.text = consumableArray[indexPath.row].getType()
cell.count.text = String(consumableArray[indexPath.row].getCount())
if Int(consumableArray[indexPath.row].getCount()) ?? 0 <= 0{
cell.count.textColor = UIColor.red
}else{
cell.count.textColor = UIColor.black
}
cell.sku.text = consumableArray[indexPath.row].getSku()
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 90
}
}
As shown below, nothing populates in the table view... I'm assuming it's something to do with the method being in a separate file, but I'm not really sure why that would be?
Simple implementation of a completion handler
//-------------- POPULATE TABLE ARRAY -----------------//
public func populateConsumableTableArray(completion: #escaping (Consumable) -> Void) {
//let the object populate itself.
ref.child("Consumables").observe(.childAdded, with: { snapshot in
guard let dataChange = snapshot.value as? [String:AnyObject] else { return }
let aRequest = Consumable(aDict: dataChange)
completion(aRequest)
})
}
override func viewDidLoad() {
super.viewDidLoad()
self.consumableTable.dataSource = self
self.consumableTable.delegate = self
//Trying to populate the table view here...
fireBaseMethods.populateConsumableTableArray { [unowned self] consumable in
self.tableArray.append(consumable)
DispatchQueue.main.async {
self.consumableTable.reloadData()
}
}
}

Searching TableView can't select row

While searching a tableView, every time I try to select a row it just takes me back to the unsearched tableView. What am I missing? the segue works perfectly when not filtering through the table. The ability to select a row just disapears while the searchBar is activated.
import UIKit
import Foundation
class BenchmarkWODViewController: UITableViewController, UISearchResultsUpdating {
var WodList = [WOD]()
var FilteredWodList = [WOD]()
var Keyword = ""
var searchController : UISearchController?
var index = Int()
#IBAction func backButton(sender: AnyObject) {
self.navigationController?.popViewControllerAnimated(true)
}
override func viewDidLoad() {
super.viewDidLoad()
for wodData in BenchmarkWODs.library {
let wod = WOD(dictionary: wodData)
WodList.append(wod)
}
// Search Bar
self.searchController = UISearchController(searchResultsController: nil)
self.searchController?.searchBar.autocapitalizationType = .None
self.tableView.tableHeaderView = self.searchController?.searchBar
self.searchController?.searchResultsUpdater = self
self.Keyword = ""
definesPresentationContext = true
self.filterByName()
}
func filterByName(){
self.FilteredWodList = self.WodList.filter({ (wod: WOD) -> Bool in
if self.Keyword.characters.count == 0 {
return true
}
if (wod.name?.lowercaseString.rangeOfString(self.Keyword.lowercaseString)) != nil {
return true
}
return false
})
self.tableView.reloadData()
}
// Search Bar Function
func updateSearchResultsForSearchController(searchController: UISearchController) {
Keyword = searchController.searchBar.text!
self.filterByName()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.FilteredWodList.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("BenchmarkCell", forIndexPath: indexPath) as UITableViewCell
let wod = self.FilteredWodList[indexPath.row]
if let wodName = wod.name {
cell.textLabel?.text = wodName
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.filterByName()
self.performSegueWithIdentifier("showBenchmarkDetail", sender: nil)
}
}
Figured it out after playing around. Apparently adding the below code corrects the problem.
searchController?.dimsBackgroundDuringPresentation = false
swift 'dimsBackgroundDuringPresentation' was deprecated in iOS 12.0 Use the obscuresBackgroundDuringPresentation property instead.
searchController?.obscuresBackgroundDuringPresentation = false
searchController.obscureBAckgroundDuringPresentation = false is deprecated in IOS 12.0, so for me it was issue with other gesture detector added to the tableview , so make sure you dont have any other gesture detector and touchesview method that distort the normal working flow of tablview's delegate method( didSelectAtRow ), hope it will work,

How to add a cell to my Table View dynamically using a button

I am trying to add a cell to my table view with a button. Everything I have read and watched suggests that what I have written should work, but it doesn't. Any suggestions?
import UIKit
class RootViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
private var cellPointSize: CGFloat!
private var albumsList: AlbumList!
private var albums:[Album]!
private let albumCell = "Album"
#IBOutlet var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let preferredTableViewFont = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cellPointSize = preferredTableViewFont.pointSize
albumsList = AlbumList.sharedAlbumList
albums = albumsList.albums
self.myTableView.dataSource = self
self.myTableView.delegate = self
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return albums.count
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Albums"
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCellWithIdentifier(albumCell, forIndexPath: indexPath) as! UITableViewCell
//cell.textLabel?.font = fontForDisplay(atIndexPath: indexPath)
cell.textLabel?.text = albums[indexPath.row].name
cell.detailTextLabel?.text = albums[indexPath.row].artist
return cell
}
#IBAction func addNewAlbumAction(sender: UIBarButtonItem) {
var newAlbum = Album(nameIn: "New Title", yearIn: "New Year", artistIn: "New Artist", labelIn: "New Label")
albumsList.addAlbum(newAlbum)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.myTableView.reloadData()
})
}
func saveData(albumObject: Album) {
var archiveArray = NSMutableArray(capacity: albums.count)
for a in albums {
var albumEncodedObject = NSKeyedArchiver.archivedDataWithRootObject(a)
archiveArray.addObject(albumEncodedObject)
}
var userData = NSUserDefaults()
userData.setObject(archiveArray, forKey: "albums")
userData.synchronize()
}
My albums array is adding the data correctly. I can see the albums in the debugger. The delegate methods are never being called after the first time when the app loads. Any ideas?
in tableView:numberOfRowsInSection:, it returns albums.count
but when the button is pressed, you add the new album to albumsList
The problem is, albums will not get update.
So I think you should return albumsList.albums.count instead.
and in tableView:cellForRowAtIndexPath:, you modify the cell correspond to albumsList.albums[indexPath.row]

Resources