i have a table view in which i'm populating data getting from my service. the data is totally dynamic and table view contain sections and cell under it all the things are dynamic. I have a button action outside the table view which is used to add the selected cell data. Now i want to restrict the button that it does not add the data till all the cell under the sections are selected. I want user to first check the cells and than add through add button. My code for the table view is this,
func numberOfSections(in tableView: UITableView) -> Int {
return AddonCategoryModel!.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return AddonCategoryModel![section].name
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 34
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return AddonCategoryModel![section].addonItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = addonTableView.dequeueReusableCell(withIdentifier: "addonCell", for: indexPath) as! RestaurantMenuDetailAddonTVC
cell.addonTitleLbl.text = AddonCategoryModel![indexPath.section].addonItems[indexPath.row].name
cell.priceLbl.text = String(AddonCategoryModel![indexPath.section].addonItems[indexPath.row].price)
if selection[indexPath.section].isSelected[indexPath.row] {
cell.radioBtn.setImage(UIImage (named: "radio"), for: UIControlState.normal)
addonItemName = cell.addonTitleLbl.text!
addonItemprice = AddonCategoryModel![indexPath.section].addonItems[indexPath.row].price
addonItemId = AddonCategoryModel![indexPath.section].addonItems[indexPath.row].addonPKcode
addonItemNameArray.append(addonItemName)
addonItemPriceArray.append(addonItemprice)
addonItemIdArray.append(addonItemId)
let defaults = UserDefaults.standard
defaults.set(addonItemName, forKey: "addonItemName")
defaults.set(addonItemprice, forKey: "addonItemPrice")
defaults.set(addonItemId, forKey: "addonItemId")
defaults.synchronize()
}
else {
cell.radioBtn.setImage(UIImage (named: "uncheckRadio"), for: UIControlState.normal)
}
cell.radioBtn.tag = indexPath.row
// cell.radioBtn.addTarget(self, action: #selector(checkBoxSelection(_:)), for: .touchUpInside)
cell.selectionStyle = .none
cell.backgroundColor = UIColor.clear
return cell
}
My screen looks like this,
Basically, you have to set selected true and false based on user have selected the row or deselected the row, then just check in your data set is anything selected if yes then make the button highlighted/enable else disable/unhighlighted
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selection[indexPath.section].isSelected = true
tableView.reloadData()
CheckIfAnyOneIsSelected()
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
selection[indexPath.section].isSelected = false
tableView.reloadData()
CheckIfAnyOneIsSelected()
}
func CheckIfAnyOneIsSelected() {
//loop through your array and check if anyone is selected if yes break the loop and set the button to enable
//else make the button disable
var anyOneSelecte = false
for singleModel in AddonCategoryModel {
for item in addonItems {
if item.isSelected == true
anyOneSelecte = true
break;
}
}
if anyOneSelecte {
//enable your button
} else {
//disable your button
}
}
I have created demo, Let's say you have two Model classes,
class AddOnCategoryModel {
var name: String = ""
var arrValues = [Category]()
init(name: String) {
self.name = name
}
}
class Category {
var name: String = ""
var price : String = ""
var isSelected: Bool = false
}
and following is the mainArray,
for i in 0...2 {
let model = AddOnCategoryModel(name: "Section \(i)")
for j in 0...3 {
let cate = Category()
cate.name = "Category \(j)"
model.arrValues.append(cate)
}
mainArray.append(model)
}
Now considering you have following ListTableCell
There are two IBOutlets
#IBOutlet weak var lblTemp: UILabel!
#IBOutlet weak var btnRadio: UIButton!
FYI. Please set btnRadio default and selected image properly.
Your UITableViewDataSource methods,
func numberOfSections(in tableView: UITableView) -> Int {
return mainArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mainArray[section].arrValues.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ListTableCell") as! ListTableCell
let category = mainArray[indexPath.section]
cell.lblTemp.text = category.arrValues[indexPath.row].name
cell.btnRadio.tag = indexPath.row
cell.tag = indexPath.section
cell.btnRadio.addTarget(self, action: #selector(btnRadioTapped), for: .touchUpInside)
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return mainArray[section].name
}
Please find btnRadioTapped method,
#objc func btnRadioTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
let cell = sender.superview?.superview as! ListTableCell
let addOnModel = mainArray[cell.tag]
let category = addOnModel.arrValues[sender.tag]
category.isSelected = sender.isSelected
}
Not let's check all checkbox's are selected or not in button tap event like this,
#IBAction func btnTapped(_ sender: UIButton) {
var isCheckedAll = true
for (_ , item) in mainArray.enumerated() {
let value = item.arrValues.filter({$0.isSelected==false})
if value.count > 0 {
isCheckedAll = false
break;
}
}
print("Done ", isCheckedAll)
}
It will return true if all radioButtons are selected, and return false if any one radioButton is not selected.
Let me know in case of any queries. This is just demo, you have to do minor changes as per your final requirements.
UPDATE
Please find didSelectRowAt indexPath method below,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let model = mainArray[indexPath.section]
let category = model.arrValues[indexPath.row]
category.isSelected = !category.isSelected
let cell = tableView.cellForRow(at: indexPath) as! ListTableCell
cell.btnRadio.isSelected = category.isSelected
}
Related
I have two buttons in my user's profile page, one for the saved shop items and one for his reviews.
I want when the user clicks the saved button it would load his saved shop's items in the table view and when he clicks the reviews button it would load his reviews.
I'm struggling on how to figure out how to do this
Any help, please?
here is my code:
#IBOutlet weak var reviewsBtn: UIButton!
#IBOutlet weak var saveBtntab: UIButton!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(reviewsBtn.isSelected == true){
print("review selected")
return reviews.count
}
if(saveBtntab.isSelected == true){
print("saved selected")
return shops.count
}
return shops.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellFave", for: indexPath) as! FaveTableViewCell
let shops = self.shops[indexPath.row]
let reviews = self.reviews[indexPath.row]
// i want to do the same idea for the number of rows here.
}
#IBAction func reviewsTapped(_ sender: Any) {
reviewsBtn.isSelected = true
reviewsBtn.isEnabled = true
faveBtntab.isEnabled = false
faveBtntab.isSelected = false
}
#IBAction func savedTapped(_ sender: Any) {
faveBtntab.isSelected = true
faveBtntab.isEnabled = true
reviewsBtn.isEnabled = false
reviewsBtn.isSelected = false
}
First of all if there are only two states you can simplify numberOfRows
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return reviewsBtn.isSelected ? reviews.count : shops.count
}
In cellForRow do the same thing, display the items depending on reviewsBtn.isSelected
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellFave", for: indexPath) as! FaveTableViewCell
if reviewsBtn.isSelected {
let reviews = self.reviews[indexPath.row]
// assign review values to the UI
} else {
let shops = self.shops[indexPath.row]
// assign shop values to the UI
}
}
And don't forget to call reloadData when the state has changed.
You can create two different dataSource instances for clarity and separation like following -
class ShopsDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
var shops: [Shop] = []
var onShopSelected: ((_ shop: Shop) -> Void)?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return shops.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ShopTableViewCell", for: indexPath) as! ShopTableViewCell
let shop = self.shops[indexPath.row]
cell.populateDetails(shop: shop)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.onShopSelected?(shops[indexPath.row])
}
}
class ReviewsDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
var reviews: [Review] = []
var onReviewSelected: ((_ review: Review) -> Void)?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return reviews.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReviewTableViewCell", for: indexPath) as! ReviewTableViewCell
let review = self.reviews[indexPath.row]
cell.populateDetails(review: review)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.onReviewSelected?(reviews[indexPath.row])
}
}
class ViewController: UIViewController {
let shopsDataSource = ShopsDataSource()
let reviewsDataSource = ReviewsDataSource()
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(ShopTableViewCell.self, forCellReuseIdentifier: "ShopTableViewCell")
tableView.register(ReviewTableViewCell.self, forCellReuseIdentifier: "ReviewTableViewCell")
shopsDataSource.onShopSelected = { [weak self] (shop) in
self?.showDetailsScreen(shop: shop)
}
reviewsDataSource.onReviewSelected = { [weak self] (review) in
self?.showDetailsScreen(review: review)
}
}
#IBAction func shopsTapped(_ sender: Any) {
tableView.dataSource = shopsDataSource
tableView.delegate = shopsDataSource
tableView.reloadData()
}
#IBAction func addNewShop(_ sender: Any) {
/// ask user about shop details and add them here
shopsDataSource.shops.append(Shop())
tableView.reloadData()
}
func showDetailsScreen(shop: Shop) {
/// Go to shop details screen
}
#IBAction func reviewsTapped(_ sender: Any) {
tableView.dataSource = reviewsDataSource
tableView.delegate = reviewsDataSource
tableView.reloadData()
}
#IBAction func addNewReview(_ sender: Any) {
/// ask user about review details and add them here
reviewsDataSource.reviews.append(Review())
tableView.reloadData()
}
func showDetailsScreen(review: Review) {
/// Go to review details screen
}
}
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()
}
}
I am trying to load my different controller using Expandable Tableview but my headerview is set
as switch condition
For Header XXX1 -> two sub menu a and b ..
For Header XXX2-> sub menu c
but for Header XXX3 no sub menu ,, So i will work on click with XXX3(currently working with check SectionData.count == 0 ) but for multiple how to manage .. check out my code
sectionNames = ["xxxx1","xxxx2","xxx3","xxxx4"] //this is main header
sectionItems = [ ["a","b"],[c],[],[],[],[],[],[]]// This is sub menu items
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.expandedSectionHeaderNumber == section) {
let arrayOfItems = self.sectionItems[section] as! NSArray
return arrayOfItems.count;
} else {
return 0;
}
//return arraylist.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if (self.sectionNames.count != 0) {
return self.sectionNames[section] as? String
}
return ""
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60.0;
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
return footerView
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifer, for: indexPath)
let section = self.sectionItems[indexPath.section] as! NSArray
cell.textLabel?.textColor = UIColor.black
cell.textLabel?.text = section[indexPath.row] as? String
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
}
let indexPath = tableView.indexPathForSelectedRow
// print(indexPath as Any)
//getting the current cell from the index path
let currentCell = tableView.cellForRow(at: indexPath!)! as UITableViewCell
// print(currentCell as Any)
//getting the text of that cell
let currentItem = currentCell.textLabel!.text
print(currentItem!)
switch currentItem {
case "XXXX1":
//// Here unable to do any work
break
case "a":
APICalla()
case "b":
APICallb ()
default:
break
}
return
}
Using this link
Sorry this tutorial is quite poor.
Swift is an object oriented language so use a custom model, a generic Section object with name, items and the information if the section is collapsed
class Section<T> {
var name : String
var items = [T]()
var isCollapsed = false
init(name : String, items : [T] = []) {
self.name = name
self.items = items
}
}
and a suitable struct for the items with a title and a closure to be called in didSelect
struct Item {
let title : String
let selectorClosure : (() -> Void)?
}
Rather than using multiple arrays populate the data source array consistently
var sections = [Section<Item>(name:"xxxx1", items: [Item(title: "a", selectorClosure: APICalla), Item(title: "b", selectorClosure: APICallb)]),
Section<Item>(name:"xxxx2", items: [Item(title: "c", selectorClosure: APICallc)]),
Section<Item>(name:"xxxx3")]
In numberOfRowsInSection return the proper number of items depending on isCollapsed
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let currentSection = sections[section]
return (currentSection.isCollapsed) ? 0 : currentSection.items.count
}
In cellForRow don't use typeless Foundation collection types
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifer, for: indexPath)
let item = sections[indexPath.section].items[indexPath.row]
cell.textLabel?.textColor = UIColor.black
cell.textLabel?.text = item.title
return cell
}
In the method to collapse/expand the sections just toggle isCollapsed
let currentSection = sections[section]
currentSection.isCollapsed.toggle()
and perform the animation
titleForHeaderInSection is much simpler, too
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section].name
}
In didSelectRow never get any data from the view (the cell) get it from the model (the data source array) and call the selector closure. With this logic a switch is not needed.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
let item = sections[indexPath.section].items[indexPath.row]
item.selectorClosure?()
}
Swift4 I think this will helps you
// declare globally
var isExpanded : Bool = true
var indexOfSection = Int()
var yourArray = [ModelName]()
override func viewDidLoad() {
super.viewDidLoad()
indexOfSection = 999
}
extension ViewController: UITableViewDelegate, UITableViewDataSource
{
func numberOfSections(in tableView: UITableView) -> Int {
if yourArray.count > 0{
return yourArray.count
}else{
return 0
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRect(x: view.frame.origin.x,y: 0 , width: view.frame.size.width ,height: 60))
headerView.backgroundColor = .white
let collapseBtn = UIButton(frame: CGRect(x: headerView.frame.origin.x,y: headerView.frame.origin.y , width: view.frame.size.width ,height: 60))
collapseBtn.addTarget(self, action: #selector(expandSection(sender:)), for: .touchUpInside)
collapseBtn.tag = section
collapseBtn.backgroundColor = .clear
headerView.addSubview(collapseBtn)
return headerView
}
#objc func expandSection(sender:UIButton){
print(sender.tag)
if isExpanded == true{
indexOfSection = sender.tag
mIdeaTableView.reloadData()
isExpanded = false
mTableView.reloadSections([indexOfSection], with: UITableView.RowAnimation.bottom)
}else{
indexOfSection = 999
isExpanded = true
self.mTableView.reloadData()
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if yourArray.count > 0{
if yourArray[section].items!.count > 0{
if indexOfSection == section{
return yourArray[section].items!.count
}else{
return 0
}
}else{
return 0
}
}else{
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: “CellID”, for: indexPath) as! Cell
if yourArray[indexPath.section]. items!.count > 0{
if yourArray[indexPath.section]. items!.count > 0{
let ideas = yourArray[indexPath.section].ideaItems
if ideas!.count > 0{
if indexOfSection == indexPath.section{
cell.mLbl.text = ideas![indexPath.row].name ?? ""
if ideas![indexPath.row].isExpanded == true{
cell.mAddImg.image = #imageLiteral(resourceName: "tick")
}else{
cell.mAddImg.image = #imageLiteral(resourceName: "edit213-1")
}
}
}
}
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
}
//Structure of my response
{
items = (
{
name = “a”;
},
{
name = “b”;
},
);
name = “xxxx1”;
}
items = (
{
name = “c”;
},
);
name = “xxxx2”;
}
}
class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource{
#IBOutlet weak var tableView: UITableView!
var questions:[Question] = []
var sectionCountGlobal = 0
override func viewDidLoad() {
super.viewDidLoad()
questions = fillQuestions()
}
func fillQuestions()-> [Question]{
var temp : [Question] = []
var choices : [Choice] = []
let choice = Choice(id: 1, text: "choice ", status: 1, questionId: 1)
choices.append(choice)
choices.append(choice)
choices.append(choice)
choices.append(choice)
choices.append(choice)
choices.append(choice)
let q1 = Question(id: 1, text: "Ahmad 55 years old man with a history of hypertension and hypocholesteremia was in a wedding and during the party he starts to feel chest pain and dizzy, his wife brought him to the emergency department. The ER nurse checked his vital signs: BP 88/50, HR: 45, RR:10, SPaO2: 90% and O2 per nasal cannula was started at 4l/minute. Few seconds later Mr.Ahmad lost consciousness and the code blue team were activated.", choices: choices)
let q2 = Question(id: 1, text: "question 2", choices: choices)
let q3 = Question(id: 1, text: "question 3", choices: choices)
temp.append(q1)
temp.append(q2)
temp.append(q3)
return temp
}
func numberOfSections(in tableView: UITableView) -> Int {
return questions.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
sectionCountGlobal = section
return questions[section].choices.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0{
let questionTextCell = tableView.dequeueReusableCell(withIdentifier: "QuestionTextCell") as! QuestionTextCell
questionTextCell.setQuestionText(text: questions[indexPath.section].text)
return questionTextCell
}else{
let choiceCell = tableView.dequeueReusableCell(withIdentifier: "ChoiceCell") as! ChoiceCell
choiceCell.choiceText.text = questions[indexPath.section].choices[indexPath.row].text
return choiceCell
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let questionNumber = "Q" + String(section+1)
return questionNumber
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 3
}
override func viewWillAppear(_ animated: Bool) {
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
}
I am working on a quiz app and there is multiple choices for each question so when checking the radio button in a cell and scroll to other cells i found that the other cells got checked without touching them what is the solution.
I tried different cell reusing methods also prepareForReuse() and nothing works how can i treat each cell independently without affect from other cells , i don't know the number of questions it is come from server.
In your cellForRowAt implementation you have to reset the cell's state according to whether it is selected or not. Due to cell reuse, you can get a cell which was previously selected, but now should not be selected - in that case you have to tell the cell to get unselected (and vice versa):
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0{
let questionTextCell = tableView.dequeueReusableCell(withIdentifier: "QuestionTextCell") as! QuestionTextCell
questionTextCell.setQuestionText(text: questions[indexPath.section].text)
return questionTextCell
} else {
let choiceCell = tableView.dequeueReusableCell(withIdentifier: "ChoiceCell") as! ChoiceCell
// here detect if the cell should be selected and set it accordingly, so something like:
let isSelected = isSelectedChoice(questions[indexPath.section].choices[indexPath.row])
choiceCell.isSelected = isSelected
// of course this is just a mockup, since I don't know exactly how you manage selection,
// but it should get you on the right path
choiceCell.choiceText.text = questions[indexPath.section].choices[indexPath.row].text
return choiceCell
}
}
The issue in you code is you not changing the status of your radio button. When you select the option from didSelectRowAt method, you have to change the status of your choice. As per your choice model you can change the status of particular choice status. Following are both method that can manage your selection of choice(your status variable should be Bool type):
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0{
let questionTextCell = tableView.dequeueReusableCell(withIdentifier: "QuestionTextCell") as! QuestionTextCell
questionTextCell.setQuestionText(text: questions[indexPath.section].text)
return questionTextCell
}else{
let choiceCell = tableView.dequeueReusableCell(withIdentifier: "ChoiceCell") as! ChoiceCell
// update your radio button UI
choiceCell.radioButton.isSelected = questions[indexPath.section].choices[indexPath.row].status
choiceCell.choiceText.text = questions[indexPath.section].choices[indexPath.row].text
return choiceCell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
questions[indexPath.section].choices[indexPath.row].status = !questions[indexPath.section].choices[indexPath.row].status
tableView.reloadData()
}
I have made a drop down gender which is working fine however I have another drop down on same viewcontroller which shows drop down but when I select the item it gives error fatal error: unexpectedly found nil while unwrapping an Optional value I have tried out but confused. help me to solve the bloodList dropdown.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var genderButton: UIButton!
#IBAction func genderButton(_ sender: Any) {
self.PressDrop()
view.addSubview(genderTable)
}
#IBOutlet weak var genderTable: UITableView!
var flag = 1
var dropDownList = [String]()
#IBOutlet weak var bloodButton: UIButton!
var bloodList = [String]()
#IBAction func bloodButton(_ sender: Any) {
self.PressBlood()
view.addSubview(bloodTable)
}
#IBOutlet weak var bloodTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
dropDownList = ["Male", "Female", "Other"]
genderTable.delegate = self
genderTable.dataSource = self
genderTable.isHidden = true
view.addSubview(genderTable)
genderTable.layer.cornerRadius = 10
bloodList = ["A+", "A-", "AB+", "AB-"]
bloodTable.delegate = self
bloodTable.dataSource = self
bloodTable.isHidden = true
view.addSubview(bloodTable)
bloodTable.layer.cornerRadius = 10
}
func PressDrop() {
if flag == 0 {
UIView.setAnimationDuration(0.5)
self.genderTable.isHidden = true
self.flag = 1
}
else{
UIView.setAnimationDuration(0.5)
self.genderTable.isHidden = false
self.flag = 0
}
}
func PressBlood() {
if flag == 0 {
UIView.setAnimationDuration(0.5)
self.bloodTable.isHidden = true
self.flag = 1
}
else{
UIView.setAnimationDuration(0.5)
self.bloodTable.isHidden = false
self.flag = 0
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dropDownList.count
}
func tableViewBlood(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return bloodList.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = genderTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel!.text = dropDownList[indexPath.row]
return cell
}
func tableViewTwo(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = bloodTable.dequeueReusableCell(withIdentifier: "bloodCell", for: indexPath)
cell.textLabel!.text = bloodList[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedData = dropDownList[indexPath.row]
genderButton.setTitle(selectedData, for: .normal)
self.genderTable.isHidden = true
self.flag =
let indexPath = genderTable.indexPathForSelectedRow
let currentCell = genderTable.cellForRow(at: indexPath!)! as UITableViewCell
let finalresult = currentCell.textLabel!.text!
print("\(finalresult)")
}
private func tableViewTwo(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedBlood = bloodList[indexPath.row]
bloodButton.setTitle(selectedBlood, for: .normal)
self.bloodTable.isHidden = true
self.flag = 1
let indexPathTwo = bloodTable.indexPathForSelectedRow
let currentCellBlood = bloodTable.cellForRow(at: indexPathTwo!)! as UITableViewCell
let finalresultBlood = currentCellBlood.textLabel!.text!
print("\(finalresultBlood)")
}
}
You cannot change default delegate methods of your tableView, give conditions in the delegate methods like below :
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == genderTable {
return dropDownList.count
}
return bloodList.count
}
Also make similar changes in the remaining delegate methods.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == genderTable {
let cell = genderTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel!.text = dropDownList[indexPath.row]
return cell
} else {
let cell = bloodTable.dequeueReusableCell(withIdentifier: "bloodCell", for: indexPath)
cell.textLabel!.text = bloodList[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == genderTable {
let selectedData = dropDownList[indexPath.row]
genderButton.setTitle(selectedData, for: .normal)
self.genderTable.isHidden = true
let currentCell = genderTable.cellForRow(at: indexPath)! as UITableViewCell
let finalresult = currentCell.textLabel!.text!
print("\(finalresult)")
} else {
let selectedBlood = bloodList[indexPath.row]
bloodButton.setTitle(selectedBlood, for: .normal)
self.bloodTable.isHidden = true
let currentCellBlood = bloodTable.cellForRow(at: indexPath)! as UITableViewCell
let finalresultBlood = currentCellBlood.textLabel!.text!
print("\(finalresultBlood)")
}
}
Don't create like this
func tableViewTwo(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell (This will consider as new method to your current class)
They are all in build delegate methods of UITableview, you should override it, you cannot change it.
Instead of creating method, you try with Bool variable in respective button action.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
If isDrop == true{
return dropDownList.count
}else{
return bloodList.count
}
}
Like change the condition
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
I waited for an answer but did not find :( that's ok . I thought a number of times and then come to this code yahoooo!!! then i believed i am really a programmer. Actually i did not defined the condition for each table view so i was in nightmare. This code is pure programatically and does not require any irritating third party library or awkward pods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == tableViewB {
return dropDownList.count
}
else {
return genderL.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == tableViewB {
let cell = tableViewB.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel!.text = dropDownList[indexPath.row]
return cell
}
else {
let cell = genderT.dequeueReusableCell(withIdentifier: "gender", for: indexPath)
cell.textLabel!.text = genderL[indexPath.row]
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == tableViewB {
let selectedData = dropDownList[indexPath.row]
buttonB.setTitle(selectedData, for: .normal)
self.tableViewB.isHidden = true
self.flag = 1
let indexPath = tableViewB.indexPathForSelectedRow
let currentCell = tableViewB.cellForRow(at: indexPath!)! as UITableViewCell
let finalresult = currentCell.textLabel!.text!
print("\(finalresult)")
}
else {
let selectedDataG = genderL[indexPath.row]
genderB.setTitle(selectedDataG, for: .normal)
self.genderT.isHidden = true
self.flag = 1
let indexPath = genderT.indexPathForSelectedRow
let currentCell = genderT.cellForRow(at: indexPath!)! as UITableViewCell
let finalresult = currentCell.textLabel!.text!
print("\(finalresult)")
}
}