Here i am having four sections in a table view but in the first section all the cells are having radio buttons. Whenever it is active, I need to show remaining three sections, if not I need to hide the remaining last three sections can anyone help me how to implement this ?
@IBAction func selectRadioButton(_ sender: KGRadioButton) {
let chekIndex = self.checkIsRadioSelect.index(of: sender.tag)
if sender.isSelected {
} else{
if(chekIndex == nil){
self.checkIsRadioSelect.removeAll(keepingCapacity: false)
self.checkIsRadioSelect.append(sender.tag)
self.ProductTableView.reloadData()
}
}
}
func numberOfSections(in tableView: UITableView) -> Int{
return 4
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if (section == 0){
return "PAYMENT INFORMATION"
}
else if (section == 1){
return "ORDER REVIEW"
}
else if (section == 2){
return "PRODUCT"
}
else{
return ""
}
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.gray
header.textLabel?.textAlignment = NSTextAlignment.center
header.textLabel?.font = UIFont(name: "Futura", size: 17)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 0){
return paymentmethodsArray.count
}
else if (section == 2) {
return productsDetails.count
}
else{
return 1
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
if (indexPath.section == 0){
return 44
}
else if (indexPath.section == 1){
return 410
}
else if (indexPath.section == 2){
return 120
}
else{
return 230
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath.section == 0){
let paymentcell = tableView.dequeueReusableCell(withIdentifier: "paymentcell",for:indexPath) as! paymentTableViewCell
myActivityIndicator.stopAnimating()
let arr = self.paymentmethodsArray[indexPath.row]
paymentcell.paymentNameLabel.text = arr["name"]as? String
paymentcell.radioButton.tag = indexPath.row
let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row)
if(checkIndex != nil){
paymentcell.radioButton.isSelected = true
}else{
paymentcell.radioButton.isSelected = false
}
return paymentcell
}
else if (indexPath.section == 1){
let shippingAddresscell = tableView.dequeueReusableCell(withIdentifier: "shippingaddresscell",for:indexPath) as! ShippingAddressTableViewCell
shippingAddresscell.nameLabel.text = name
shippingAddresscell.addressLabel.text = billingaddress
shippingAddresscell.mobileNumberLabel.text = String(describing: phoneNumber)
shippingAddresscell.shippingNameLabel.text = name
shippingAddresscell.shippingAddressLabel.text = billingaddress
shippingAddresscell.shippingMobileNumberLabel.text = String(describing: phoneNumber)
shippingAddresscell.shippingMethodNameLabel.text = shippingMethod
shippingAddresscell.paymentMethodNameLabel.text = paymentMethod
return shippingAddresscell
}
else if (indexPath.section == 2){
let productNamecell = tableView.dequeueReusableCell(withIdentifier: "productNamecell",for:indexPath) as! ProductNameTableViewCell
let arr = productsDetails[indexPath.row]
let array = arr["product name"] as! String
productNamecell.productNameLabel.text = array
productNamecell.priceLabel.text = arr["Price"] as! String
productNamecell.QuantityLabel.text = String(describing: arr["Quantity"] as! Int)
productNamecell.subTotalLabel.text = arr["SubTotal"] as! String
return productNamecell
}
else{
let ordercell = tableView.dequeueReusableCell(withIdentifier: "ordercell",for:indexPath) as! orderTableViewCell
ordercell.subTotalLabel.text = subTotal
ordercell.shippingLabel.text = shippingHandling
ordercell.grandTotalLabel.text = total
return ordercell
}
}
Take one flag (Bool I mean), It's initial value will be
false
. now on radio button's selection set it's value totrue
.Now, change
numberofSection
method. It will conditionally returns1
or4
. I mean if your flag wastrue
then return4
else1
! that's it!Here you need to change the
numberOfSections
method according to the condition. Instead of returningnumberOfSections
as 4 you need to change it according to the radio button selection. You can keep a boolean variable to check whether the button is selected or not and based on its value change numberOfSections method likeKeep the
Bool
variable to identify the radio button is selected or not based on that return thecount
ofnumber of section
.