UITableViewCell Button Vanishes If Screen Is Relatively Small - ios

I'm creating a simple tabbed(3 tab bar items) app using Swift 5 and Xcode 11. Inside the iOS app, which originally had 2 tab bar items, I was thinking of making some themes for my app, and so I did, adding one more tab bar item to my app. I added the themes for the users to select like this, using UITableViewCells and programmatically made buttons:
That's what looks like. At least for big screen sized iPhones. Starting at iPhone 6, everything works fine, like pictured above. But below iPhone 6, like this iPhone 5, the left set of buttons disappear:
It's weird, and I have no idea why the right set of buttons stayed.
This is my UITableViewController:
import UIKit
#objcMembers class CustomViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
self.tableView.rowHeight = 138
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection
section: Int) -> String? {
return "Themes"
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return SingletonViewController.themes.count
}
// 3
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "themeCell", for: indexPath) as! ThemeCell
var cellyi = UIButton(frame: CGRect(x: 5, y: 5, width: 50, height: 30))
cellyi.tag = indexPath.row + 1
/////////
if cellyi.tag == 1 {
let cellButton1 = UIButton(frame: CGRect(x: 0, y: 5, width: 88, height: 119.5))
cellButton1.translatesAutoresizingMaskIntoConstraints = false
cell.addSubview(cellButton1)
cell.accessoryView = cellButton1
cellButton1.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: 10).isActive = true
cellButton1.topAnchor.constraint(equalTo: cell.topAnchor, constant: 10).isActive = true
cellButton1.widthAnchor.constraint(equalToConstant: 88).isActive = true
cellButton1.heightAnchor.constraint(equalToConstant: 119.5).isActive = true
cellButton1.addTarget(self, action: #selector(CustomViewController.backBTN(sender:)), for: .touchUpInside)
cellButton1.setTitle("Classic", for: .normal)
cellButton1.setTitleColor(UIColor.black, for: .normal)
cellButton1.tag = indexPath.row + 1
}else{
let cellButton = UIButton(frame: CGRect(x: 0, y: 5, width: 88, height: 119.5))
cellButton.translatesAutoresizingMaskIntoConstraints = false
cell.addSubview(cellButton)
cell.accessoryView = cellButton
cellButton.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: 10).isActive = true
cellButton.topAnchor.constraint(equalTo: cell.topAnchor, constant: 10).isActive = true
cellButton.widthAnchor.constraint(equalToConstant: 88).isActive = true
cellButton.heightAnchor.constraint(equalToConstant: 119.5).isActive = true
tag = indexPath.row - 1
cellButton.setImage(UIImage(named: SingletonViewController.themes[indexPath.row - 1]), for: UIControl.State.normal)
cellButton.addTarget(self, action: #selector(CustomViewController.backBTN(sender:)), for: .touchUpInside)
cellButton.tag = indexPath.row + 1
}
//////////
cell.addSubview(cellyi)
cell.accessoryView = cellyi
cellyi.backgroundColor = UIColor.red
cellyi.addTarget(self, action: #selector(CustomViewController.backBTN(sender:)), for: .touchUpInside)
if UserDefaults.standard.integer(forKey: "like") == 0{
UserDefaults.standard.set(1, forKey: "like")
}
if UserDefaults.standard.integer(forKey: "like") == indexPath.row + 1{
cellyi.backgroundColor = UIColor.green
if indexPath.row + 1 == 1{
self.tabBarController?.tabBar.barTintColor = UIColor(red:0.84, green:0.84, blue:0.84, alpha:1.0)
self.tabBarController?.tabBar.tintColor = UIColor(red:0.21, green:0.56, blue:0.96, alpha:1.0)
}else if indexPath.row + 1 == 2{
self.tabBarController?.tabBar.barTintColor = UIColor.black
self.tabBarController?.tabBar.tintColor = UIColor(red:0.98, green:0.64, blue:0.02, alpha:1.0)
}
}
tableView.allowsSelection = false
return cell
}
#objc func backBTN(sender: UIButton){
UserDefaults.standard.set(sender.tag, forKey: "like")
tableView.reloadData()
}
}
I have viewed some other of the similar questions to this one. Currently, I still haven't found a solution to this. Any help is appreciated

Related

Change Spacing of Cocoa Class TableViewCell in ViewController - Swift

I have searched for hours trying stackOverFlows solutions to this issue!!! All of which cause my custom UITable Cells to overlap and not space out. I just want my spacing to be automatic like 'return UITableView.automaticDimension' but say + 8 so that my custom cells aren't right next to each other. I cannot figure it out and maybe it is because I have one line of code that I shouldn't.
This is my cocoa touch class that creates my custom cell 'DiscussionTableViewCell':
class DiscussionTableViewCell: UITableViewCell {
lazy var backView: UIView = {
let view = UIView(frame: CGRect(x: 10, y: 6, width: UIScreen.main.bounds.width - 20, height: 55))
view.backgroundColor = UIColor.rgb(r: 194, g: 208, b: 242)
//view.layer.borderWidth = 3
return view
}()
lazy var discussionTitleLabel: UILabel = {
let LDiscussionTitleLabel = UILabel(frame: CGRect(x: 4, y: 6, width: backView.frame.width - 116, height: 30))
LDiscussionTitleLabel.textAlignment = .left
LDiscussionTitleLabel.font = UIFont.boldSystemFont(ofSize: 18)
return LDiscussionTitleLabel
}()
lazy var discussionDescriptionLabel: UILabel = {
let LDiscussionDescriptionLabel = UILabel(frame: CGRect(x: 4, y: 27, width: backView.frame.width - 30, height: 30))
LDiscussionDescriptionLabel.textAlignment = .left
LDiscussionDescriptionLabel.font = UIFont.systemFont(ofSize: 16)
LDiscussionDescriptionLabel.textColor = UIColor.gray
return LDiscussionDescriptionLabel
}()
override func awakeFromNib() {
super.awakeFromNib()
//layoutMargins = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
// Initialization code
}
override func layoutSubviews() {
//super.layoutSubviews()
contentView.backgroundColor = UIColor.rgb(r: 203, g: 215, b: 242)
backgroundColor = UIColor.yellow
backView.layer.cornerRadius = 5
backView.clipsToBounds = true
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
addSubview(backView)
backView.addSubview(discussionTitleLabel)
backView.addSubview(discussionDescriptionLabel)
// Configure the view for the selected state
}
}
And a section of my ViewController that applies to assigning text to the table cell, and spacing between cells:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return discussionTitles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DiscussionTableViewCell
cell.discussionTitleLabel.text = discussionTitles[indexPath.row]
cell.discussionDescriptionLabel.text = discussionDescriptions[indexPath.row]
return cell
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (singlePageVar == 100) {
return 1407//UITableView.automaticDimension
}
return 62//UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("section: \(indexPath.section)")
print("row: \(indexPath.row)")
var topicNumberRow = (indexPath.row)
mainTopicNumberRow = topicNumberRow
DiscussionBoardTopicThread()
}
//function I call in viewdidload to show table
func setupDiscussionBoard() {
discussionBoardView.separatorStyle = .none
view.addSubview(discussionBoardView)
discussionBoardView.backgroundColor = UIColor(red: 203/255, green: 215/255, blue: 242/255, alpha: 1.0)
discussionBoardView.dataSource = self
discussionBoardView.delegate = self
discussionBoardView.translatesAutoresizingMaskIntoConstraints = false
discussionBoardView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
discussionBoardView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
discussionBoardView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
discussionBoardView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
discussionBoardView.register(DiscussionTableViewCell.self, forCellReuseIdentifier: "cell")
}
}

Don't Click button inside in TableView

I have added one Button into tableview button is not clicked. The button is not clicked on the top of TableView. What should I do to make the button clickable? I need to make the CircleMenu button clickable. The button is now on top of tableView3. Do I need to add the button to the tableView?
override func viewDidLoad() {
super.viewDidLoad()
let button = CircleMenu(
frame: CGRect(x: view.frame.width/2 - 10, y: view.frame.height - 270, width: 50, height: 50),
normalIcon:"icon_menu",
selectedIcon:"icon_close",
buttonsCount: 3,
duration: 4,
distance: 85)
button.backgroundColor = UIColor.flatSkyBlue
button.delegate = self
button.layer.cornerRadius = button.frame.size.width / 2.0
view.addSubview(button)
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.tableView1 {
if !chipnumber2.text!.isEmpty {
let cell:DeviceTableViewCell2 = tableView1.dequeueReusableCell(withIdentifier: cellIdNew, for: indexPath) as! DeviceTableViewCell2
let deviceItem: Device3New = itemsNew[indexPath.row]
let tap1 = UITapGestureRecognizer(target: self, action: #selector(tittleNewTapped(_:)))
let tap2 = UITapGestureRecognizer(target: self, action: #selector(tittleNewTapped2(_:)))
return cell
}
}
if tableView == self.tableView2 {
if !chipnumber.text!.isEmpty {
let cell:DeviceTableViewCell2 = tableView2.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! DeviceTableViewCell2
let deviceItem: Device3 = items[indexPath.row]
cell.backgroundColor = GradientColor(UIGradientStyle.leftToRight, frame: self.view.frame, colors: [UIColor.flatPowderBlueDark, UIColor.flatSand])
return cell
}
}
if tableView == self.tableView3 {
if !chipnumber3.text!.isEmpty {
let cell:DeviceTableViewCell2 = tableView3.dequeueReusableCell(withIdentifier: cellIdNew2, for: indexPath) as! DeviceTableViewCell2
cell.backgroundColor = GradientColor(UIGradientStyle.leftToRight, frame: self.view.frame, colors: [UIColor.flatPowderBlueDark, UIColor.flatSand])
return cell
}
}
return UITableViewCell()
}
}
Just add a target to the button:
button.addTarget(self, action: #selector(buttonClicked(_:)), for: .touchUpInside)
And then a selector method:
#objc func buttonClicked(_ sender: UIButton) {
print("clicked!")
}
(Edit: Possible duplicate?)

"self.pushViewController" does not work in Swift4

I would like to make a transition by placing a UITableView within the UINavigationController class and tapping the cell in it.
However, no matter how many times I press cell, it does not make a transition.
The code is the last one, "self.pushViewController (editViewController, animated: true)".
I am assuming navigationPush, but I would like you to tell me why it does not work.
I will ask for answers.
import UIKit
import APIKit
class BookListViewController: UINavigationController {
struct BookInfo {
let bookImage: UIImage?
let bookTitle: UILabel?
let bookPrice: UILabel?
let bookID: UILabel?
}
private lazy var navBar: UINavigationBar = {
let navBar = UINavigationBar()
navBar.frame = CGRect(x: 0, y: view.safeAreaInsets.top, width: view.frame.width, height: 44)
navBar.barTintColor = UIColor.white
let navItem = UINavigationItem()
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(bookAdd))
navItem.rightBarButtonItem = addButton
navItem.title = "書籍一覧"
navBar.pushItem(navItem, animated: false)
return navBar
}()
private lazy var tableView: UITableView = {
let tableView = UITableView()
tableView.frame = CGRect(x: 0, y: navigationBar.frame.size.height + view.safeAreaInsets.top, width: view.frame.size.width, height: view.frame.size.height - view.safeAreaInsets.bottom)
tableView.rowHeight = 100
tableView.delegate = self
tableView.dataSource = self
tableView.register(BookListCell.self, forCellReuseIdentifier: NSStringFromClass(BookListCell.self))
return tableView
}()
private lazy var safeView: UIView = {
let safeView = UIView()
safeView.backgroundColor = UIColor.white
safeView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.safeAreaInsets.top)
return safeView
}()
private lazy var loadButton: UIButton = {
let button = UIButton()
button.setTitle("読込", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 13)
button.backgroundColor = UIColor.blue
button.layer.cornerRadius = 25
button.addTarget(self, action: #selector(moreLoad(sender:)), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
setupUI()
}
}
extension BookListViewController {
private func setupUI() {
view.addSubview(navBar)
view.addSubview(safeView)
view.addSubview(tableView)
view.addSubview(loadButton)
loadButton.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20.0).isActive = true
loadButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -100.0).isActive = true
loadButton.widthAnchor.constraint(equalToConstant: 50.0).isActive = true
loadButton.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
}
#objc func bookAdd() {
let bookAddViewController = BookAddViewController()
self.present(bookAddViewController, animated: true)
}
#objc func moreLoad(sender: UIButton) {
print("moreLoad!!")
}
}
extension BookListViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell: BookListCell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(BookListCell.self), for: indexPath) as? BookListCell {
cell.bookimage.image = UIImage(named: "hoge")
cell.title.text = "書籍一覧"
cell.price.text = "200.000円"
cell.bookDay.text = "2018/12/31"
cell.editButton.setTitle(">", for: .normal)
return cell
}
let cell = UITableViewCell()
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
//個別の情報を載せて遷移
let editViewController = R.storyboard.main.edit()!
self.pushViewController(editViewController, animated: true)
}
}
In storyboard you need to add navigation controller which make this controller as rootviewcontroller. Just by dragging from navigation controller to your tableview controller.
You should extend BookListViewController from UIViewController and present this controller from another controller like this self.present(UINavigationController(rootViewController: BookListViewController()), animated: true) then you can access navigationItem easily with self.navigationItem inside BookListViewController.
self show current context when we use self.navigationController then we are actually telling push to perform that push on that current view's navigation controller
use:
self.navigationController.pushViewController(editViewController, animated: true)

Invalid update: invalid number of rows in section 0 when trying to expand and collapse UITableView sections

While debugging I found that in my numberOfRowInSection it is always returning the count and skipping over the the if !DealsDataService.instance.getKellysDeals()[section].isExpanded condition which is causing the error below since it wont return 0.
Would there be any reason why that condition would be getting skipped over?
Error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 2 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
ViewController that is handling tableview and expanding and collapsing sections:
#IBOutlet weak var daysTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
daysTable.dataSource = self
daysTable.delegate = self
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let view = UIView()
view.backgroundColor = .black
let headerLabel = UILabel()
let button = UIButton(type: .system)
let headerList = DaysService.instance.getHeader()
headerLabel.textColor = .yellow
headerLabel.font = UIFont.boldSystemFont(ofSize: 20)
headerLabel.backgroundColor = .black
headerLabel.text = headerList[section]
button.setTitle("Close", for: .normal)
button.setTitleColor(.yellow, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
button.addTarget(self, action: #selector(handleOpenClose), for: .touchUpInside)
button.tag = section
headerLabel.frame = CGRect(x: 5, y: 7.5, width: 150, height: 25)
button.frame = CGRect(x: 150, y: 7.5, width: 150, height: 25)
view.addSubview(headerLabel)
view.addSubview(button)
return view
}
#objc func handleOpenClose(button: UIButton)
{
print("trying to expand....")
let section = button.tag
var dealsArray = DealsDataService.instance.getKellysDeals()
var indexPaths = [IndexPath]()
for deals in dealsArray[section].deals.indices
{
print(0, deals)
let indexPath = IndexPath(row: deals, section: section)
indexPaths.append(indexPath)
}
let isExpanded = dealsArray[section].isExpanded
dealsArray[section].isExpanded = !isExpanded
button.setTitle(isExpanded ? "Open" : "Close", for: .normal)
//still having problem expanding and closing
if isExpanded
{
dealsArray[section].deals.removeAll()
daysTable.deleteRows(at: indexPaths, with: .fade)
}
else
{
self.daysTable.insertRows(at: indexPaths, with: .fade)
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if !DealsDataService.instance.getKellysDeals()[section].isExpanded
{
return 0
}
return DealsDataService.instance.getKellysDeals()[section].deals.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return DealsDataService.instance.getKellysDeals().count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DaysCell", for: indexPath)
let deals = DealsDataService.instance.getKellysDeals()[indexPath.section].deals[indexPath.row]
cell.textLabel?.text = deals
return cell
}
Data service that returns the array of deals:
static let instance = DealsDataService()
private(set) public var barNames = [Bars]()
public var kellysDeals = [
ExpandableDeals(isExpanded: true, deals:["$2 Bud Lights", "$1 Tacos"]),//Monday
ExpandableDeals(isExpanded: true, deals:["$2 Budweiser","$2 Burgers"]),//Tuesday
ExpandableDeals(isExpanded: true, deals:["$3 Drafts","50 cent wings"]),//Wednesday
ExpandableDeals(isExpanded: true, deals:["$8 Coors pitchers","$5 Sandwiches"]),//Thursday
ExpandableDeals(isExpanded: true, deals:["No Deal"]),//Friday
ExpandableDeals(isExpanded: true, deals:["No Deal"]),//Saturday
ExpandableDeals(isExpanded: true, deals:["No Deal"])//Sunday
]
func getKellysDeals() -> [ExpandableDeals]
{
return kellysDeals
}
}
In handleOpenClose I am calling the DataService when I should be declaring a global array variable then using that in my handleOpenClose function.
var barDeals: [ExpandableDeals] = []
#IBOutlet weak var daysTable: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
daysTable.dataSource = self
daysTable.delegate = self
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let view = UIView()
view.backgroundColor = .black
let headerLabel = UILabel()
let button = UIButton(type: .system)
let headerList = DaysService.instance.getHeader()
headerLabel.textColor = .white
headerLabel.font = UIFont.boldSystemFont(ofSize: 20)
headerLabel.text = headerList[section]
button.setTitle("Open", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
button.addTarget(self, action: #selector(handleOpenClose), for: .touchUpInside)
button.tag = section
headerLabel.frame = CGRect(x: 5, y: 7.5, width: 150, height: 25)
button.frame = CGRect(x: 150, y: 7.5, width: 150, height: 25)
view.addSubview(headerLabel)
view.addSubview(button)
return view
}
#objc func handleOpenClose(button: UIButton)
{
let section = button.tag
self.daysTable.beginUpdates()
var indexPaths = [IndexPath]()
for deals in self.barDeals[section].deals.indices
{
let indexPath = IndexPath(row: deals, section: section)
indexPaths.append(indexPath)
}
let isExpanded = self.barDeals[section].isExpanded
self.barDeals[section].isExpanded = !isExpanded
button.setTitle(!isExpanded ? "Close" : "Open", for: .normal)
if isExpanded
{
daysTable.deleteRows(at: indexPaths, with: .fade)
}
else
{
daysTable.insertRows(at: indexPaths, with: .fade)
}
self.daysTable.endUpdates()
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.barDeals[section].isExpanded == false
{
return 0
}
return self.barDeals[section].deals.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.barDeals.count
}

how to implement another level in table view?

Here I had implemented collapsible table view in which first level can be used as sections and the child class can be given as rows count and for another child class how it to be implemented in table view and I am having a expand button if I click on it need to expand and show the third level and the image is shown below libraries can anyone help me how to implement this ?
and here is my code is shown here which I had already implemented
func numberOfSections(in tableView: UITableView) -> Int {
return detailsArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return detailsArray[section].childern.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRect(x: 275, y: 5, width: 30, height: 35))
headerView.backgroundColor = UIColor.white
headerView.tag = section
let headerString = UILabel(frame: CGRect(x: 10, y: 10, width: tableView.frame.size.width-10, height: 30)) as UILabel
headerString.text = detailsArray[section].name
headerView .addSubview(headerString)
let btn :UIButton = UIButton(frame: CGRect(x: UIScreen.main.bounds.size.width - 35, y: 5, width: 30, height: 35))
btn.backgroundColor = UIColor.white
if (detailsArray[section].childern.count != 0){
btn.setImage(plusImage , for: .normal)
}
else {
btn.setImage(UIImage(named: ""), for: .normal)
}
if detailsArray[section].expanded == true {
btn.setImage(minusImage, for: .normal)
}
else {
btn.setImage(plusImage, for: .normal)
}
btn.tag = section
btn.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside)
headerView.addSubview(btn)
let headerTapped = UITapGestureRecognizer (target: self, action:#selector(sectionHeaderTapped(recognizer:)))
headerView .addGestureRecognizer(headerTapped)
return headerView
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
activityIndicator.stopAnimating()
activityIndicator.hidesWhenStopped = true
tableView.isHidden = false
let cell = tableView.dequeueReusableCell(withIdentifier: "categoriescell", for: indexPath) as! CategoriesTableViewCell
let obj1 = self.detailsArray[indexPath.section].childern[indexPath.row]
cell.categoryLabel?.text = obj1.name
cell.expandButton.tag = indexPath.row
cell.expandButton.setImage(plusImage, for: .normal)
return cell
}
func sectionHeaderTapped(recognizer: UITapGestureRecognizer) {
let newViewcontroller = storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.navigationController?.pushViewController(newViewcontroller, animated: true)
}
func buttonTapped(sender: UIButton) {
let section = sender.tag
detailsArray[section].expanded = !detailsArray[section].expanded
tableView.beginUpdates()
for i in 0 ..< detailsArray[section].childern.count {
tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
}
if (detailsArray[section].childern.count != 0) {
if (sender.currentImage == plusImage){
sender.setImage(minusImage, for: .normal)
}
else {
sender.setImage(plusImage, for: .normal)
}
}
else {
sender.setImage(UIImage(named: ""), for: .normal)
}
tableView.endUpdates()
}

Resources