UITableView reload issue - ios

I have UITableView inside UIScrollView and implemented paging with which I get 10 records in each page. I am facing a problem when after the IndexPath row is 9 then again UITableView reloads cells starting from row 2 due to which all the Pages are loaded once. Here is my code:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if listData != nil{
print("list count in numberOfRowsInSection\(listData?.count)")
return (listData?.count)!
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("CellForRowIndexPath:\(indexPath.row)")
let cell: NewPostCell = tableView.dequeueReusableCell(withIdentifier: "NewPostCell") as? NewPostCell ??
NewPostCell(style: .default, reuseIdentifier: "NewPostCell")
cell.delegate = self
cell.updateWithModel(self.listData![indexPath.row] as AnyObject)
cell.tbleUpdateDelegate = self
cell.selectionStyle = .none
cell.accessoryType = .none
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print("in will display row - \(indexPath.row)")
if pageNumber >= totalPages {
return
}
else
{
if (listData?.count)! == 10*pageNumber{
if (listData?.count)! - 3 == indexPath.row{
if !boolHitApi{
boolHitApi = true
return
}
pageNumber += 1
self.callService()
}
}
}
}
override func viewWillAppear(_ animated: Bool) {
callService()
}
func callService(){
SVProgressHUD.show()
setPageToOne()
ProfileApiStore.shared.requestToGetProfile(loggedInUserId: UserStore.shared.userId, userId: UserStore.shared.userIdToViewProfile, limit: "10", page: String(self.pageNumber), completion: {(result) in
SVProgressHUD.dismiss()
self.totalPages = result.totalPages!
if self.listData?.count == 0 || (self.pageNumber as AnyObject) as! Int == (1 as AnyObject) as! Int{
self.listData = result.userdata?.newPostData
} else {
self.listData = self.listData! + (result.userdata?.newPostData)!
}
self.tableView.reloadData()
})
}

The code you mentioned in the comments adds 10 rows to the datasource (self.listData) but you are only calling insertRows with one row.
You could loop through them, adding an item to the array and adding a row each time:
func callService() {
ProfileApiStore.shared.requestToGetProfile(loggedInUserId: UserStore.shared.userId, userId: UserStore.shared.userIdToViewProfile, limit: "10", page: String(self.pageNumber), completion: {(result) in
SVProgressHUD.dismiss()
self.totalPages = result.totalPages!
let newItems = result.userdata?.newPostData
tableView.beingUpdates()
for item in newItems {
self.listData.append(item)
let indexPath = IndexPath(row:(self.listData!.count-1), section:0) // shouldn't this be just self.listData!.count as your adding a new row?
tableView.insertRows(at: [indexPath], with: .left)
}
tableView.endUpdates()
})
}

Related

How to get row index from UITableView which the array list made by append to a class

How to get row index from UITableView which the array list made by append to a Object Class.
And I want to get the row index based on the value from the object, I needed that for scroll to a row which I only know a value from object, but don't know which the row index.
Below is the code for create a array and show to the UITableView.
let paging: Int
let obj: Any
var currentAyaPlaying: Int?
var listArr = [] as [Any]
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 700
tableView.rowHeight = UITableView.automaticDimension
tableView.allowsSelection = true
if paging == Paging.SURA {
let sura = obj as! Sura
listArr.append(Sura(sura.index, sura.start, sura.ayas, sura.type, sura.img, sura.name, sura.translate))
for aya in 1...sura.ayas {
listArr.append(Mark(sura.index, aya))
}
} else if (paging == Paging.JUZ){
let juz = obj as! Juz
for suras in juz.sura_start...juz.sura_end {
let sura = MetaData().mSuras[suras - 1]
if juz.sura_start == suras {
for aya in juz.aya_start...(juz.sura_end == suras ? juz.aya_end : sura.ayas) {
listArr.append(Mark(suras, aya))
}
} else if juz.sura_end == suras {
listArr.append(Sura(sura.index, sura.start, sura.ayas, sura.type, sura.img, sura.name, sura.translate))
for aya in 1...juz.aya_end {
listArr.append(Mark(suras, aya))
}
} else {
listArr.append(Sura(sura.index, sura.start, sura.ayas, sura.type, sura.img, sura.name, sura.translate))
for aya in 1...sura.ayas {
listArr.append(Mark(suras, aya))
}
}
}
}
NotificationCenter.default.addObserver(self, selector: #selector(appearNotifAudioReload(_:)), name: NSNotification.Name(rawValue: NotifKey.actAudioReloadFromParentToChild), object: nil)
}
#objc func appearNotifAudioReload(_ notification: Notification) {
guard let sura = notification.userInfo?["sura"] as? Int else { return }
guard let aya = notification.userInfo?["aya"] as? Int else { return }
print("sura: \(sura)")
print("aya: \(aya)")
self.currentAyaPlaying = aya
if paging == Paging.SURA {
let indexPath = NSIndexPath(row: self.currentAyaPlaying!, section: 0)
tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)
} else if paging == Paging.JUZ {
let IStackInHere = Mark(sura, aya) // how to get the row index from this data?
let indexPath = NSIndexPath(row: IStackInHere, section: 0)
tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)
}
tableView.reloadData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listArr.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (listArr[indexPath.row] as? Sura) != nil {
tableView.register(UINib(nibName: identifierAyaHeader, bundle: Bundle.main), forCellReuseIdentifier: identifierAyaHeader)
let cell = tableView.dequeueReusableCell(withIdentifier: identifierAyaHeader, for: indexPath) as! AyaCellHeader
let data = listArr[indexPath.row] as! Sura
cell.configureWithData(data)
return cell
}
tableView.register(UINib(nibName: identifierAya, bundle: Bundle.main), forCellReuseIdentifier: identifierAya)
let cell = tableView.dequeueReusableCell(withIdentifier: identifierAya, for: indexPath) as! AyaCell
let data = listArr[indexPath.row] as! Mark
cell.configureWithData(data)
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (listArr[indexPath.row] as? Mark) != nil {
tableView.deselectRow(at: indexPath, animated: false)
let mark = listArr[indexPath.row] as! Mark
showActionBottom(mark: mark)
}
}
I want to get it in the condition if paging == Paging.JUZ in the func appearNotifAudioReload, I just try to get it with:
let index = listArr.firstIndex{$0 === Mark(sura, aya)}
But no lucky and error
"Binary operator '===' cannot be applied to operands of type 'Any' and
'Mark'"
I know this forum is not to solve my coding problems, but right now I'm really troubled.
Thanks in advance.
You need to use == (to equal signs) to compare equality. Assuming the Mark struct conforms to Equatable, you can use the following:
let index = listArr.firstIndex { $0 as! Mark == Mark(sura, aya) }

Scroll to the latest inserted row in UITableView using Realm Objects

I have the following code which is working fine, it gets a list of items from a list in Realm called groceryList and displays them on a UITableView in descending order based on the productName. What I would like to be able to do is scroll to the latest inserted row/item in the table, right now when a new item is inserted the user may not see it since the items are alphabetically reordered and the latest item may not be visible on the tableView.
How can I scroll to the latest inserted row/item in a UITableView?
Realm Objects:
class Item:Object{
#objc dynamic var productName:String = ""
#objc dynamic var isItemActive = true
#objc dynamic var createdAt = NSDate()
}
class ItemList: Object {
#objc dynamic var listName = ""
#objc dynamic var createdAt = NSDate()
let items = List<Item>()
}
UITableView:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
var allItems : Results<Item>!
var groceryList : ItemList!
override func viewDidLoad() {
super.viewDidLoad()
groceryList = realm.objects(ItemList.self).filter("listName = %#", "groceryList").first
updateResultsList()
}
func updateResultsList(){
if let list = groceryList{
allItems = activeList.items.sorted(byKeyPath: "productName", ascending: false)
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reusableCell", for: indexPath) as! CustomCell
let data = allItems[indexPath.row]
cell.displayProductName.text = data.productName
return cell
}
}
You can use Realm notifications to know when the data source Results has been modified, then update your table view from there and do the scrolling as well.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var allItems: Results<Item>!
var groceryList: ItemList!
var notificationToken: NotificationToken? = nil
deinit {
notificationToken?.invalidate()
}
override func viewDidLoad() {
super.viewDidLoad()
groceryList = realm.objects(ItemList.self).filter("listName = %#", "groceryList").first
updateResultsList()
observeGroceryList
}
func updateResultsList(){
if let list = groceryList {
allItems = activeList.items.sorted(byKeyPath: "productName", ascending: false)
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reusableCell", for: indexPath) as! CustomCell
let data = allItems[indexPath.row]
cell.displayProductName.text = data.productName
return cell
}
func observeGroceryList() {
notificationToken = allItems.observe { [weak self] (changes: RealmCollectionChange) in
switch changes {
case .initial:
self?.tableView.reloadData()
case .update(_, let deletions, let insertions, let modifications):
// Query results have changed, so apply them to the UITableView
self?.tableView.beginUpdates()
self?.tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
self?.tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
with: .automatic)
self?.tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
self?.tableView.endUpdates()
if let lastInsertedRow = insertions.last {
self?.tableView.scrollToRow(at: insertions.last, at: .none, animated: true)
}
case .error(let error):
// An error occurred while opening the Realm file on the background worker thread
print("\(error)")
}
}
}
}
Add below code as extension of tableview.
extension UITableView {
func scrollToBottom() {
let sections = numberOfSections-1
if sections >= 0 {
let rows = numberOfRows(inSection: sections)-1
if rows >= 0 {
let indexPath = IndexPath(row: rows, section: sections)
DispatchQueue.main.async { [weak self] in
self?.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
}
}
}
}
Now simply use it in your method:
func updateResultsList(){
if let list = groceryList{
allItems = activeList.items.sorted(byKeyPath: "productName", ascending: false
yourTableView.scrollToBottom()
}
}
Just use this method where you want, it should be scroll down.
yourTableView.scrollToBottom()

Selected row from each section of UITableView ( Multiple Selection )

I have used tableview(grouped).
So i need to select one row from the each section of UITableviewSection.
So for that i have tableview and one submit button .So i need to check when i click on the submit button i need to check whether i have selected one row from the each section ,if not then show alert as not selected the section number.How to check?
This is my data.
{
"data":[
{
"question": "Gender",
"options": ["Male","Female"]
},
{
"question": "How old are you",
"options": ["Under 18","Age 18 to 24","Age 25 to 40","Age 41 to 60","Above 60"]
},
{
"question": "I am filling the Questionnaire for?",
"options": ["Myself","Mychild","Partner","Others"]
}
]
}
QuestionModel:-
class QuestionListModel: NSObject {
var selected = false
var dataListArray33:[NH_OptionsModel] = []
var id:Int!
var question:String!
var buttontype:String!
var options:[String]?
var v:String?
var optionsModelArray:[OptionsModel] = []
init(dictionary :JSONDictionary) {
guard let question = dictionary["question"] as? String,
let typebutton = dictionary["button_type"] as? String,
let id = dictionary["id"] as? Int
else {
return
}
if let options = dictionary["options"] as? [String]{
print(options)
print(options)
for values in options{
print(values)
let optionmodel = OptionsModel(values: values)
self.optionsModelArray.append(optionmodel)
}
}
self.buttontype = typebutton
self.question = question
self.id = id
// print(self.dataListArray33)
}
}
optionModel:-
class OptionsModel: NSObject {
var isSelected:Bool? = false
var v:String?
var values:String?
init(values:String) {
self.values = values
print( self.values)
}
ViewModel:-
func numberOfSections(tableView: UITableView) -> Int{
print((datasourceModel.dataListArray?.count)!)
return (datasourceModel.dataListArray?.count)!
}
func titleForHeaderInSection(atsection section: Int) -> NH_QuestionListModel {
return datasourceModel.dataListArray![section]
}
func numberOfRowsIn(section:Int) -> Int {
print( datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0)
return datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0
// return self.questionsModelArray?[section].optionsModelArray.count ?? 0
}
func datafordisplay(atindex indexPath: IndexPath) -> NH_OptionsModel{
print(datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row])
return datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row]
}
func question(answer:String) {
print(questions)
questions.append(answer)
print(questions )
}
func questionlist(answer:String) {
print( questionlist )
questionlist.append(answer)
print( questionlist )
}
func answer(answer:String) {
answers.append(answer)
print(answers)
}
and finally viewController:-
func numberOfSections(in tableView: UITableView) -> Int {
return questionViewModel.numberOfSections(tableView: tableView)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let identifier = "HeaderCell"
var headercell: questionheader! = tableView.dequeueReusableCell(withIdentifier: identifier) as? questionheader
if headercell == nil {
tableView.register(UINib(nibName: "questionheader", bundle: nil), forCellReuseIdentifier: identifier)
headercell = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_questionheader
}
headercell.setReviewData(reviews:questionViewModel.titleForHeaderInSection(atsection:section))
return headercell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 150
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return questionViewModel.numberOfRowsIn(section: section)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "Cell"
var cell: QuestionListCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? QuestionListCell
if cell == nil {
tableView.register(UINib(nibName: "QuestionListCell", bundle: nil), forCellReuseIdentifier: identifier)
cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_QuestionListCell
}
cell.contentView.backgroundColor = UIColor.clear
let questionsModel = questionViewModel.titleForHeaderInSection(atsection:indexPath.section)
print(questionsModel.buttontype)
questionViewModel.button = questionsModel.buttontype
cell.setOptions(Options1: questionViewModel.datafordisplay(atindex: indexPath))
print("Section \(indexPath.section), Row : \(indexPath.row)")
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
print("Section \(indexPath.section), Row : \(indexPath.row)")
let cell = tableview.cellForRow(at: indexPath) as? NH_QuestionListCell
let model = questionViewModel.datafordisplay(atindex: indexPath)
print(model.isSelected)
cell?.setOptions(OptionsSelected:questionViewModel.datafordisplay(atindex: indexPath))
print(model.isSelected)
questionViewModel.isselected = model.isSelected!
let section = indexPath.section
let index = indexPath.row
print(section)
print(index)
if !questionViewModel.selectedIndexPaths.contains(indexPath) {
questionViewModel.selectedIndexPaths.append(indexPath)
print(questionViewModel.selectedIndexPaths.append(indexPath))
let questionModel = questionViewModel.titleForHeaderInSection(atsection: section)
print(questionModel.question)
questionViewModel.question = questionModel.question
questionViewModel.questionlist(answer: questionViewModel.question!)
let cell = tableview.cellForRow(at: indexPath) as? NH_QuestionListCell
let model = questionViewModel.datafordisplay(atindex: indexPath)
print(model.values)
questionViewModel.answer(answer: model.values!)
let value: Int = questionModel.id
let string = String(describing: value)
//let x: Int? = Int(model.id)
questionViewModel.question_id = string
questionViewModel.question(answer: questionViewModel.question_id!)
print(questionModel.id)
// append the selected index paths
} // if indexPath.section == section {
// questionViewModel.indexPath(indexPaths: index)
// }
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let index = questionViewModel.selectedIndexPaths.index(of: indexPath) {
print(index)
questionViewModel.selectedIndexPaths.remove(at: index)
}
}
According to this i got the output .
But i have button action in viewcontroller.
#IBAction func forward(_ sender: AnyObject) {
}
In this button action i need to check whether from each section did i selected one row or not .if not show alert .How to do
my current didselect method :-
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let cell = tableview.cellForRow(at: indexPath) as? NH_QuestionListCell
let model = questionViewModel.datafordisplay(atindex: indexPath)
print(model.isSelected)
cell?.setOptions(OptionsSelected:questionViewModel.datafordisplay(atindex: indexPath))
print(model.isSelected)
questionViewModel.isselected = model.isSelected!
let section = indexPath.section
let index = indexPath.row
print(section)
print(index)
if !questionViewModel.selectedIndexPaths.contains(indexPath) {
questionViewModel.selectedIndexPaths.append(indexPath)
print(questionViewModel.selectedIndexPaths.append(indexPath))
let questionModel = questionViewModel.titleForHeaderInSection(atsection: section)
print(questionModel.question)
questionViewModel.question = questionModel.question
questionViewModel.questionlist(answer: questionViewModel.question!)
let cell = tableview.cellForRow(at: indexPath) as? NH_QuestionListCell
let model = questionViewModel.datafordisplay(atindex: indexPath)
print(model.values)
questionViewModel.answer(answer: model.values!)
let value: Int = questionModel.id
let string = String(describing: value)
//let x: Int? = Int(model.id)
questionViewModel.question_id = string
questionViewModel.question(answer: questionViewModel.question_id!)
print(questionModel.id)
}
I have 3 array
According to this didselect method:-
ex:-for section 1 :-i selected 1st row so the data append as below.
questionlist:["How r u?"]
answelist:["fine"]
But suppose i think that i need 2nd indexpath ,so i need to remove the previous appended data from arrays and append the current data .As below:
questionlist:["How r u?"]
answelist:["not well"]
And next for section 2 : i selected 1st indexpath.row data .then that data is append.So i need to get as below:-
questionlist:["How r u?","Gender"]
answelist:["not well","Male"]
Here selecting i think that i need the 2nd option then remove the added indexpath.row data from array and show as:-
questionlist:["How r u?","Gender"]
answelist:["not well","Female"]
Such way how to set?
you can update your model based on the selection like
"data":[
{
"question": "Gender",
"options": ["Male","Female"],
"optionSelected": "Male"
}
]
and on Submit , check data for selections
The table view has a property to get selected index paths. You can use all native components for that. What you need is to deselect an item at index path where one is already selected in a certain section. You also just need to then check that the number of selected index paths is the same as number of arrays in your data source.
Check something like this:
var dataSource: [[Any]]!
var tableView: UITableView!
func didSelectRowAt(_ indexPath: IndexPath) {
guard let selectedPaths = tableView.indexPathsForSelectedRows else { return } // We need to have selected paths
guard selectedPaths.contains(indexPath) == false else { return } // The same cell being selected
let previouslySelectedCellIndexPaths: [IndexPath] = selectedPaths.filter { $0.section == indexPath.section && $0 != indexPath } // Getting all selected index paths within this section
previouslySelectedCellIndexPaths.forEach { tableView.deselectRow(at: $0, animated: true) } // Deselect waht was previously selected
}
/// Will return array of selected objects only if all sections have a selected index
///
/// - Returns: A result array
func getSelectionData() -> [Any]? {
guard let selectedPaths = tableView.indexPathsForSelectedRows else { return nil } // We need to have selected paths
guard selectedPaths.count == dataSource.count else { return nil } // This should prevent missing selections assuming all index paths are unique in sections
return selectedPaths.map { dataSource[$0.section][$0.row] } // Map selected index paths back to objects
}
I tried to use kind of minimum code to show all of this. It is all commented so you can see row by row what goes on.
You might want to check is all sections are unique the second method but it is not needed if the first one is always used.
You can store selected indexPath in an array. OnClick of submit just loop through array and check either at least one element is from each section.
FYI : indexPath contains section info also.
Declare an mutable array and allocate in viewDidLoad.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[anArray addObject:indexPath];
}
on Submit action follow this, you can improvise based on your requirement
-(void)onSubmitAction{
[anArray addObject:indexPath];
NSMutableArray *countOfSection=[[NSMutableArray alloc]init];
for (NSIndexPath*indexPath in anArray ) {
if(![anArray containsObject:indexPath.section])
[countOfSection addObject:indexPath.section];
}
if(countOfSection.count == self.tableview.numberOfSections){
//write your code
}else{
// show alert
}
}
Step 1 : Create Global Variable
var selectedIndexPaths = [IndexPath]()
Step 2: Add UITableView Property
tableView.allowsMultipleSelection = true
Step 3 : Implement the delegate methods
//On Selection
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedIndexPathAtCurrentSection = selectedIndexPaths.filter({ $0.section == indexPath.section})
for indexPath in selectedIndexPathAtCurrentSection {
tableView.deselectRow(at: indexPath, animated: true)
if let indexOf = selectedIndexPaths.index(of: indexPath) {
selectedIndexPaths.remove(at: indexOf)
}
}
selectedIndexPaths.append(indexPath)
}
// On DeSelection
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let index = selectedIndexPaths.index(of: indexPath) {
selectedIndexPaths.remove(at: index)
}
}
Step 4: Getting Selected IndexPaths with sections
#IBAction func forward(sender:Any){
let totalSections = questionViewModel.numberOfSections(tableView: tableView)
for section in 0..<totalSections {
if (selectedIndexPaths.filter({ $0.section == section}).count >= 1) {
continue
} else {
// Show alert
print("Please select item at",(section))
return
}
}
}

Reload tableView data from UIAlertController not working

I'm having trouble refreshing tableView data from a UIAlertController.
The code is for a quiz-style app and this page lets the user choose
subjects as well as some other options (see screenshots). There is a
reset button next to "Show only unseen questions" which triggers a
UIAlertController. However, clicking the Reset action in this alert
updates the database but doesn't update the tableView. The database is
definitely updated as if I go back a page and then revisit the
tableView, the unseen question values in the subject cells are updated. I realise there's quite a few of
these type of questions here but I'm afraid none of the usual fixes are
working.
Extra info:
The tableView is customised with a series of custom
UITableViewCells
Data is loaded from a SQLite database through FMDB
The UIAlertController is triggered from a NSNotification when the reset
button is clicked
So far I have:
Checked datasource and delegates set correctly, programmatically and
in IB. Confirmed with print(self.tableView.datasource) etc
Confirmed reloadData() is firing
Using main thread for reloadData()
Extract of TableViewController code and screenshots below.
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
//For unique question picker changed
NotificationCenter.default.addObserver(self, selector: #selector(SubjectsTableViewController.reloadView(_:)), name:NSNotification.Name(rawValue: "reload"), object: nil)
//For slider value changed
NotificationCenter.default.addObserver(self, selector: #selector(SubjectsTableViewController.updateQuantity(_:)), name:NSNotification.Name(rawValue: "updateQuantity"), object: nil)
//Trigger UIAlertController
NotificationCenter.default.addObserver(self, selector: #selector(SubjectsTableViewController.showAlert(_:)), name:NSNotification.Name(rawValue: "showAlert"), object: nil)
}
// MARK: - Table view data source
///////// Sections and Headers /////////
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let subjectHeaderCell = tableView.dequeueReusableCell(withIdentifier: "sectionHeader")
switch section {
case 0:
subjectHeaderCell?.textLabel?.text = "Select Subjects"
return subjectHeaderCell
case 1:
subjectHeaderCell?.textLabel?.text = "Options"
return subjectHeaderCell
case 2:
subjectHeaderCell?.textLabel?.text = ""
return subjectHeaderCell
default:
subjectHeaderCell?.textLabel?.text = ""
return subjectHeaderCell
}
}
//Header heights
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
return 34.0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return SubjectManager.subjectWorker.countSubjects()
case 1:
return 2
case 2:
return 1
default:
return 0
}
}
///////// Rows within sections /////////
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch (indexPath.section) {
case 0:
//Configure subjectCell //
let cellWithSubject = tableView.dequeueReusableCell(withIdentifier: "subjectCell", for: indexPath) as! SubjectTableViewCell
//Curve corners
cellWithSubject.subjectCellContainer.layer.cornerRadius = 2
cellWithSubject.subjectCellContainer.layer.masksToBounds = true
//Set subject title label
cellWithSubject.subjectTitleLabel.text = SubjectManager.subjectWorker.collateSubjectTitles()[indexPath.row]
//Available questions for subject label
questionCountForSubjectArray = QuestionManager.questionWorker.countQuestions()
cellWithSubject.subjectAvailableQuestionsLabel.text = "Total questions available: \(questionCountForSubjectArray[indexPath.row])"
//Get questions in subject variables
seenQuestionsForSubjectArray = QuestionManager.questionWorker.countOfQuestionsAlreadySeen()
//New questions available label
unseenQuestionsForSubjectArray.append(questionCountForSubjectArray[indexPath.row] - seenQuestionsForSubjectArray[indexPath.row])
cellWithSubject.newQuestionsRemainingLabel.text = "New questions remaining: \(unseenQuestionsForSubjectArray[indexPath.row])"
return cellWithSubject
case 1:
switch (indexPath.row) {
case 0:
//Configure uniqueQuestionCell //
let cellWithSwitch = tableView.dequeueReusableCell(withIdentifier: "uniqueQuestionCell", for: indexPath) as! UniqueQuestionTableViewCell
//Curve corners
cellWithSwitch.uniqueQuestionContainer.layer.cornerRadius = 2
cellWithSwitch.uniqueQuestionContainer.layer.masksToBounds = true
return cellWithSwitch
case 1:
//Configure sliderCell //
let cellWithSlider = tableView.dequeueReusableCell(withIdentifier: "questionPickerCell", for: indexPath) as! QuestionPickerTableViewCell
//Curve corners
cellWithSlider.pickerCellContainer.layer.cornerRadius = 2
cellWithSlider.pickerCellContainer.layer.masksToBounds = true
//Set questions available label
cellWithSlider.questionsAvailableLabel.text = "Available: \(sumQuestionsSelected)"
//Configure slider
cellWithSlider.questionPicker.maximumValue = Float(sumQuestionsSelected)
cellWithSlider.questionPicker.isContinuous = true
//Logic for if available questions changes - updates slider stuff
if questionQuantityFromSlider > sumQuestionsSelected {
questionQuantityFromSlider = sumQuestionsSelected
cellWithSlider.questionsToStudy = questionQuantityFromSlider
cellWithSlider.questionsChosenLabel.text = "Questions to study: \(questionQuantityFromSlider)"
} else { questionQuantityFromSlider = cellWithSlider.questionsToStudy
}
//Configure questions chosen label:
if questionsToStudyDict.isEmpty {
cellWithSlider.chooseSubjectsLabel.text = "Choose a subject"
cellWithSlider.questionsChosenLabel.text = "Questions to study: 0"
} else {
cellWithSlider.chooseSubjectsLabel.text = ""
}
return cellWithSlider
default:
return UITableViewCell()
}
case 2:
print("cellForRowAt case 2")
//Configure beginCell //
let cellWithStart = tableView.dequeueReusableCell(withIdentifier: "beginCell", for: indexPath) as! BeginStudyTableViewCell
//Curve corners
cellWithStart.startContainer.layer.cornerRadius = 2
cellWithStart.startContainer.layer.masksToBounds = true
return cellWithStart
default:
return UITableViewCell()
}
}
//Row heights
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch (indexPath.section) {
case 0:
return 120.0
case 1:
switch (indexPath.row) {
case 0:
return 60.0
case 1:
return 100.0
default:
return 44.0
}
case 2:
return 100.0
default:
return 44.0
}
}
override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
if indexPath.section == 2 || indexPath.section == 0 {
return true
} else {
return false
}
}
override func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
if indexPath.section == 2 && selectedRowsDict.isEmpty != true && questionQuantityFromSlider > 0 {
let cellToBegin = tableView.cellForRow(at: indexPath) as! BeginStudyTableViewCell
cellToBegin.startContainer.backgroundColor = UIColor.lightGray
}
}
override func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
if indexPath.section == 2 {
let cellToBegin = tableView.cellForRow(at: indexPath) as! BeginStudyTableViewCell
cellToBegin.startContainer.backgroundColor = UIColor.white
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch (indexPath.section) {
case 0:
//Set checkbox to ticked image
let cellWithSubject = tableView.cellForRow(at: indexPath) as! SubjectTableViewCell
cellWithSubject.subjectSelectedImageView.image = UIImage(named: "CheckboxTicked")
//Determine questions available for subject depending on unseen value
if showUnseenQuestions == true {
questionsToStudyDict[indexPath.row] = unseenQuestionsForSubjectArray[indexPath.row]
} else {
questionsToStudyDict[indexPath.row] = questionCountForSubjectArray[indexPath.row]
}
//Sum questions available
sumQuestionsSelected = Array(questionsToStudyDict.values).reduce(0, +)
//Reload table to pass this to questions available label in UISlider cell and reselect selected rows
let key: Int = indexPath.row
selectedRowsDict[key] = indexPath.row
self.tableView.reloadData()
if selectedRowsDict.isEmpty == false {
for (keys,_) in selectedRowsDict {
let index: IndexPath = NSIndexPath(row: selectedRowsDict[keys]!, section: 0) as IndexPath
tableView.selectRow(at: index, animated: false, scrollPosition: .none)
}
}
case 1:
break
case 2:
if selectedRowsDict.isEmpty != true && questionQuantityFromSlider > 0 {
self.performSegue(withIdentifier: "showStudyQuestion", sender: self)
} else {
print("Segue not fired")
}
default:
break
}
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
//Set checkbox to unticked image
let cellWithSubject = tableView.cellForRow(at: indexPath) as! SubjectTableViewCell
cellWithSubject.subjectSelectedImageView.image = UIImage(named: "Checkbox")
//Remove questions available for unselected subject from questions dictionary
questionsToStudyDict[indexPath.row] = nil
//Update sum of questions selected
sumQuestionsSelected = Array(questionsToStudyDict.values).reduce(0, +)
//Reload table to pass this to questions available label in UISlider cell and reselect selected rows
let key: Int = indexPath.row
selectedRowsDict[key] = nil
self.tableView.reloadData()
if selectedRowsDict.isEmpty == false {
for (keys,_) in selectedRowsDict {
let index: IndexPath = NSIndexPath(row: selectedRowsDict[keys]!, section: 0) as IndexPath
tableView.selectRow(at: index, animated: false, scrollPosition: .none)
}
}
}
}
func reloadView(_ notification: Notification) {
//Change bool value
showUnseenQuestions = !showUnseenQuestions
//For keys in dict, update values according to showUnseenQuestion value
if showUnseenQuestions == true {
for (key,_) in questionsToStudyDict {
questionsToStudyDict[key] = unseenQuestionsForSubjectArray[key]
}
} else {
for (key,_) in questionsToStudyDict {
questionsToStudyDict[key] = questionCountForSubjectArray[key]
}
}
//Re-run sum dict function
sumQuestionsSelected = Array(questionsToStudyDict.values).reduce(0, +)
//Finally reload the view and reselect selected rows
let selectedRowsIndexes = tableView.indexPathsForSelectedRows
self.tableView.reloadData()
if selectedRowsIndexes != nil {
for i in (selectedRowsIndexes)! {
tableView.selectRow(at: i, animated: false, scrollPosition: .none)
}
}
}
func updateQuantity(_ notification: Notification) {
//Reload the view and reselect selected rows
let selectedRowsIndexes = tableView.indexPathsForSelectedRows
self.tableView.reloadData()
if selectedRowsIndexes != nil {
for i in (selectedRowsIndexes)! {
tableView.selectRow(at: i, animated: false, scrollPosition: .none)
}
}
}
func showAlert(_ notification: Notification) {
let alertController = UIAlertController(title: "Reset Seen Questions", message: "Are you sure you want to reset all questions to unseen?", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { action in
// ...
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "Reset", style: .default, handler:{(action:UIAlertAction) -> Void in
QuestionManager.questionWorker.resetHasSeenValues()
self.reloadData()
print("reloadData fired")
})
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
func reloadData() {
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
func countOfQuestionsAlreadySeen() -> [Int] {
var questionSeenYesArray: [Int] = []
if openWriteDatabase() {
let queryYes = "SELECT SUM(hasSeen) FROM UserData GROUP BY subjectID"
let querySeenYes: FMResultSet? = writeDatabase?.executeQuery(queryYes, withArgumentsIn: nil)
while (querySeenYes?.next())! {
if let questionSeenYes = (querySeenYes?.int(forColumnIndex: 0)) {
questionSeenYesArray.append(Int(questionSeenYes))
}
}
}
return questionSeenYesArray
}
func resetHasSeenValues() {
if openWriteDatabase() {
let resetHasSeenValues = "UPDATE UserData Set hasSeen = 0"
_ = writeDatabase?.executeUpdate(resetHasSeenValues, withArgumentsIn: nil)
}
}
Some more debugging revealed that the unseenQuestionsForSubjectArray wasn't being populated correctly in the cellForRowAt method. I fixed this, and this fixed the reloadData() issue. Thanks all for the help.
add self.reloadData() inside main queue. Outlet changes always should be held inside main thread.
let OKAction = UIAlertAction(title: "Reset", style: .default, handler:{(action:UIAlertAction) -> Void in
QuestionManager.questionWorker.resetHasSeenValues()
DispatchQueue.main.async {
self.tableView.reloadData()
}
print("reloadData fired")
})

Swift/iOS: Collapsing a section in a UITableView

I have a UITableView with about 5 sections. I am trying to collapse and expand one of those section by the click of a button, but I am seeing an issue where the code I'm using to do so results in the collapsing of other sections as well. Specifically, the first row of all visible sections are collapsed.
Here is what that code looks like:
func didClickSectionCollapseButton() {
shouldCollapseSection = !shouldCollapseSection
tableView.beginUpdates()
tableView.reloadSections(NSIndexSet(index: 1), withRowAnimation: .Fade)
tableView.endUpdates()
}
And here is the numberOfRowInSection method:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 1
case 1:
// collapsible section
return shouldCollapse ? 0 : collapsibleSectionCellCount
case 2:
return getCellCount()
case 3:
return 1
case 4:
return 1
default:
return 0
}
}
Is there anything I'm missing here? I've gone through various tutorials and questions, but I haven't been able to find a solution yet.
Hi after a lot of research, i found a solution which worked for me perfectly using storyboard.
View controller code:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tblView: UITableView!
var sections = ["section1","section2","section3"]
var cells = ["cell1","cell2","cell3","cell4"]
var selectedIndx = -1
var thereIsCellTapped = false
override func viewDidLoad() {
super.viewDidLoad()
tblView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 2
case 1:
return 3
default:
return 4
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == selectedIndx && thereIsCellTapped{
return 50
}else{
return 0
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "SectionTableViewCell") as! SectionTableViewCell
headerCell.lblHeader.text = sections[section]
headerCell.btnSelection.tag = section
headerCell.btnSelection.addTarget(self, action: #selector(ViewController.btnSectionClick(sender:)), for: .touchUpInside)
return headerCell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ExpandeTableViewCell") as! ExpandeTableViewCell
cell.lblCell.text = cells[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.section)
}
#objc func btnSectionClick(sender:UIButton!){
print("selected index",sender.tag)
if selectedIndx != sender.tag {
self.thereIsCellTapped = true
self.selectedIndx = sender.tag
}
else {
// there is no cell selected anymore
self.thereIsCellTapped = false
self.selectedIndx = -1
}
tblView.reloadData()
}
}
If you don't want to do select and unselect on the same selection then, see code below.
#objc func btnSectionClick(sender:UIButton!){
print("selected index",sender.tag)
selectedIndx = sender.tag
tblView.reloadData()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == selectedIndx{
return 50
}else{
return 0
}
}
It works for me, i referred lot of answers and made it. I hope it will help you.
I've Used this code long time ago it is in Swift 2.3. I don't know if this will help or not but worth to mention it.
class DriversVC : UIViewController , UITableViewDelegate , UITableViewDataSource {
//-----------------------------------------------------------------------
//MARK: - Outlets
#IBOutlet var tvDriverList: UITableView! {
didSet {
tvDriverList.delegate = self
tvDriverList.dataSource = self
}
}
//-----------------------------------------------------------------------
//MARK: - Variables
var arrDriverList : NSArray? //Section data
var arrWorkerList : NSArray? //Section data
var collapseSection0 : Bool = false
var collapseSection1 : Bool = false
var btnSection0Headder : UIButton = UIButton()
var btnSection1Headder : UIButton = UIButton()
//------------------------------------------------------
func btnSection0HeadderTapped () {
if collapseSection0 {
collapseSection0 = false
} else {
collapseSection0 = true
}
tvDriverList.reloadSections(NSIndexSet(index: 0), withRowAnimation: UITableViewRowAnimation.Fade)
}
//------------------------------------------------------
func btnSection1HeadderTapped () {
if collapseSection1 {
collapseSection1 = false
} else {
collapseSection1 = true
}
tvDriverList.reloadSections(NSIndexSet(index: 1), withRowAnimation: UITableViewRowAnimation.Fade)
}
//-----------------------------------------------------------------------------------
//MARK:- Table delegate and data sources
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
//------------------------------------------------------
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20
}
//------------------------------------------------------
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 50))
view.backgroundColor = OrangeColor //Set your color
let lbl = UILabel(frame: CGRect(x: 10, y: 5, width: UIScreen.mainScreen().bounds.width - 20, height: 40))
lbl.font = UIFont(name: OpenSansRegular, size: 18) //Set your font
lbl.textColor = UIColor.whiteColor()
view.addSubview(lbl)
if section == 0 {
lbl.text = "D R I V E R"
btnSection0Headder.addTarget(self, action: #selector(self.btnSection0HeadderTapped), forControlEvents: .TouchUpInside)
btnSection0Headder.frame = view.frame
view.addSubview(btnSection0Headder) // uncomment to apply collapse effect
} else {
lbl.text = "W O R K E R"
btnSection1Headder.addTarget(self, action: #selector(self.btnSection1HeadderTapped), forControlEvents: .TouchUpInside)
btnSection1Headder.frame = view.frame
view.addSubview(btnSection1Headder) // uncomment to apply collapse effect
}
return view
}
//------------------------------------------------------
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
//------------------------------------------------------
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if arrWorkerList != nil && arrWorkerList?.count > 0 {
return 2
}
return 1
}
//------------------------------------------------------
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
if !collapseSection0 {
guard arrDriverList != nil else {return 0}
return arrDriverList!.count
} else {
return 0
}
} else {
if !collapseSection1 {
guard arrWorkerList != nil else {return 0}
return arrWorkerList!.count
} else {
return 0
}
}
}
//------------------------------------------------------
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCellWithIdentifier(NSStringFromClass(DriversCVC).componentsSeparatedByString(".").last!) as? DriversCVC else { fatalError("unexpected DriversCVC dequeued from tableView") }
cell.superViewController = self
if indexPath.section == 0 {
guard let dict = arrDriverList![indexPath.row] as? NSDictionary else {return cell}
cell.data = dict
} else {
guard let dict = arrWorkerList![indexPath.row] as? NSDictionary else {return cell}
cell.data = dict
}
cell.setup()
return cell
}
//----------------------------------------------------------------------
//MARK: - Action Method
#IBAction func btnBackTapped(sender: AnyObject) {
guard self.navigationController != nil else {
self.dismissViewControllerAnimated(true, completion: nil)
return
}
guard self.navigationController?.popViewControllerAnimated(true) != nil else {
guard self.navigationController?.dismissViewControllerAnimated(true, completion: nil) != nil else {
AppDelegate.sharedInstance().loginCall()
return
}
return
}
}
//-----------------------------------------------------------------------
//MARK: - View Life Cycle Methods
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//----------------------------------------------------------------------
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
setUpVC()
}
//----------------------------------------------------------------------
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
} }
You can use:
func didClickSectionCollapseButton() {
shouldCollapseSection = !shouldCollapseSection
tableView.beginUpdates()
tableView.deleteSections(NSIndexSet(index: 1), withRowAnimation: .Fade)
tableView.endUpdates()
}
beginUpdates() and endUpdates() works in pair if you want subsequent insertions, deletion, and selection operations, but not for the reloadData.
In your code, remove beginUpdates() and endUpdates().
Is there a difference between the shouldCollapseSection variable being set in the button action and the shouldCollapse variable used in the numberOfRowsInSection method ?
It would seem that you are not setting the same variable you are using in the data source delegate.

Resources