I have one UITableView and it has also header section. Based on header section they have correspondent data.
Each header section have one button which is clickable as user will
click on it it will expend the UITableView
I am using below code for it
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = Bundle.main.loadNibNamed("DashTableViewCell", owner: self, options: nil)?.first as! DashTableViewCell
headerCell.lblQueryType.text = queriesArray[section]
headerCell.btnExpendable.addTarget(self, action: #selector(expendSection), for: .touchUpInside)
headerCell.btnExpendable.tag = section
return headerCell
}
#objc func expendSection(button: UIButton) {
var indexPaths = [IndexPath]()
let section = button.tag
if button.tag == 0 {
QueriesStructArray[section].isExpended = !QueriesStructArray[section].isExpended
for row in QueriesStructArray.indices {
let indexPath = IndexPath(row: row, section: section)
indexPaths.append(indexPath)
}
} else if button.tag == 1 {
MyQueriesStructArray[section].isExpended = !MyQueriesStructArray[section].isExpended
for row in MyQueriesStructArray.indices {
let indexPath = IndexPath(row: row, section: section)
indexPaths.append(indexPath)
}
}
if button.tag == 1 {
if MyQueriesStructArray[section].isExpended == true {
tblProjectDashBoard.deleteRows(at: indexPaths, with: .fade)
} else {
tblProjectDashBoard.insertRows(at: indexPaths, with: .fade)
}
}
else if button.tag == 0 {
if QueriesStructArray[section].isExpended == true {
tblProjectDashBoard.deleteRows(at: indexPaths, with: .fade)
} else {
tblProjectDashBoard.insertRows(at: indexPaths, with: .fade)
}
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return queriesArray.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
if QueriesStructArray[section].isExpended == true {
return 0
}
return QueriesStructArray.count
} else if section == 1 {
if MyQueriesStructArray[section].isExpended == true {
return 0
} else {
return MyQueriesStructArray.count
}
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "ProjectDashTableViewCell", for: indexPath) as? ProjectDashTableViewCell {
if indexPath.section == 0 {
cell.lblTitle.text = QueriesStructArray[indexPath.row].queries
return cell
} else if indexPath.section == 1 {
cell.lblTitle.text = MyQueriesStructArray[indexPath.row].myQueries
return cell
}
}
return UITableViewCell()
}
every thing is working fine but as I clicked on "Shared Queries"
button then "My Queries" section got hide which is wrong I am unable
to find the issue in code
Related
I have to show tableview with two sections based on flag value. Based on flag value I have to show/hide first section.
First section has only one row and static customised cell which will show always same data.
And second section is another customised cell, Which is dynamic rows shows from server data.
I need to show Second section is always. First section based on flag I have to show or hide.
How to handle this?
Here is my code
override func viewDidLoad() {
super.viewDidLoad()
self.registerCells()
}
func registerCells(){
self.DetailsTableview.register(RadioButtonTableViewCell.nib, forCellReuseIdentifier: RadioButtonTableViewCell.identifier)
if flagValue == true {
self.DetailsTableview.register(UPPercentageCell.nib, forCellReuseIdentifier: PercentageCell.identifier)
}
}
func numberOfSections(in tableView: UITableView) -> Int {
if flagValue == true {
return 2
}
return 1
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if flagValue == true {
return 20
}
return 40
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if flagValue == true {
if section == 0 {
return 1
} else {
return self.response?.data?.count ?? 0
}
}
return self.response?.data?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if flagValue == true {
if indexPath.section == 0 {
let percentagecell: PercentageCell = tableView.dequeueReusableCell(withIdentifier: "PercentageCell", for: indexPath) as! UPPercentageCell
percentagecell.percentage = "20" //some dynamic value
percentagecell.isUserInteractionEnabled = false
return percentagecell
} else {
let cell: RadioButtonTableViewCell = tableView.dequeueReusableCell(withIdentifier: "RadioButtonTableViewCell", for: indexPath) as! RadioButtonTableViewCell
cell.displayDataToUI(title: response?.data?[indexPath.row] ?? "", currentIndexpath: indexPath, selectedIndexpath: selectedIndexpath ?? IndexPath())
cell.radioButtonClicked = {
[weak self] (indexpath) in
self?.saveButton.isUserInteractionEnabled = true
self?.reloadTableviewFromSelectedIndexpath(indexpath: indexpath)
}
return cell
}
} else {
let cell: RadioButtonTableViewCell = tableView.dequeueReusableCell(withIdentifier: "RadioButtonTableViewCell", for: indexPath) as! RadioButtonTableViewCell
cell.displayDataToUI(title: response?.data?[indexPath.row] ?? "", currentIndexpath: indexPath, selectedIndexpath: selectedIndexpath ?? IndexPath())
cell.radioButtonClicked = {
[weak self] (indexpath) in
self?.saveButton.isUserInteractionEnabled = true
self?.reloadTableviewFromSelectedIndexpath(indexpath: indexpath)
}
return cell
}
return UITableViewCell()
}
Is there any better approach to achieve this?
Highly appreciate your valuable suggestions.
Your approach is fine, but you can simplify things a bit with fewer if/else blocks...
First:
func registerCells(){
self.DetailsTableview.register(RadioButtonTableViewCell.nib, forCellReuseIdentifier: RadioButtonTableViewCell.identifier)
// doesn't hurt to go ahead and register both cell classes for reuse
self.DetailsTableview.register(UPPercentageCell.nib, forCellReuseIdentifier: PercentageCell.identifier)
}
next, just a more compact way of writing it:
override func numberOfSections(in tableView: UITableView) -> Int {
return flagValue ? 2 : 1
}
and:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// this will only be true if
// section is 0
// AND
// flagValue is true
if section == 0 && flagValue {
return 1
}
// whether we're in section 0 WITHOUT the "top" section, or
// we're in section 1 WITH the "top" section
return self.response?.data?.count ?? 0
}
and:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// this will only be true if
// section is 0
// AND
// flagValue is true
if indexPath.section == 0 && flagValue {
let cell = tableView.dequeueReusableCell(withIdentifier: UPPercentageCell.identifier, for: indexPath) as! UPPercentageCell
cell.percentage = "20" //some dynamic value
cell.isUserInteractionEnabled = false
return cell
}
// we want to display the same cells from the same data array
// whether we're in section 0 WITHOUT the "top" section, or
// we're in section 1 WITH the "top" section
let cell = tableView.dequeueReusableCell(withIdentifier: RadioButtonTableViewCell.identifier, for: indexPath) as! RadioButtonTableViewCell
cell.displayDataToUI(title: response?.data?[indexPath.row] ?? "", currentIndexpath: indexPath, selectedIndexpath: selectedIndexpath ?? IndexPath())
cell.radioButtonClicked = {
[weak self] (indexpath) in
self?.saveButton.isUserInteractionEnabled = true
self?.reloadTableviewFromSelectedIndexpath(indexpath: indexpath)
}
return cell
}
I have 3 sections and their corresponding cells how to make it expand/collapse? I have created an struct having expanded:Bool value
below are my code.
*THIS IS MY cellForRowAt *
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch (indexPath.section) {
case 0:
if let cell1 = tableview.dequeueReusableCell(withIdentifier: "TimeCardDetailedTableViewCell", for: indexPath) as? TimeCardDetailedTableViewCell{
return cell1
}
case 1:
if let cell2 = tableview.dequeueReusableCell(withIdentifier: "MessageDetailedTableViewCell", for: indexPath) as? MessageDetailedTableViewCell{
return cell2
}
case 2:
if let cell3 = tableview.dequeueReusableCell(withIdentifier: "CrewDetailedTableViewCell", for: indexPath) as? CrewDetailedTableViewCell{
return cell3
}
default:
return UITableViewCell()
}
return UITableViewCell()
}
--->>this is numberOfRowsInSection
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch (section) {
case 0:
return self.timecards.count
case 1:
return self.msglog.count
default:
return self.profile.count
}
}
--->>> viewForHeaderInSection
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0{
let header1 = Bundle.main.loadNibNamed("TimeCardHeaderTableViewCell", owner: self, options: nil)?.last as! TimeCardHeaderTableViewCell
header1.backgroundColor = .red
return header1
} else if section == 1 {
let header2 = Bundle.main.loadNibNamed("MessageTableViewCell", owner: self, options: nil)?.last as! MessageTableViewCell
return header2
} else if section == 2 {
let header3 = Bundle.main.loadNibNamed("CrewTableViewCell", owner: self, options: nil)?.last as! CrewTableViewCell
return header3
}
return UITableViewCell()
}
How to implement expansion on section.im new to iOS
One approach is to update your numberOfRowsInSection like this
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch (section) {
case 0: return self.hiddenSections.contains(0) ? 0 : self.timecards.count
case 1: return self.hiddenSections.contains(1) ? 0 : return self.msglog.count
case 2: return self.hiddenSections.contains(2) ? 0 : return self.profile.count
}
}
viewForHeaderInSection will be like this, where you will set tag and selector.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionButton = UIButton()
sectionButton.setTitle(String(section),
for: .normal)
sectionButton.backgroundColor = .systemBlue
sectionButton.tag = section
sectionButton.addTarget(self,
action: #selector(self.hideSection(sender:)),
for: .touchUpInside)
return sectionButton
}
hideSection method will have logic to hide/show your section
#objc
private func hideSection(sender: UIButton) {
let section = sender.tag
func indexPathsForSection() -> [IndexPath] {
var indexPaths = [IndexPath]()
for row in 0..<self.tableViewData[section].count {
indexPaths.append(IndexPath(row: row,
section: section))
}
return indexPaths
}
if self.hiddenSections.contains(section) {
self.hiddenSections.remove(section)
self.tableView.insertRows(at: indexPathsForSection(),
with: .fade)
} else {
self.hiddenSections.insert(section)
self.tableView.deleteRows(at: indexPathsForSection(),
with: .fade)
}
}
I have filter which is done in dropdown table view in an view controller. The dropdown table view contains three section namely section 1, section 2 and section 3. For section 1 and section 3 should single selection and section 2 should be multiple selection. When tapping section 1 it expands table view cell and when tapping on section 2 will expand and section 1 will close the expansion.
When selecting the option from each section should stored even user close and reopens the filter dropdown table view.
I have four questions:
When user tap on different section it should automatically close already open sections?
Table view should adjust height and its position based number cells in each sections?
how to do multiple and single selection for three sections?
selected should be stored even if dropdown table view is close and reopened.
Here is the code which tried so far all question which i have mentioned above:
extension HomeViewController : UITableViewDelegate, UITableViewDataSource, ExpandableHeaderViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
if locationListBool == true {
return 1
} else {
return sectionss.count
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if locationListBool == true {
return autocompleteplaceArray.count
} else {
return sectionss[section].category.count
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if locationListBool == true {
return 0
} else {
return 30
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if locationListBool == true {
return 30
} else {
if (sectionss[indexPath.section].expanded) {
return 30
} else {
return 0
}
}
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if locationListBool == true {
return 0
} else {
return 2
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if locationListBool == true {
return nil
} else {
let header = ExpandableHeaderView()
header.contentView.backgroundColor = UIColor.white
header.customInit(title: sectionss[section].genre, section: section, delegate: self)
return header
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if locationListBool == true {
let cell = tableView.dequeueReusableCell(withIdentifier: "placecell", for: indexPath) as! locationNameTableViewCell
guard autocompleteplaceArray.count > 0 else {
return cell
}
cell.locationName.text = autocompleteplaceArray[indexPath.row]
return cell
} else {
let cell = dropDownTbl.dequeueReusableCell(withIdentifier: "dropDownCell", for: indexPath) as! dropDownCell
cell.dropDownLbl.text = sectionss[indexPath.section].category[indexPath.row]
cell.selectionStyle = .none
return cell
}
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.clear
if locationListBool == true {
let lastRowIndex = tableView.numberOfRows(inSection: 0)
if indexPath.row == lastRowIndex - 1 {
tableView.allowsSelection = true
} else {
tableView.allowsSelection = true
}
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if locationListBool == true {
if let indexPath = tableView.indexPathForSelectedRow {
let currentCell = tableView.cellForRow(at: indexPath) as! UITableViewCell
searchText.text = (currentCell.textLabel?.text)
searchText.text = autocompleteplaceArray[indexPath.row]
placeIDString = autocompletePlaceIDArray[indexPath.row]
print("placeIDString::::\(String(describing: placeIDString))")
if placeIDString != nil {
getPlaceIDLatLong(placeIDs: placeIDString!)
print("get lat long \(getPlaceIDLatLong(placeIDs: placeIDString!))")
}
// PrefsManager.sharedinstance.lastlocation = searchText.text
locationText = searchText.text
print("locationText::::\(String(describing: locationText))")
}
self.locationTableList.isHidden = true
}
else {
}
}
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
switch indexPath.section {
case 0:
if let previousIndexPath = indexPathOfSelectedRowPaidBy {
dropDownTbl.deselectRow(at: previousIndexPath as IndexPath, animated: false)
dropDownTbl.cellForRow(at: previousIndexPath as IndexPath)?.accessoryType = UITableViewCellAccessoryType.none
}
indexPathOfSelectedRowPaidBy = indexPath as NSIndexPath?
dropDownTbl.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
case 1:
dropDownTbl.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
default:
break
}
return indexPath
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)
{
switch indexPath.section {
case 0:
if let previousIndexPath = indexPathOfSelectedRowPaidBy {
dropDownTbl.deselectRow(at: previousIndexPath as IndexPath, animated: false)
dropDownTbl.cellForRow(at: previousIndexPath as IndexPath)?.accessoryType = UITableViewCellAccessoryType.none
}
indexPathOfSelectedRowPaidBy = nil
case 1:
dropDownTbl.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
default:
break
}
}
func toogleSection(header: ExpandableHeaderView, section: Int) {
sectionss[section].expanded = !sectionss[section].expanded
dropDownTbl.beginUpdates()
if sectionss[0].expanded{
dropDownTbl.layer.frame = CGRect(x: 15, y: 152, width: 345, height: 300)
} else if sectionss[1].expanded {
dropDownTbl.layer.frame = CGRect(x: 15, y: 152, width: 345, height: 230)
} else if sectionss[2].expanded {
dropDownTbl.layer.frame = CGRect(x: 15, y: 152, width: 345, height: 330)
} else {
dropDownTbl.layer.frame = CGRect(x: 15, y: 152, width: 345, height: 90)
}
for i in 0 ..< sectionss[section].category.count {
dropDownTbl.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
}
dropDownTbl.endUpdates()
}
}
Expandable table view header::
import UIKit
protocol ExpandableHeaderViewDelegate {
func toogleSection(header: ExpandableHeaderView, section: Int)
}
class ExpandableHeaderView: UITableViewHeaderFooterView {
var delegate: ExpandableHeaderViewDelegate?
var section: Int!
var collapaseHandlerArray = [String]()
let button = UIButton()
let button2 = UIButton()
override init(reuseIdentifier: String?){
super.init(reuseIdentifier: reuseIdentifier)
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectheaderAction)))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#objc func selectheaderAction(gestureRecognizer: UITapGestureRecognizer) {
let cell = gestureRecognizer.view as! ExpandableHeaderView
}
func customInit(title: String, section: Int, delegate: ExpandableHeaderViewDelegate) {
self.textLabel?.text = title
self.section = section
self.delegate = delegate
}
override func layoutSubviews() {
super.layoutSubviews()
self.textLabel?.font = UIFont(name: "Nunito-Light", size: 12)
self.textLabel?.textColor = UIColor(red: 64.0/255, green: 75.0/255, blue: 105.0/255, alpha: 1.0)
self.contentView.backgroundColor = UIColor.white
}
}
Dropdown table view cell:
class dropDownCell: UITableViewCell {
#IBOutlet weak var dropDownLbl: UILabel!
#IBOutlet weak var dropDwnBtn: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Here is the screen shots when selection done in cell and after reopen filter selections are removed or options selected are changed, sections are not closed already expanded sections. Excepted result:
There is no built-in support for allowing differing numbers of cells to be selected in different sections of a table view.
However, the UITableViewDelegate protocol includes the function tableView(_:willSelectRowAt:).
If you read the docs on that function, it says:
Return Value
An index-path object that confirms or alters the selected
row. Return an NSIndexPath object other than indexPath if you want
another cell to be selected. Return nil if you don't want the row
selected.
So you should be able to set your view controller up as the delegate of the table view, set the allowsMultipleSelection flag to true, and implement logic in the tableView(_:willSelectRowAt:) function that provides the selection rules you want.
Take a stab at writing such a function and if you have trouble, post your code, tell us how it fails to meet your needs, and we'll try to help you fix it.
I want to make a tableview with a button in the section. I want the button to add one more row to a tableview like this
Here is the source code:
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection sectionInd: Int) -> Int {
if sectionInd == 0 {
return others.count
} else {
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "ShareCell", for: indexPath as IndexPath) as! SelectOthersTableViewCell
cell.firstName.text = others[indexPath.row].firstname
cell.lastName.text = others[indexPath.row].lastname
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "addCell", for: indexPath as IndexPath) as! addTableCell
cell.addCells.tag = indexPath.row
cell.addCells.addTarget(self, action: #selector(OthersViewController.addButtonClicked(sender:)), for: UIControlEvents.touchUpInside)
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var height:CGFloat = CGFloat()
if indexPath.section == 0 {
height = 145
} else {
height = 50
}
return height
}
#objc func addButtonClicked(sender:UIButton) {
data.append("Guest 1")
let buttonPosition = sender.convert(CGPoint.zero, to: self.tableView)
let indexPath = self.tableView.indexPathForRow(at: buttonPosition)
print("indexPath \(indexPath!)")
selectedIndexes[indexPath!] = !(selectedIndexes[indexPath!] ?? false)
tableView.reloadRows(at: [indexPath!], with: .automatic)
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: data.count-1, section: 0)], with: .automatic)
tableView.endUpdates()
}
i need help please. How to add new row by tap button on icon (+)?
On click of "Add" button, You should not reload the the entire table view because it increases the processing time. Instead of that you can use of
beginUpdates and endUpdates for inserting new cell when button clicked.
Basic Approaches:
(1). On click of "Add", update your data-source for table-view.
dataSource.append(NewRecord)
(2). Insert the new cell:
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: dataSource.count-1, section: 0)], with: .automatic)
tableView.endUpdates()
Reviewing your Code:
func addButtonClicked(sender:UIButton) {
data.append("Guest 1")
.....
}
Your datasource is others on which the tableview is created and configured.
But on click of add button (addButtonClicked function), you are not updating the others data-source. Please verify it, except that your code seems good.
fun onPlusButtonClicked(){
sections.append(whatever you want)
items[2].append(["1", "2", "3", "4"]) // whatever you want to add here
tableview.reloadData() // you can call this on a background thread as well, if its not working
}
// Ex of how to use with tableview
var sections = Your array
var items = your array
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items[section].count
}
I am trying to reload my TableView but I am getting this exception "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 3 from section 1 which only contains 0 rows before the update'".
Below is my code :-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if statusTableView == tableView {
return ModelAssessStatus.sharedInstance.arrType.count
}
else {
if !sections[section].expanded {
return 0
}
else {
return sections[section].names.count
}
//return sections[section].names.count
/*if sections[section].expanded == true {
return sections[section].names.count
}
else {
return 0
}*/
}
}.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var expandableCategoryCell = tableView.dequeueReusableCell(withIdentifier: "ExpandableCategoryCell") as? ExpandableCategoryCell
if expandableCategoryCell == nil {
var nib:Array = Bundle.main.loadNibNamed("ExpandableCategoryCell", owner: self, options: nil)!
expandableCategoryCell = nib[0] as? ExpandableCategoryCell
}
expandableCategoryCell?.selectionStyle = .none
expandableCategoryCell?.labelName.text = sections[indexPath.section].names[indexPath.row]
return expandableCategoryCell!
//return UITableViewCell()
}.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if tableView == self.expandableTableView {
return 60
}
return 0
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if tableView == self.expandableTableView {
return 0
}
return 0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if tableView == expandableTableView {
let cell = self.expandableTableView.dequeueReusableHeaderFooterView(withIdentifier: "ExpandableHeaderView")
let header = cell as! ExpandableHeaderView
header.customInit(title: sections[section].categoryType, section: section, delegate: self, isSelected: sections[section].fullSelected)
if sections[section].expanded {
header.imgArrow?.image = UIImage(named : "down_Arow")//.transform = (header.imgArrow?.transform.rotated(by: CGFloat(Double.pi)))!
}
else {
header.imgArrow?.image = UIImage(named : "error")
}
return cell
}
else {
return UIView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
}
}
//Height For Raw at indexPath
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if statusTableView == tableView {
return 50
}
else {
if (sections[indexPath.section].expanded) {
return 60
}
else {
return 0
}
}
}
}
extension PeerCategoryStatusVC : expandableHeaderViewDelegate {
func toggleSections(header: ExpandableHeaderView, section: Int) {
sections[section].expanded = !sections[section].expanded
//expandableTableView.reloadData()
expandableTableView.beginUpdates()
for index in 0..<sections[section].names.count {
expandableTableView.reloadRows(at: [IndexPath(row : index, section : section)], with: .automatic)
}
expandableTableView.endUpdates()
//self.view.layoutIfNeeded()
}
}.
Here Begin Update and End Update TableView.
I am trying to reload tableView but don't know why I am getting exception.
If I will keep same number of rows then constraints issue is coming.
Is there anything which I have to add ?
I just fix the same exception. It's mainly because you call reloadRows(at: with:) with indexPath(s) whose row is larger than the total row count.