Deselect the rest of cells when first cell is select - ios

I have a tableview cells, and the titles I get from server. Titles are array of strings. The data is drawing like this
All data
List item
Coffees
Teas
Dessert
Main dishes
when I click on each of these I can easily select them and deselect, that part works perfect. Now i want that when i select List item, Coffees, teas, dessert, main dishes together, these 5 items will be deselected and all data(first cell) will be selected automatically.
I know i should write this in didselect method, only i dont know how to write logic correctly. Please help me to solve this. Thanks
this is my code example
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let isSelected = arrayOfTitles[indexPath.row].isSelected else {arrayOfTitles[indexPath.row].isSelected = true; return}
arrayOfTitles[indexPath.row].isSelected = !isSelected
filter?.values?[indexPath.row].isSelected = !isSelected
for i in 1..<arrayOfTitles.count {
if arrayOfTitles[i].isSelected == isSelected {
arrayOfTitles[0].isSelected = isSelected
arrayOfTitles[i].isSelected = !isSelected
}
}
}

You can try
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let isSelected = arrayOfTitles[indexPath.row].isSelected else {arrayOfTitles[indexPath.row].isSelected = true; return}
arrayOfTitles[indexPath.row].isSelected = !isSelected
filter?.values?[indexPath.row].isSelected = !isSelected
let allSelected = arrayOfTitles[1...].filter { $0.isSelected }
if allSelected.count == arrayOfTitles.count - 1 {
arrayOfTitles[0].isSelected = true
(1..<arrayOfTitles.count).forEach { arrayOfTitles[$0].isSelected = false }
}
}

Because a table view maintains its own array of selected rows (IndexPaths), you don't really need to maintain a separate .isSelected property on your data. If you need it (for state persistence, for example), you can always grab the selected rows when needed.
So, here's another approach...
I'm guessing that if you have some of the rows selected, and you then select the top row, you would also want to deselect the other rows:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var arrayOfTitles: [String] = [
"All data",
"List Item",
"Coffees",
"Teas",
"Dessert",
"Main dishes",
]
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
tableView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
tableView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
tableView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
])
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "c")
tableView.dataSource = self
tableView.delegate = self
tableView.allowsMultipleSelection = true
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayOfTitles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let c = tableView.dequeueReusableCell(withIdentifier: "c", for: indexPath)
c.textLabel?.text = arrayOfTitles[indexPath.row]
return c
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// ignore if nothing is selected
guard let paths = tableView.indexPathsForSelectedRows else { return }
// if we selected the top row ("All data"), deselect the others
if indexPath.row == 0 {
// deselect the other rows
paths.forEach {
if $0.row != 0 {
tableView.deselectRow(at: $0, animated: true)
}
}
} else {
// get the selected rows as a sorted array of Int
let rows: [Int] = (paths.map { $0.row }).sorted()
// Int array of rows 1 thru 5
let myFiveRows: [Int] = Array(1...5)
// if the two arrays are equal (they're both [1,2,3,4,5])
if rows == myFiveRows {
// deselect all
tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
// select the top row
tableView.selectRow(at: IndexPath(row: 0, section: 0), animated: true, scrollPosition: .none)
}
}
}
}

Related

Facing issue in selecting and deselecting tableview cell in swift

I am showing pincodes in tableview, and when i select a cell then it should select and if i tap on the same cell again then it should deselect(while tapping cell should work like switch)
but with below code
issue 1: initially i am unable to select 1st row but after selecting any other row and then able to select 1st row.. why? where am i wrong?
issue 2: only one time i can select deselect the same row with two tapping if i tap 3rd time continuously then unable to select the same row, why?.. please guide
class PincodeModel{
var name: String?
var id: Int?
var isSelected: Bool
init(name: String?, id: Int?, isSelected: Bool) {
self.name = name
self.id = id
self.isSelected = isSelected
}
}
class FilterViewController: UIViewController {
var pincodePreviousIndex: Int = -1
var pincodes = [PincodeModel]()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
for pincode in pincodeList {
self.pincodes.append(PincodeModel(name: pincode, id: 0, isSelected: false))
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SubFilterTableViewCell", for: indexPath) as! SubFilterTableViewCell
cell.title.text = self.pincodes[indexPath.row].name
if !self.pincodes.isEmpty {
if self.pincodes[indexPath.row].isSelected == true {
cell.tickImageView.image = #imageLiteral(resourceName: "iconTick")
}else {
cell.tickImageView.image = UIImage()
}
}
return cell
}
// EDITED Code according to below answer
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.pincodes[indexPath.row].isSelected = !self.pincodes[indexPath.row].isSelected
if self.pincodes[indexPath.row].isSelected == true {
self.filterData.pincode = pincodes[indexPath.row].name ?? ""
}else {
self.filterData.pincode = ""
}
if pincodePreviousIndex > 0 && pincodePreviousIndex != indexPath.row {
pincodes[pincodePreviousIndex].isSelected = false
}
pincodePreviousIndex = indexPath.row
}
this is working as i want when i select from index = 1, but if i select first row(index = 0) then the right mark remains if i select another row, why?
o/p with edited code:
For issue 1 - By using this line of code:
var pincodePreviousIndex: Int = 0
You cannot click the first row until you click another since
pincodes[pincodePreviousIndex].isSelected = false
Since you're defaulting to 0 in the beginning, that correlates to the first row.
For issue 2 - if you select row 2 (selected) and then select it again to deselect it: pincodePreviousIndex will hold the value of that row and then deselect it again with
pincodes[pincodePreviousIndex].isSelected = false
So even though you're selecting it it will deselect it.
I would do this at the top:
var pincodePreviousIndex: Int = -1
and at the bottom:
if pincodePreviousIndex > 0 && pincodePreviousIndex != indexPath.row {
pincodes[pincodePreviousIndex].isSelected = false
}
There are a couple approaches you can take to save yourself some trouble.
First, set .selectionStyle = .none on your cells, and then in your cell class, override setSelected(...). For example, I added an image view to my cell and gave it an empty-box as its image, and a checked-box as its highlighted image:
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
imgView.isHighlighted = selected ? true : false
}
Now the cell appearance will reflect its selected state which is maintained by the table view.
Next, instead of didSelectRowAt, we'll implement willSelectRowAt ... if the cell is currently selected, we'll deselect it (and update our data):
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
// is a row already selected?
if let idx = tableView.indexPathForSelectedRow {
if idx == indexPath {
// tapped row is already selected, so
// deselect it
tableView.deselectRow(at: indexPath, animated: false)
// update our data
pincodes[indexPath.row].isSelected = false
// tell table view NOT to select the row
return nil
} else {
// some other row is selected, so
// update our data
// table view will automatically deselect that row
pincodes[idx.row].isSelected = false
}
}
// tapped row should now be selected, so
// update our data
pincodes[indexPath.row].isSelected = true
// tell table view TO select the row
return indexPath
}
Here's a complete example:
class PincodeModel{
var name: String?
var id: Int?
var isSelected: Bool
init(name: String?, id: Int?, isSelected: Bool) {
self.name = name
self.id = id
self.isSelected = isSelected
}
}
class SelMethodTableViewController: UIViewController {
var pincodes: [PincodeModel] = []
let tableView = UITableView()
let infoView: UIView = {
let v = UILabel()
v.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
return v
}()
let infoTitle: UILabel = {
let v = UILabel()
v.text = "Info:"
return v
}()
let infoLabel: UILabel = {
let v = UILabel()
v.numberOfLines = 0
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
for i in 0..<20 {
let pcm = PincodeModel(name: "\(i)", id: i, isSelected: false)
pincodes.append(pcm)
}
[tableView, infoView, infoTitle, infoLabel].forEach { v in
v.translatesAutoresizingMaskIntoConstraints = false
}
[infoTitle, infoLabel].forEach { v in
infoView.addSubview(v)
}
[tableView, infoView].forEach { v in
view.addSubview(v)
}
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
// constrain the table view on right-side of view
tableView.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
tableView.widthAnchor.constraint(equalTo: g.widthAnchor, multiplier: 0.5),
tableView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
tableView.bottomAnchor.constraint(equalTo: infoView.topAnchor, constant: -16.0),
// let's add a tappable "info" view below the table view
infoView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
infoView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
infoView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
infoView.heightAnchor.constraint(equalToConstant: 120.0),
// add labels to infoView
infoTitle.topAnchor.constraint(equalTo: infoView.topAnchor, constant: 8.0),
infoTitle.leadingAnchor.constraint(equalTo: infoView.leadingAnchor, constant: 8.0),
infoTitle.trailingAnchor.constraint(equalTo: infoView.trailingAnchor, constant: -8.0),
infoLabel.topAnchor.constraint(equalTo: infoTitle.bottomAnchor, constant: 8.0),
infoLabel.leadingAnchor.constraint(equalTo: infoView.leadingAnchor, constant: 8.0),
infoLabel.trailingAnchor.constraint(equalTo: infoView.trailingAnchor, constant: -8.0),
//infoLabel.bottomAnchor.constraint(lessThanOrEqualTo: infoView.bottomAnchor, constant: -8.0),
])
tableView.dataSource = self
tableView.delegate = self
tableView.register(MyToggleCell.self, forCellReuseIdentifier: "toggleCell")
// just so we can see the frame of the table view
tableView.layer.borderWidth = 1.0
tableView.layer.borderColor = UIColor.red.cgColor
let t = UITapGestureRecognizer(target: self, action: #selector(showInfo(_:)))
infoView.addGestureRecognizer(t)
infoView.isUserInteractionEnabled = true
}
#objc func showInfo(_ g: UIGestureRecognizer) -> Void {
var s: String = ""
let selectedFromData = pincodes.filter( {$0.isSelected == true} )
s += "Data reports:"
if selectedFromData.count > 0 {
selectedFromData.forEach { ob in
let obID = ob.id ?? -1
s += " \(obID)"
}
} else {
s += " Nothing selected"
}
s += "\n"
s += "Table reports: "
if let selectedFromTable = tableView.indexPathsForSelectedRows {
selectedFromTable.forEach { idx in
s += " \(idx.row)"
}
} else {
s += " No rows selected"
}
infoLabel.text = s
}
}
extension SelMethodTableViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pincodes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let c = tableView.dequeueReusableCell(withIdentifier: "toggleCell", for: indexPath) as! MyToggleCell
c.label.text = pincodes[indexPath.row].name
c.selectionStyle = .none
return c
}
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
// is a row already selected?
if let idx = tableView.indexPathForSelectedRow {
if idx == indexPath {
// tapped row is already selected, so
// deselect it
tableView.deselectRow(at: indexPath, animated: false)
// update our data
pincodes[indexPath.row].isSelected = false
// tell table view NOT to select the row
return nil
} else {
// some other row is selected, so
// update our data
// table view will automatically deselect that row
pincodes[idx.row].isSelected = false
}
}
// tapped row should now be selected, so
// update our data
pincodes[indexPath.row].isSelected = true
// tell table view TO select the row
return indexPath
}
}
class MyToggleCell: UITableViewCell {
let imgView: UIImageView = {
let v = UIImageView()
return v
}()
let label: UILabel = {
let v = UILabel()
return v
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
[imgView, label].forEach { v in
v.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(v)
}
let g = contentView.layoutMarginsGuide
// give bottom anchor less-than-required
// to avoid auto-layout complaints
let b = imgView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -4.0)
b.priority = .required - 1
NSLayoutConstraint.activate([
imgView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),
imgView.topAnchor.constraint(equalTo: g.topAnchor, constant: 4.0),
imgView.widthAnchor.constraint(equalToConstant: 32.0),
imgView.heightAnchor.constraint(equalTo: imgView.widthAnchor),
b,
label.centerYAnchor.constraint(equalTo: g.centerYAnchor, constant: 0.0),
label.leadingAnchor.constraint(equalTo: imgView.trailingAnchor, constant: 16.0),
label.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 0.0),
])
if let img1 = UIImage(systemName: "square"),
let img2 = UIImage(systemName: "checkmark.square") {
imgView.image = img1
imgView.highlightedImage = img2
}
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
imgView.isHighlighted = selected ? true : false
}
}
It will look like this:
When running:
Tapping a row will select that row
Tapping a different row will select the new row and deselect the currently selected row
Tapping the already-selected row will deselect it
Tapping the gray "info view" will report on the selection states from both the data and the table view
Note that if a selected row is scrolled out-of-view, it will remain selected (and will show selected when scrolled back into view) and the data and table view selection states will continue to be correct.
Edit
If we want to use didSelectRowAt (perhaps for other uses), we can "toggle" the selected row like this:
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
// if the tapped row is already selected
if let indexPathForSelectedRow = tableView.indexPathForSelectedRow,
indexPathForSelectedRow == indexPath {
tableView.deselectRow(at: indexPath, animated: false)
// calling .deselectRow(at: ...) does NOT trigger a call to didDeselectRowAt
// so update our data here
pincodes[indexPath.row].isSelected = false
return nil
}
return indexPath
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("did select", indexPath)
pincodes[indexPath.row].isSelected = true
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
print("did deselect", indexPath)
pincodes[indexPath.row].isSelected = false
}

Saving TableView cells data (containing segment controller) using button

I'm tired of searching about what I want, so I will ask here and hope you guys help if possible.
I have a tableview contain segments in each cell and I want to save all cells segment using button [outside the tableview] so I can show them in another table later.
Here is my tableview
here is my view Controller:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
let arr = ["item 1",
"item 2",
"item 3"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(CustomCell.self, forCellReuseIdentifier: "cell")
}
#IBAction func saveButtonAction(_ sender: UIButton) {
// I want to save the cells segmented control selectedSegmentIndex???
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
90
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.textView.text = arr[indexPath.row]
return cell
}
}
and here is my Custom Cell and it contain TextView and Segment Controller I tried to save the segments changes in array but I don't know what to do after lol:
import UIKit
class CustomCell: UITableViewCell {
let textView: UITextView = {
let tv = UITextView()
tv.translatesAutoresizingMaskIntoConstraints = false
tv.font = UIFont.systemFont(ofSize: 16)
tv.isEditable = false
return tv
}()
// var rowIndexPath: Int?
var segmentArray: [Int] = []
lazy var itemSegmentedControl: UISegmentedControl = {
let sc = UISegmentedControl(items: ["1", "2", "3"])
sc.translatesAutoresizingMaskIntoConstraints = false
sc.tintColor = UIColor.white
sc.selectedSegmentIndex = 0
sc.addTarget(self, action: #selector(handlePointChange), for: .valueChanged)
return sc
}()
let stackView: UIStackView = {
let sv = UIStackView()
sv.translatesAutoresizingMaskIntoConstraints = false
return sv
}()
#objc func handlePointChange() {
let segChange = itemSegmentedControl.titleForSegment(at: itemSegmentedControl.selectedSegmentIndex)!
segmentArray.append(Int(segChange)!)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
stackView.addSubview(textView)
stackView.addSubview(itemSegmentedControl)
addSubview(stackView)
stackView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
stackView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
stackView.widthAnchor.constraint(equalToConstant: 400).isActive = true
stackView.heightAnchor.constraint(equalToConstant: 85).isActive = true
itemSegmentedControl.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
itemSegmentedControl.topAnchor.constraint(equalTo: textView.bottomAnchor, constant: 5).isActive = true
itemSegmentedControl.widthAnchor.constraint(equalTo: textView.widthAnchor).isActive = true
itemSegmentedControl.heightAnchor.constraint(equalToConstant: 40).isActive = true
textView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -10).isActive = true
textView.topAnchor.constraint(equalTo: stackView.topAnchor).isActive = true
textView.widthAnchor.constraint(equalTo: stackView.widthAnchor).isActive = true
textView.heightAnchor.constraint(equalToConstant: 40).isActive = true
}
}
Regards and thanks.
First: in the ViewController I added the empty array:
// I moved the segment array from the custom cell class to here
var segmentArray: [Int:Int] = [:]
override func viewDidLoad() {
super.viewDidLoad()
// I made this array to store default state to the items segments
segmentArray = [0:0,1:0,2:0]
}
// with this func I can update the segmentArray
func getSegmentNumber(r: Int, s: Int) {
segmentArray[r] = Int(s)
}
#IBAction func saveButtonAction(_ sender: UIButton) {
// Here I can save the new state for the segment to the firebase or anywhere.
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.textView.text = arr[indexPath.row]
// here to store the index path row
cell.rowIndexPath = indexPath.row
// you need to add the link because it won't work
cell.link = self
return cell
}
Second: in the Custom Cell Class:
// I removed the store array
var segmentArray: [Int] = [] // removed
// I made link to the ViewController
var link: ViewController?
// variable to get the index path row
var rowIndexPath: Int?
// here I can update the state of the segment using the link
#objc func handlePointChange() {
let segChange = itemSegmentedControl.selectedSegmentIndex
link?.getSegmentNumber(r: rowIndexPath!, s: segChange)
}
That worked for me, and I hope you got it.
If you need anything you can ask.
Again thanks to Kudos.
Regards to all

Setting constraints to UITableView Footer Swft

I'm trying to place a footer with a UIButton, now I placed it only with frame and when I try it out across apple's iphones the button is not placed well due to the fact that I didn't set any auto-layout, the thing is Im trying to set auto-layout to the footer and it fails all the time also I'm not sure if its possible doing that, would love to get a handy help with that or even hints :).
Here's my code of the tableView:
import UIKit
import PanModal
protocol FilterTableViewDelegate {
func didUpdateSelectedDate(_ date: Date)
}
class FilterTableViewController: UITableViewController, PanModalPresentable {
var panScrollable: UIScrollView? {
return tableView
}
var albumsPickerIndexPath: IndexPath? // indexPath of the currently shown albums picker in tableview.
var delegate: FilterTableViewDelegate?
var datesCell = DatesCell()
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
// registerTableViewCells()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// tableView.frame = view.bounds
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
// MARK: - View Configurations
func setupTableView() {
tableView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
tableView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
tableView.separatorStyle = .singleLine
tableView.isScrollEnabled = false
tableView.allowsSelection = true
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 600
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
}
func indexPathToInsertDatePicker(indexPath: IndexPath) -> IndexPath {
if let albumsPickerIndexPath = albumsPickerIndexPath, albumsPickerIndexPath.row < indexPath.row {
return indexPath
} else {
return IndexPath(row: indexPath.row + 1, section: indexPath.section)
}
}
// MARK: - UITableViewDataSource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// If datePicker is already present, we add one extra cell for that
if albumsPickerIndexPath != nil {
return 5 + 1
} else {
return 5
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let byActivityCell = UINib(nibName: "byActivityCell",bundle: nil)
self.tableView.register(byActivityCell,forCellReuseIdentifier: "byActivityCell")
let activityCell = tableView.dequeueReusableCell(withIdentifier: "byActivityCell", for: indexPath) as! byActivityCell
activityCell.selectionStyle = .none
return activityCell
case 1:
let byTypeCell = UINib(nibName: "ByType",bundle: nil)
self.tableView.register(byTypeCell,forCellReuseIdentifier: "byTypeCell")
let typeCell = tableView.dequeueReusableCell(withIdentifier: "byTypeCell", for: indexPath) as! ByType
typeCell.selectionStyle = .none
return typeCell
case 2:
let byHashtagsCell = UINib(nibName: "ByHashtags",bundle: nil)
self.tableView.register(byHashtagsCell,forCellReuseIdentifier: "byHashtagsCell")
let hashtagsCell = tableView.dequeueReusableCell(withIdentifier: "byHashtagsCell", for: indexPath) as! ByHashtags
hashtagsCell.selectionStyle = .none
return hashtagsCell
case 3:
let byDatesCell = UINib(nibName: "DatesCell",bundle: nil)
self.tableView.register(byDatesCell,forCellReuseIdentifier: "byDatesCell")
let datesCell = tableView.dequeueReusableCell(withIdentifier: "byDatesCell", for: indexPath) as! DatesCell
datesCell.selectionStyle = .none
datesCell.datesTableViewCellDelegate = self
return datesCell
case 4:
let byAlbumCell = UINib(nibName: "AlbumCell",bundle: nil)
self.tableView.register(byAlbumCell,forCellReuseIdentifier: "byAlbumCell")
let albumCell = tableView.dequeueReusableCell(withIdentifier: "byAlbumCell", for: indexPath) as! AlbumCell
albumCell.configureCell(choosenAlbum: "Any")
albumCell.selectionStyle = .none
return albumCell
case 5:
let albumPickerCell = UINib(nibName: "AlbumsPickerTableViewCell", bundle: nil)
self.tableView.register(albumPickerCell, forCellReuseIdentifier: "albumPickerCell")
let albumsPicker = tableView.dequeueReusableCell(withIdentifier: "albumPickerCell", for: indexPath) as! AlbumsPickerTableViewCell
return albumsPicker
default:
return UITableViewCell()
}
}
// MARK: - footer Methods:
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return getfooterView()
}
func getfooterView() -> UIView
{
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 400))
let applyFiltersBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 380, height: 40))
applyFiltersBtn.center = footerView.center
applyFiltersBtn.layer.cornerRadius = 12
applyFiltersBtn.layer.masksToBounds = true
applyFiltersBtn.setTitle("Apply Filters", for: .normal)
applyFiltersBtn.backgroundColor = #colorLiteral(red: 0.1957295239, green: 0.6059523225, blue: 0.960457623, alpha: 1)
// doneButton.addTarget(self, action: #selector(hello(sender:)), for: .touchUpInside)
footerView.addSubview(applyFiltersBtn)
return footerView
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 10
}
// MARK: TableViewDelegate Methods:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
tableView.beginUpdates()
// 1 - We Delete the UIPicker when the user "Deselect" thr row.
if let datePickerIndexPath = albumsPickerIndexPath, datePickerIndexPath.row - 1 == indexPath.row {
tableView.deleteRows(at: [datePickerIndexPath], with: .fade)
self.albumsPickerIndexPath = nil
} else {
// 2
// if let datePickerIndexPath = albumsPickerIndexPath {
// tableView.deleteRows(at: [datePickerIndexPath], with: .fade)
// }
albumsPickerIndexPath = indexPathToInsertDatePicker(indexPath: indexPath)
tableView.insertRows(at: [albumsPickerIndexPath!], with: .fade)
tableView.deselectRow(at: indexPath, animated: true)
}
tableView.endUpdates()
}
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if indexPath.row == 4 {
return indexPath
} else {
return nil
}
}
}
extension FilterTableViewController: DatesTableViewCellDelegate {
func didButtonFromPressed() {
print("Button From is Pressed")
let pickerController = CalendarPickerViewController(
baseDate: Date(),
selectedDateChanged: { [weak self] date in
guard let self = self else { return }
// self.item.date = date
//TODO: Pass the date to the DatesCell to update the UIButtons.
self.delegate?.didUpdateSelectedDate(date)
self.tableView.reloadRows(at: [IndexPath(row: 3, section: 0)], with: .fade)
})
present(pickerController, animated: true, completion: nil)
}
func didButtonToPressed() {
//TODO: Present our custom calendar
let calendarController = CalendarPickerViewController(
baseDate: Date(),
selectedDateChanged: { [weak self] date in
guard let self = self else { return }
self.tableView.reloadRows(at: [IndexPath(row: 3, section: 0)], with: .fade)
})
self.present(calendarController, animated: true, completion: nil)
}
}
First, a tip (based on this and other questions you've posted): Simplify what you're doing. When you post code referencing 5 different cell classes along with code for handling cell action delegates, inserting and deleting, etc... but your question is about Section Footer layout, it gets difficult to help.
So, here's an example of a simple table view controller, using a default cell class, and a custom UITableViewHeaderFooterView for the section footer:
class MySectionFooterView: UITableViewHeaderFooterView {
let applyFiltersBtn = UIButton()
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
configureContents()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
configureContents()
}
func configureContents() {
applyFiltersBtn.translatesAutoresizingMaskIntoConstraints = false
applyFiltersBtn.layer.cornerRadius = 12
applyFiltersBtn.layer.masksToBounds = true
applyFiltersBtn.setTitle("Apply Filters", for: .normal)
applyFiltersBtn.backgroundColor = #colorLiteral(red: 0.1957295239, green: 0.6059523225, blue: 0.960457623, alpha: 1)
contentView.addSubview(applyFiltersBtn)
let g = contentView.layoutMarginsGuide
NSLayoutConstraint.activate([
// use layoutMarginsGuide for top and bottom
applyFiltersBtn.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
applyFiltersBtn.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
// inset button 20-pts from layoutMarginsGuide on each side
applyFiltersBtn.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
applyFiltersBtn.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
// make button height 40-pts
applyFiltersBtn.heightAnchor.constraint(equalToConstant: 40.0),
])
}
}
class SectionFooterViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// register cell class for reuse
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
// register Section Footer class for reuse
tableView.register(MySectionFooterView.self, forHeaderFooterViewReuseIdentifier: "mySectionFooter")
tableView.sectionFooterHeight = UITableView.automaticDimension
tableView.estimatedSectionFooterHeight = 50
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 8
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "\(indexPath)"
return cell
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = tableView.dequeueReusableHeaderFooterView(withIdentifier:"mySectionFooter") as! MySectionFooterView
return view
}
}
To handle tapping the button in the section footer, you can either assign it an action in viewForFooterInSection, or build that action into the MySectionFooterView class and use a closure or protocol / delegate pattern.

DidSelectRow is not working

Pretty much what the title says: DidSelectRow is not getting called, and I did check that I didn't use Deselect. I also checked that delegate and DataSource are connected to the tableViewController and the Selection was Single Selection. However, the method is still not getting called.
Does an empty tableView might have any effect to it(reason it's empty is because the user starts to populate it. I'm using CoreData for it)?
EDIT
This is what the method looks like:
var effectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
var effect: UIVisualEffect!
let defaults = UserDefaults.standard
var userMagazineTitle = [User]()
var dataString = String()
override func viewDidLoad() {
super.viewDidLoad()
addProgrammatically()
let fetchRequest: NSFetchRequest<User> = User.fetchRequest()
do{
let users = try PresistanceService.context.fetch(fetchRequest)
self.userMagazineTitle = users
self.tableView.reloadData()
}catch{
ProgressHUD.showError("Nothing to see here")
}
}
/ MARK: - Table view data source
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 {
// #warning Incomplete implementation, return the number of rows
return userMagazineTitle.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyFeedTableViewCell
cell.myHeadline?.text = userMagazineTitle[indexPath.row].title
cell.indentationLevel = 3
return cell
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let manage = PresistanceService.persistentContainer.viewContext
let del = userMagazineTitle[indexPath.row]
if editingStyle == .delete {
// Delete the row from the data source
manage.delete(del)
do{
try manage.save()
}catch{
ProgressHUD.showError()
}
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
do{
userMagazineTitle = try manage.fetch(fetchRequest) as! [User]
}catch{
ProgressHUD.showError()
}
tableView.reloadData()
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "showUser", sender: indexPath)
navigationController?.navigationBar.isHidden = false
tableView.deselectRow(at: indexPath, animated: true)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showUser" {
let DestViewController = segue.destination as! UINavigationController
let targetController = DestViewController.topViewController as! CompanyTableViewController
let indexPath = sender as! IndexPath
let user = userMagazineTitle[indexPath.row].title
let user2 = userMagazineTitle[indexPath.row].rssurl
targetController.customUserInit(articlesindex: indexPath.row, title: user!, rssUrl: user2!)
}
}
func addProgrammatically() {
effectView.frame = view.bounds
tableView.addSubview(effectView)
effectView.translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 11.0, *) {
effectView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
} else {
// Fallback on earlier versions
effectView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
}
effectView.heightAnchor.constraint(equalTo: self.view.heightAnchor).isActive = true
effectView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
effect = effectView.effect
effectView.effect = nil
self.tableView.separatorStyle = .none
tableView.backgroundView = UIImageView(image: UIImage(named: "myMagazines.jpg"))
tableView.backgroundView?.contentMode = .scaleAspectFill
tableView.clipsToBounds = true
navigationController?.navigationBar.isHidden = false
UIApplication.shared.statusBarStyle = .lightContent
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.barStyle = .black
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
}
}
Another thing that might lead to the issue is not selected selection kind. I lost my two hours for fixing this.
Cross check cell selection , if it is selected for no selection change it to single selection.
To do this programatically :
Swift : tableView.allowsSelection = YES;
Objective C: [tableView allowsSelection : YES];
==============================
This might be helpful to someone!
You have added a UIVisualEffectView view on top of tableview, which is not passing the touch events to tableview.
Setting effectView.isUserInteractionEnabled = false, should make this view pass touch events to the views underneath.
There could be two issues for this problem:
The method signature of the TableView DidSelectRow could be wrong. The current signature is:
Swift:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
ObjectiveC: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
You are setting the Delegate of the TableView to nil somewhere in the code.

want to select option from dropdown and change title of button according to selected option ...How?

For dropdrawn menu ,i used UItableView and button to display dropdown list when i select option from dropdown i want to change title of button according to selected option ??
// foodbutton click
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "foodcell", for: indexPath)
// cell.textLabel?.text = array [indexPath.row]
cell.textLabel?.text = array [indexPath.row]
return cell
}
#nonobjc func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let cell = tableView.cellForRow(at: indexPath)
selectFoodbutton.setTitle(cell?.textLabel?.text, for: .normal)
self.foodtable.isHidden = true
}
#IBAction func selectFoodbutton(_ sender: Any) {
self.foodtable.isHidden = !self.foodtable.isHidden
}
In button action write the code for appear the table view like below way.
First u create a tableview below that button.
load datasource into that tableview.
then select the one of the cell.
then set the button title as selected one.
then finally hide the table view.
code snippet like this
#IBAction func educationBtnAction(_ sender: AnyObject) {
// let point = textfield.getBottomTableViewPoint()
let point = sender.getPointBased(level: 1)
self.educationTableview = UITableView(frame: CGRect(x: point.x, y: point.y, width: sender.frame.size.width + 10, height: 150), style: .plain)
self.educationTableview.dataSource = self
self.educationTableview.delegate = self
self.educationTableview.backgroundColor = UIColor.clear
self.educationTableview.applyPropertiesToPopupTableView()
// textfield.superview?.addSubview(labPanelTableView)
bgScrollView.addSubview(educationTableview)
if self.educationTableview.alpha == 0.0{
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseOut, animations: {
self.hidePopups()
self.educationTableview.alpha = 1.0
}, completion: { finished in
})
}
commonTypeArray = ["1", "2", "3", "4", "5",]
self.educationTableview.reloadData()
}
func hidePopups() {
self.educationTableview.alpha = 0.0
}
For remaining process your code above in question is enough

Resources