How to expand cell when button inside cell is pressed? - ios

I'm newer in xcode and swift and i found a guide for expand cell when the entire cell is pressed.
Now i wanna expand the cell when the button inside the cell is pressed.
I have this in my view controller :
//datasource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView.tag == 100 {
return nameArr.count
}else{
return prova.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.tag == 100 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
cell.lblName.text = nameArr[indexPath.row]
cell.expand.tag = indexPath.row
return cell
}else{
tableView.estimatedRowHeight = 60
tableView.rowHeight = UITableView.automaticDimension
let cell = tableView.dequeueReusableCell(withIdentifier: "InsideTableViewCell") as! InsideTableViewCell
cell.lblInsideName.text = prova[indexPath.row]
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//se la cella è selezionata e deve essere tirata ancora giù ritorna 243 che sarebbe il valore della cella + l'immagine dentro da mostrare
if selectedIndex == indexPath.row && isCollapsed == true && tableView.tag == 100 {
return 375
}else {
//altrimenti se è gia collassata e ripremiamo sulla cella ritorna 50 e quindi richiude la cella
return 96
}
}
//delegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if selectedIndex == indexPath.row {
if self.isCollapsed == false {
self.isCollapsed = true
}else{
self.isCollapsed = false
}
}else{
self.isCollapsed = true
}
self.selectedIndex = indexPath.row
//tableView.reloadRows(at: [indexPath], with: .automatic)
tableView.beginUpdates()
tableView.endUpdates()
}
But i don't know ho to do that.
In my viewController now i have this variable :
var selectedIndex = -1 //tell me which cell i pressed
var isCollapsed = false // tell if the cell is already collapsed

You can define a clouser in cell and call it when you press the button in your cell
in your cell define:
var buttonClicked: (() -> Void)?
in your cell button call this clouser
func buttonPressAction() {
buttonClicked?()
}
in your cellForRow method change it like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.tag == 100 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
cell.buttonClicked = { [weak self] in
if self.isCollapsed == false {
self.isCollapsed = true
} else{
self.isCollapsed = false
}
}else{
self.isCollapsed = true
}
}
}

Modify the data source to change it
And of course the data source can be more complex it can be an object that contains whether or not to open the property
let nameArr:[Bool] = [false, false, false]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.nameArr[indexPath.row] = !self.nameArr[indexPath.row]
tableView.reloadRows(at: [indexPath], with: .none)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let isshow = self.nameArr[indexPath.row]
if isshow {
return 375
} else {
return 69
}
}

You should update the table view after changing the height:
tableView.reloadData().
You can do it by sending info about that action to the view controller using delegates.
1) You should save a state of your cells. You can store it in the cell model:
class YourCellModel {
var isExpanded = false
}
2) You should create a delegate protocol:
protocol YourCellDelegate: AnyObject {
func buttonPressed()
}
3) Add properties for the cell delegate and the cell model. Also you should add a function buttonPressed:
class YourCell: UITableViewCell {
weak var delegate: YourCellDelegate?
var model: YourCellModel?
//...
#IBAction func buttonPressed() {
model?.isExpanded = true
delegate?.buttonPressed()
}
}
4) You should store cell models in the view controller: {
class YourViewController: UIViewController {
var cellModels: [YourCellModel] = []
//...
override func viewDidLoad() {
super.viewDidLoad()
cellModels = Array(repeating: YourCellModel(), count: <count of cells, maybe it is a prova.count>)
}
}
5) Setup cell models and delegates to the cells in cellForItem:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = cellModels[indexPath.row]
if tableView.tag == 100 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
cell.lblName.text = nameArr[indexPath.row]
cell.expand.tag = indexPath.row
cell.model = model
cell.delegate = self
return cell
} else {
tableView.estimatedRowHeight = 60
tableView.rowHeight = UITableView.automaticDimension
let cell = tableView.dequeueReusableCell(withIdentifier: "InsideTableViewCell") as! InsideTableViewCell
cell.lblInsideName.text = prova[indexPath.row]
cell.model = model
cell.delegate = self
return cell
}
}
6) Update heightForItem:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let model = cellModels[indexPath.row]
if model.isExpanded {
return 375
} else {
return 96
}
}
7) Your view controller should implement YourCellDelegate:
extension YourViewController: YourCellDelegate {
func buttonPressed() {
tableView.reloadData()
}
}

Related

How to show/hide particular section in tableview with customised two cells

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
}

Tableview header cell expand with same cardView in swift?

I have attached the image click the card view expands the same card inside the table cell dynamically its passible to achieve this?
I have searched a lot but not working
Hear my code added header cell with CardView
added arrow button to click the button expand the cell
its able expand but not in parent card it was showing diff card
I have adde my source code
var hiddenSections = Set<Int>()
let tableViewData = [
["1","2","3","4","5"],
["1","2","3","4","5"],
["1","2","3","4","5"],
]
override func viewDidLoad() {
super.viewDidLoad()
let CustomeHeaderNib = UINib(nibName: "CustomSectionHeader", bundle: Bundle.main)
historyTableView.register(CustomeHeaderNib, forHeaderFooterViewReuseIdentifier: "customSectionHeader")
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.tableViewData.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.hiddenSections.contains(section) {
return 0
}
return self.tableViewData[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = self.tableViewData[indexPath.section][indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return view.frame.width/4
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = self.historyTableView.dequeueReusableHeaderFooterView(withIdentifier: "customSectionHeader") as! CustomSectionHeader
header.setupCornerRadious()
let sectionButton = header.expandBtn
sectionButton?.setTitle(String(section),
for: .normal)
sectionButton?.tag = section
sectionButton?.addTarget(self,action: #selector(self.hideSection(sender:)), for: .touchUpInside)
return header
}
#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.historyTableView.insertRows(at: indexPathsForSection(),
with: .fade)
} else {
self.hiddenSections.insert(section)
self.historyTableView.deleteRows(at: indexPathsForSection(),
with: .fade)
}
}
With out sections also you can achieve this. To do this,
1.Return cell height as section height. If user clicks on the cell then return total content height to the particular cell.
2.You need to take an array, if user selects cell, add indexPath number in to array. If selects already expand cell remove it from array. In height for row at index check indexPath is in array or not.
This is one of the way. With sections also you can do that.
//MARK:- UITableView Related Methods
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrDict.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
// var cel = tblExpandedTest.dequeueReusableCellWithIdentifier("expCell", forIndexPath: indexPath) as! CDTableViewCell
var cel : CaseHearingTabTVC! = tableView.dequeueReusableCell(withIdentifier: "caseHearingTabCell") as! CaseHearingTabTVC
if(cel == nil)
{
cel = Bundle.main.loadNibNamed("caseHearingTabCell", owner: self, options: nil)?[0] as! CaseHearingTabTVC;
}
//cell?.backgroundColor = UIColor.white
cel.delegate = self
if indexPath != selctedIndexPath{
cel.subview_desc.isHidden = true
cel.subview_remarks.isHidden = true
cel.lblHearingTime.isHidden = true
}
else {
cel.subview_desc.isHidden = false
cel.subview_remarks.isHidden = false
cel.lblHearingTime.isHidden = false
}
return cel
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectIndex = true;
if(selectedInd == indexPath.row) {
selectedInd = -1
}
else{
let currentCell = tableView.cellForRow(at: indexPath)! as! CaseHearingTabTVC
cellUpdatedHeight = Float(currentCell.lblHearingTime.frame.origin.y + currentCell.lblHearingTime.frame.size.height) + 2;
selectedInd = -1
tblCaseHearing.reloadData()
selectedInd = indexPath.row
}
let previousPth = selctedIndexPath
if indexPath == selctedIndexPath{
selctedIndexPath = nil
}else{
selctedIndexPath = indexPath
}
var indexPaths : Array<IndexPath> = []
if let previous = previousPth{
indexPaths = [previous]
}
if let current = selctedIndexPath{
indexPaths = [current]
}
if indexPaths.count>0{
tblCaseHearing.reloadRows(at: indexPaths, with: UITableView.RowAnimation.automatic)
}
}
func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowIndexPath indexPath:IndexPath) {
(cell as! CaseHearingTabTVC).watchFrameChanges()
}
func tableView(_ tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowIndexPath indexPath:IndexPath) {
(cell as! CaseHearingTabTVC).ignoreFrameChanges()
}
func tableView(_ TableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
if indexPath == selctedIndexPath{
return CGFloat(cellUpdatedHeight)
}else{
return CaseHearingTabTVC.defaultHeight
}
}
Best approach is to create two different cells for normal card and expanded card.
fileprivate var selectedIndex: Int?
func registerTableViewCells() {
tableView.register(UINib(nibName:Nib.CardCell , bundle: nil), forCellReuseIdentifier: "CardCell")
tableView.register(UINib(nibName:Nib.ExpandedCardCell , bundle: nil), forCellReuseIdentifier: "ExpandedCardCell")
}
override func viewDidLoad() {
super.viewDidLoad()
self.registerTableViewCells()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
guard let index = selectedIndex else {
return 115
}
if index == indexPath.row{
return 200
}
return 115
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let selected = selectedIndex, selected == indexPath.row{
let cell = tableView.dequeueReusableCell(withIdentifier: "ExpandedCardCell", for: indexPath) as! ExpandedCardCell
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: "CardCell", for: indexPath) as! CardCell
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if selectedIndex == indexPath.row{
selectedIndex = nil
}
else{
selectedIndex = indexPath.row
}
UIView.performWithoutAnimation {
tableView.reloadData()
}
}

UITableView Multi Selection's selected checkmark not remains checked

I have two UITableView in my application.
One is for Category and Second is for SubCategory.
On the basis of selected Category SubCategory UITableView, data will change, and SubCategory UITableView have multi-selection functionality, till this my application is working fine.
Now the problem is when I am on category UITableView and click on suppose Category cell it will redirect to the various subCategory, On that screen, I have selected multiple choices and click on back button appear on top, and when I click again on Category tab my selection(Checkmark) is disappearing.
I want my checkmark to be selected as long as I manually set them as unchecked.
How can I implement that thing?
Sample screenshot of my application attached below.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tblSubCategory.cellForRow(at: indexPath)
if cell!.isSelected
{
cell!.isSelected = false
if cell!.accessoryType == UITableViewCell.AccessoryType.none
{
if strCategoryData == "Category" {
cell!.accessoryType = UITableViewCell.AccessoryType.checkmark
let objectForCell = arrSubCategoryData[indexPath.row]
arrSelectedCetegoryIndex.append(objectForCell)
let defaults = UserDefaults.standard
defaults.set(arrSelectedCetegoryIndex, forKey: "categoryKey")
}
else if strCategoryData == "Brand" {
cell!.accessoryType = UITableViewCell.AccessoryType.checkmark
let objectForCell = arrSubCategoryData[indexPath.row]
arrSelectedBrandIndex.append(objectForCell)
}
else if strCategoryData == "Color" {
cell!.accessoryType = UITableViewCell.AccessoryType.checkmark
let objectForCell = arrSubCategoryData[indexPath.row]
arrSelectedColorIndex.append(objectForCell)
}
else if strCategoryData == "Size" {
cell!.accessoryType = UITableViewCell.AccessoryType.checkmark
let objectForCell = arrSubCategoryData[indexPath.row]
arrSelectedSizeIndex.append(objectForCell)
}
}
else
{
if strCategoryData == "Category" {
cell!.accessoryType = UITableViewCell.AccessoryType.none
let selectedIndex = (tblSubCategory.indexPathForSelectedRow?.row)!
let selectedIndexValue = arrSubCategoryData[selectedIndex]
print(selectedIndexValue)
let index = arrSelectedCetegoryIndex.firstIndex(of: selectedIndexValue)!
arrSelectedCetegoryIndex.remove(at: index)
}
else if strCategoryData == "Brand" {
cell!.accessoryType = UITableViewCell.AccessoryType.none
let selectedIndex = (tblSubCategory.indexPathForSelectedRow?.row)!
let selectedIndexValue = arrSubCategoryData[selectedIndex]
print(selectedIndexValue)
let index = arrSelectedBrandIndex.firstIndex(of: selectedIndexValue)!
arrSelectedBrandIndex.remove(at: index)
}
else if strCategoryData == "Color" {
cell!.accessoryType = UITableViewCell.AccessoryType.none
let selectedIndex = (tblSubCategory.indexPathForSelectedRow?.row)!
let selectedIndexValue = arrSubCategoryData[selectedIndex]
print(selectedIndexValue)
let index = arrSelectedColorIndex.firstIndex(of: selectedIndexValue)!
arrSelectedColorIndex.remove(at: index)
}
else if strCategoryData == "Size" {
cell!.accessoryType = UITableViewCell.AccessoryType.none
let selectedIndex = (tblSubCategory.indexPathForSelectedRow?.row)!
let selectedIndexValue = arrSubCategoryData[selectedIndex]
print(selectedIndexValue)
let index = arrSelectedSizeIndex.firstIndex(of: selectedIndexValue)!
arrSelectedSizeIndex.remove(at: index)
}
}
}
}
You are probably performing a segue to go to the sub category view controller, and every time you perform this segue, tableview delegate and datasource methods are called again and cells are initialized all over again.
For you to show your cells checked you are going to need to save the checked values in the Categories view controller and pass them to the SubCategory View Controller and set the checked values in your cellForRowAtIndexpath method.
Here is an example on how to implement that:
class CategoryViewController: UIViewController {
var checkedValues = [[Bool]]()
var indexSelected = -1
override func viewDidLoad() {
super.viewDidLoad()
// your code here
checkedValues.append(contentsOf: repeatElement([], count: yourCategArray.count))
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// your code here
indexSelected = indexPath.row
self.performSegue(withIdentifier: "yourSegueIdentifierHere", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
(segue.destination as! SubCategoryViewController).parentCategoryVC = self
}
}
Now for the other View Controller:
class SubCategoryViewController: UIViewController {
var parentCategoryVC = CategoryViewController()
override func viewDidLoad() {
super.viewDidLoad()
if parentCategoryVC.checkedValues[parentCategoryVC.indexSelected].count == 0 {
parentCategoryVC.checkedValues[parentCategoryVC.indexSelected].append(contentsOf: repeatElement(false, count: yourSubCategArray.count))
}
// your code here
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourSubCategArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell...
if parentCategoryVC.checkedValues[parentCategoryVC.indexSelected][indexPath.row] { cell.accessoryType = .checkmark } else { cell.accessoryType = .none }
// your code here
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// your code
parentCategoryVC.checkedValues[parentCategoryVC.indexSelected][indexPath.row] = !parentCategoryVC.checkedValues[parentCategoryVC.indexSelected][indexPath.row]
tableView.reloadRows(at: indexPath, with: UITableViewRowAnimation.none)
}
}
For any additional clarification feel free to ask
You need to create one Int type array and then append value on click if not in array and if already exist so you need to remove from array and set checkmark in cellForRowAt method.
Please See complete code
import UIKit
class testViewController: UIViewController {
var selectedRows: [Int] = []
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension testViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
cell.textLabel?.text = "Welcome " + (indexPath.row+1).description
cell.selectionStyle = .none
cell.accessoryType = selectedRows.contains(indexPath.row) ? .checkmark : .none
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if self.selectedRows.contains(indexPath.row) {
if let index = self.selectedRows.firstIndex(of: indexPath.row) {
self.selectedRows.remove(at: index)
}
} else {
self.selectedRows.append(indexPath.row)
}
tableView.reloadData()
}
}

Labels in UITableViewCell Shifts/ Disappears while expanding the cell

I am trying to create expand/ collapse tableView having multiple labels, textViews and images. The problem is when I expand a cell, the top most label (Black Text/ Blue background in image) disappears and then comes back when cell updates. Is there any proper solution to fix this type of problem? Is this related to reloadRows?
// ViewController Class:
private func bindTableView() {
guard let tableView = self.planServicesTableView,
let viewModel = self.viewModel else {
return
}
tableView.estimatedRowHeight = 130
tableView.rowHeight = UITableView.automaticDimension
let dataSource = RxTableViewSectionedReloadDataSource<PlanServiceSection>(configureCell:
{(dataSource: TableViewSectionedDataSource<PlanServiceSection>,
tableView: UITableView,
indexPath: IndexPath,
item: PlanServiceSection.Item) -> UITableViewCell in
let cell = tableView.dequeueReusableCell(withIdentifier: item.cellType.cellIdent, for: indexPath)
if let planServiceCell = cell as? PlanServiceDescriptionTableViewCell {
planServiceCell.setCollapsed(collapsed:(viewModel.cellIsExpanded(at: indexPath)) ? false : true)
planServiceCell.configureCell(item: item)
planServiceCell.upgradeTextView.sizeToFit()
planServiceCell.featureDisclaimerTextView.sizeToFit()
}
if let disclaimerCell = cell as? PlanDisclaimerTableViewCell {
disclaimerCell.setCollapsed(collapsed: (viewModel.cellIsExpanded(at: indexPath)) ? false : true)
disclaimerCell.configureCell(item: item)
disclaimerCell.disclaimerDescriptionTextView.sizeToFit()
}
return cell
})
viewModel.dataSource = dataSource
tableView.tableFooterView = UIView()
tableView.delegate = self
viewModel.sections.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: self.disposeBag)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let descriptionCell = tableView.cellForRow(at: indexPath) as? PlanServiceDescriptionTableViewCell {
descriptionCell.setCollapsed(collapsed: shouldCollapseCell(indexPath: indexPath))
}
if let disclaimerCell = tableView.cellForRow(at: indexPath) as? PlanDisclaimerTableViewCell {
disclaimerCell.setCollapsed(collapsed: shouldCollapseCell(indexPath: indexPath))
}
DispatchQueue.main.async {
tableView.reloadRows(at: [indexPath], with: .automatic)
}
}
private func shouldCollapseCell(indexPath: IndexPath) -> Bool {
if let isExpanded = viewModel?.cellIsExpanded(at: indexPath),
isExpanded {
self.viewModel?.removeExpandedIndexPath(indexPath)
return true
}
self.viewModel?.addExpandedIndexPath(indexPath)
return false
}
// TableViewCell Class:
func setCollapsed(collapsed: Bool) {
self.toggleArrowImage.image = (collapsed ? expandImage : collapseImage)
self.stackView.isHidden = collapsed
}
you need to do the following to fix it
var cellHeights:[IndexPath:CGFloat] = [ : ]
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if let height = cellHeights[indexPath]{
return height
}
return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cellHeights[indexPath] = cell.frame.size.height
}
let me know once you have tested!

How do select a row from each section of the tableview in swift?

I want to select a row from different sections of the same table-view. I am getting output that many rows are selecting but I want exactly only one selected row from each section.
Here is My Arrays:
var filterFeedUnderAll = ["Complex","NotComplex","Easy"]
var filterFeedUnderAllStocks = ["AllStocks","Portfolio","Watchlist","Sector","Ticker"]
var filterFeedUnderByDate = ["ByDate","ByComments","ByLikes"]
The methods I have used:
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 3
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
var count:Int?
if section == 0
{
count = filterFeedUnderAll.count
}
else if section == 1
{
count = filterFeedUnderAllStocks.count
}
else if section == 2
{
count = filterFeedUnderByDate.count
}
return count!
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = self.m_HomeFeedFilterBaseTableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! HomeFeedFIlterBaseTableViewCell
switch (indexPath.section)
{
case 0:
cell.m_TableItemsLabel.text = filterFeedUnderAll[indexPath.row]
case 1:
cell.m_TableItemsLabel.text = self.filterFeedUnderAllStocks[indexPath.row]
case 2:
cell.m_TableItemsLabel.text = filterFeedUnderByDate[indexPath.row]
default:
cell.m_TableItemsLabel.text = "Other"
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let cell = m_HomeFeedFilterBaseTableView.cellForRowAtIndexPath(indexPath) as! HomeFeedFIlterBaseTableViewCell
for selectedIndexPath: NSIndexPath in tableView.indexPathsForSelectedRows!
{
if selectedIndexPath.section == indexPath.section
{
cell.m_TableItemsLabel.textColor = selectedTextColor
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
}
I want to select one row from each section. Help me to achieve this task.
first of all you need to enable multiple selection in your tableView and then this is the code that I used to do that, note that I use a Dictionary with format [String:NSIndexPath] named selectedRows where I store one indexPath by section I do this in addSelectedCellWithSection
UPDATED for last swift
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate {
#IBOutlet weak var tableView: UITableView!
var filterFeedUnderAll = ["Complex","NotComplex","Easy"]
var filterFeedUnderAllStocks = ["AllStocks","Portfolio","Watchlist","Sector","Ticker","bla bla bla1","bla bla bla2","bla bla bla3","bla bla bla1","bla bla bla2","bla bla bla3","bla bla bla1","bla bla bla2","bla bla bla3"]
var filterFeedUnderByDate = ["ByDate","ByComments","ByLikes"]
var selectedRows = [String:IndexPath]()
var alert : UIAlertController?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int
{
return 3
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50;
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! mycustomHeader
let layer = CAShapeLayer()
let corners = UIRectCorner.topLeft.union(UIRectCorner.topRight)
layer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: headerCell.frame.width, height: headerCell.frame.height), byRoundingCorners: corners, cornerRadii:CGSize(width: 20.0, height: 20.0)).cgPath
headerCell.layer.mask = layer
return headerCell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
var count:Int?
if section == 0
{
count = filterFeedUnderAll.count
}
else if section == 1
{
count = filterFeedUnderAllStocks.count
}
else if section == 2
{
count = filterFeedUnderByDate.count
}
return count!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! testCell
switch (indexPath.section)
{
case 0:
cell.lblName.text = filterFeedUnderAll[indexPath.row]
case 1:
cell.lblName.text = self.filterFeedUnderAllStocks[indexPath.row]
case 2:
cell.lblName.text = filterFeedUnderByDate[indexPath.row]
default:
cell.lblName.text = "Other"
}
cell.lblName.textColor = UIColor.black
if(self.indexPathIsSelected(indexPath)) {
cell.lblName.textColor = UIColor.red
}
return cell
}
func addSelectedCellWithSection(_ indexPath:IndexPath) ->IndexPath?
{
let existingIndexPath = selectedRows["\(indexPath.section)"]
selectedRows["\(indexPath.section)"]=indexPath;
return existingIndexPath
}
func indexPathIsSelected(_ indexPath:IndexPath) ->Bool {
if let selectedIndexPathInSection = selectedRows["\(indexPath.section)"] {
if(selectedIndexPathInSection.row == indexPath.row) { return true }
}
return false
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = self.tableView.cellForRow(at: indexPath) as! testCell
let previusSelectedCellIndexPath = self.addSelectedCellWithSection(indexPath);
if(previusSelectedCellIndexPath != nil)
{
let previusSelectedCell = self.tableView.cellForRow(at: previusSelectedCellIndexPath!) as! testCell
previusSelectedCell.lblName.textColor = UIColor.black
cell.lblName.textColor = UIColor.red
tableView.deselectRow(at: previusSelectedCellIndexPath!, animated: true)
}
else
{
cell.lblName.textColor = UIColor.red
}
for selectedIndexPath: IndexPath in tableView.indexPathsForSelectedRows!
{
if selectedIndexPath.section == indexPath.section
{
cell.lblName.textColor = UIColor.red
tableView.deselectRow(at: indexPath, animated: true)
}
}
}
}
Hope this helps you, for me works perfect

Resources