Make a section Visible on the action of Button from other section - ios

I'm showing the data on tableview with multiple section,
when I open tableView height of section 1 should be 0. If I click a button in section 0 the height of the section 1 will be 150. For example if I click Agent button height of (want to) section cell show 150,I click builder button section hight will 0
I want to make the Section 1 visible on the button click from section 0
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! postaddTableViewCell1
cell.ownerButton.addTarget(self, action: #selector(FisrtButtonClick), for: .touchUpInside)
cell.agentButton.addTarget(self, action: #selector(FisrtButtonClick), for: .touchUpInside)
cell.builderButton.addTarget(self, action: #selector(FisrtButtonClick), for: .touchUpInside)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! postaddTableViewCell2
cell.checkbutton.tag = indexPath.row
cell.checkbutton.addTarget(self, action: #selector(checkbuttonClick) , for: .touchUpInside)
return cell
}
}
#objc func FisrtButtonClick(_ sender :UIButton)
{
let indexPath = IndexPath(row: 0, section: 0)
let cell = TableView.cellForRow(at: indexPath) as? postaddTableViewCell1
print(cell?.agentButton.titleLabel as Any)
if sender.tag == 10
{
cell?.ownerButton.backgroundColor = #colorLiteral(red: 0, green: 0.7254901961, blue: 0.4588235294, alpha: 1)
cell?.agentButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
cell?.builderButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
}else if sender.tag == 11
{
cell?.ownerButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
cell?.agentButton.backgroundColor = #colorLiteral(red: 0, green: 0.7254901961, blue: 0.4588235294, alpha: 1)
cell?.builderButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
} else if sender.tag == 12
{
cell?.ownerButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
cell?.agentButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
cell?.builderButton.backgroundColor = #colorLiteral(red: 0, green: 0.7254901961, blue: 0.4588235294, alpha: 1)
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0
{
return 80
}
if indexPath.section == 1
{
return 0
}
return 80
}

Check if the required button is clicked in the FisrtButtonClick method and set a boolean that is accessible to heightForRowAt. After toggling the boolean you can just reload the section you require (also cleaned up your code lil bit).
var selectedAgent = false
#objc func firstButtonClick(_ sender: UIButton) {
if sender.currentTitle == "Agent" {
selectedAgent = true
tableView.reloadSections([1], with: .automatic)
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 1 && !selectedAgent {
return 0
}
return 80
}

Related

Swift collectionview cant automatically select first cell when load

so i have collectionview where i want the first cell of the collectionview cell is selected when the view is load, i try to use viewDidAppear method and.....well not work very good.
cause for the very first time i try to load the view the first collectionview cell is not selected, but when i back to previous controller and then tap back to the collectionview controller, the first cell of the collectionview is selected.
i mean i dont know what i have to go back and forth so the first cell can automatically be selected.
heres my code :
#IBOutlet weak var katalogColView: UICollectionView!
let indexPos = NSIndexPath(row: 0, section: 0) as IndexPath
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.katalogColView.selectItem(at: indexPos, animated: false, scrollPosition: [])
self.collectionView(katalogColView, didSelectItemAt: indexPos)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == katalogColView {
guard let cell = collectionView.cellForItem(at: indexPath) as? KatalogCell else {return}
cell.indicator.backgroundColor = #colorLiteral(red: 0.9916914105, green: 0.2737390399, blue: 0.284696877, alpha: 1)
cell.katalogText.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if collectionView == katalogColView {
guard let cell = collectionView.cellForItem(at: indexPath) as? KatalogCell else {return}
cell.indicator.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
cell.katalogText.textColor = #colorLiteral(red: 0.3950070143, green: 0.4162634611, blue: 0.4362195134, alpha: 1)
}
}
so the code is when the cell is selected, there's a uiview indicator that colored red if selected, and white if not selected
is there something wrong with my code? i mean of course there's something wrong but can anyone tell me why its not working?
i mean like i said it only work if i go back to previous controller, and then i go back to that collectionView controller where it automatically selected first cell, but not selected automatically at the first time i load the controller.
Thanks
Add these lines in cell class and try:
override var isSelected: Bool {
didSet {
if isSelected {
indicator.backgroundColor = #colorLiteral(red: 0.9916914105, green: 0.2737390399, blue: 0.284696877, alpha: 1)
katalogText.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
}else {
indicator.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
katalogText.textColor = #colorLiteral(red: 0.3950070143, green: 0.4162634611, blue: 0.4362195134, alpha: 1)
}
}
}
You have to select it from visibleCells.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
let selectedIndex = collectionView.indexPathsForVisibleItems![0]
collectionView.selectItem(at: selectedIndex, animated: true, scrollPosition: .none)
}

How to handle custom radio buttons

Hi I need to radio button in my project like
Radio Button Image
I take images into button, Booth selected and empty.
How to I can handle these one is selected another one is don't select.
our selection is only once.
#IBOutlet var nothisonlybtn: UIButton!
#IBOutlet var biweeklybtn: UIButton!
#IBOutlet var weeklybtn: UIButton!
How to we are handle by using of button outlets
The image you've posted shows a table view will do good in this case.
If so, use the tableView tableView: didSelectItemAt indexPath: & tableView tableView: didDeselectItemAt indexPath: methods.
func tableView(_ tableView: UITableView, didSelectItemAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! <#UITableViewCellClass#>
cell.rImageBtn.setImage(<#selected image#>, for: .normal)
}
func tableView(_ tableView: UITableView, didDeselectItemAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! <#UITableViewCellClass#>
cell.rImageBtn.setImage(<#unselected image#>, for: .normal)
}
And if the images are individual entities in a UIView, then:
#IBOutlet weak var rImageBtn1: UIButton!
#IBOutlet weak var rImageBtn2: UIButton!
#IBOutlet weak var rImageBtn3: UIButton!
#objc func button1Tapped() {
rImageBtn1.setImage(<#selected image#>, for: .normal)
rImageBtn2.setImage(<#unselected image#>, for: .normal)
rImageBtn3.setImage(<#unselected image#>, for: .normal)
}
#objc func button2Tapped() {
rImageBtn1.setImage(<#unselected image#>, for: .normal)
rImageBtn2.setImage(<#selected image#>, for: .normal)
rImageBtn3.setImage(<#unselected image#>, for: .normal)
}
#objc func button3Tapped() {
rImageBtn1.setImage(<#unselected image#>, for: .normal)
rImageBtn2.setImage(<#unselected image#>, for: .normal)
rImageBtn3.setImage(<#selected image#>, for: .normal)
}
Hope this will solve your problem.
We are giving one common action for all buttons and give those Tags like 0,1,2.
Try with this code.
#IBAction func radioButtonAction(_ sender: UIButton) {
weeklybtn.setImage(UIImage(named: "Radio Button"), for: .normal)
biweeklybtn.setImage(UIImage(named: "Radio Button"), for: .normal)
nothisonlybtn.setImage(UIImage(named: "Radio Button"), for: .normal)
weeklylbl.textColor = UIColor(red: 92/255, green: 112/255, blue: 139/255, alpha: 1.0)
biweeklylbl.textColor = UIColor(red: 92/225, green: 112/255, blue: 139/255, alpha: 1.0)
onlyForthisLbl.textColor = UIColor(red: 92/225, green: 112/255, blue: 139/255, alpha: 1.0)
if sender.tag == 0
{
weeklybtn.setImage(UIImage(named: "Checked"), for: .normal)
weeklylbl.textColor = UIColor(red: 46/225, green: 125/255, blue: 225/255, alpha: 1.0)
}
else if sender.tag == 1
{
biweeklybtn.setImage(UIImage(named: "Checked"), for: .normal)
biweeklylbl.textColor = UIColor(red: 46/225, green: 125/255, blue: 225/255, alpha: 1.0)
}
else
{
nothisonlybtn.setImage(UIImage(named: "Checked"), for: .normal)
onlyForthisLbl.textColor = UIColor(red: 46/225, green: 125/255, blue: 225/255, alpha: 1.0)
}
}
Give one common IBOutlet and IBAction for all buttons and give those Tags like 0,1,2.
Common IBOutlet
#IBOutlet var radioButtons: [UIButton]!
Common IBAction
#IBAction func radioButtonTapped(_ sender: UIButton) {
for button in radioButtons {
if button.tag == sender.tag {
button.setImage(UIImage(named: "Checked"), for: .normal)
button.titleLabel!.textColor = UIColor(red: 46/225, green: 125/255, blue: 225/255, alpha: 1.0)
}else{
button.setImage(UIImage(named: "Radio Button"), for: .normal)
button.titleLabel!.textColor = UIColor(red: 92/255, green: 112/255, blue: 139/255, alpha: 1.0)
}
}
}

Changed TableViewCell with XMSegmentedControl

I want to changed tableviewcell's data with XMSegmentedControl(https://github.com/xaviermerino/XMSegmentedControl),
and I using switch in the
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
,but it's always print
fatal error: unexpectedly found nil while unwrapping an Optional value**,and say *segmentedControl1 is nil.
let tin = ["1","2","3","4"]
let how = ["one","two","three","four"]
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var segmentedControl1: XMSegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
let segmentedControl3 = XMSegmentedControl(frame: CGRect(x: 0, y: 70, width: self.view.frame.width, height: 44), segmentTitle: ["Hello", "World", "Three"], selectedItemHighlightStyle: XMSelectedItemHighlightStyle.topEdge)
segmentedControl3.backgroundColor = UIColor(red: 22/255, green: 150/255, blue: 122/255, alpha: 1)
segmentedControl3.highlightColor = UIColor(red: 25/255, green: 180/255, blue: 145/255, alpha: 1)
segmentedControl3.tint = UIColor.white
segmentedControl3.highlightTint = UIColor.black
self.view.addSubview(segmentedControl3)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var returnValue = 0
switch(segmentedControl1.selectedSegment)
{
case 0 :
returnValue = tin.count
case 1 :
returnValue = how.count
default :
break
}
return returnValue
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
switch(segmentedControl1.selectedSegment)
{
case 0 :
cell.textLabel?.text = tin[indexPath.row]
case 1 :
cell.textLabel?.text = how[indexPath.row]
default :
break
}
return cell
}
Can anyone help me solve this problem?
Thanks!
replace your viewDidLoad() by my code.
override func viewDidLoad() {
super.viewDidLoad()
segmentedControl1 = XMSegmentedControl(frame: CGRect(x: 0, y: 70, width: self.view.frame.width, height: 44), segmentTitle: ["Hello", "World", "Three"], selectedItemHighlightStyle: XMSelectedItemHighlightStyle.topEdge)
segmentedControl1.backgroundColor = UIColor(red: 22/255, green: 150/255, blue: 122/255, alpha: 1)
segmentedControl1.highlightColor = UIColor(red: 25/255, green: 180/255, blue: 145/255, alpha: 1)
segmentedControl1.tint = UIColor.white
segmentedControl1.highlightTint = UIColor.black
self.view.addSubview(segmentedControl1)
}
put into valuechenged method of segmentControl
DispatchQueue.main.async{
self.ttblView.reloadData()
}
You have applied wrong segmentControl . You put value in segmentedControl3 so segmentedControl1 gives nil .

UITableView cellForRowAt if statement faulty work

According to the codes below, the first time I run the application.
The first 3 lines are working correctly.
But when I scroll up and down.
Some lines also change.
When I make a little more up and down
It changes to other lines
How can i solve this problem
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = KategoriEkleCell()
var KategoriId :String = ""
if tableView == tableView {
cell.selectionStyle = .none
cell = self.tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath as IndexPath) as! KategoriEkleCell
cell.itemLabel.text = (AltKategoriKayit[indexPath.section][indexPath.row] as? [String:String])?["kategoriadi"]
cell.itemLabel.font = UIFont(name:"OpenSans-Light", size: 14.0)
KategoriId = ((AltKategoriKayit[indexPath.section][indexPath.row] as? [String:String])?["kategoriid"])!
if (KategoriId) == (MenuKategoriKayit[indexPath.row]["kategoriid"]!) {
cell.EkleButton.layer.cornerRadius = 2
cell.EkleButton.layer.borderWidth = 1
cell.EkleButton.layer.borderColor = UIColor(red: 255/255, green: 56/255, blue: 107/255, alpha: 1.0).cgColor
cell.EkleButton.backgroundColor = UIColor(red: 255/255, green: 56/255, blue: 107/255, alpha: 1.0)
cell.EkleButton.setTitleColor(UIColor.white, for: .normal)
cell.EkleButton.clipsToBounds = true
}else {
cell.EkleButton.layer.cornerRadius = 2
cell.EkleButton.layer.borderWidth = 1
cell.EkleButton.layer.borderColor = UIColor(red: 255/255, green: 56/255, blue: 107/255, alpha: 1.0).cgColor
cell.EkleButton.clipsToBounds = true
}
}
return cell
}
Right employee
Faulty employee. when I scroll up and down.
As mentioned in the comments cells are reused.
You have to ensure that any UI element is set to a defined state in cellForRow. I moved the duplicate lines which both are executed in the if and else branch out of the if - else scope for better readability.
By the way the parentheses in the if line are not needed either
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let specialRed = UIColor(red: 1.0, green: 56/255, blue: 107/255, alpha: 1.0)
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! KategoriEkleCell
cell.selectionStyle = .none
cell.itemLabel.text = (AltKategoriKayit[indexPath.section][indexPath.row] as? [String:String])?["kategoriadi"]
cell.itemLabel.font = UIFont(name:"OpenSans-Light", size: 14.0)
let KategoriId = ((AltKategoriKayit[indexPath.section][indexPath.row] as? [String:String])?["kategoriid"])!
cell.EkleButton.layer.cornerRadius = 2
cell.EkleButton.layer.borderWidth = 1
cell.EkleButton.clipsToBounds = true
cell.EkleButton.layer.borderColor = specialRed.cgColor
if KategoriId == MenuKategoriKayit[indexPath.row]["kategoriid"]! {
cell.EkleButton.backgroundColor = specialRed
cell.EkleButton.setTitleColor(UIColor.white, for: .normal)
} else {
cell.EkleButton.backgroundColor = UIColor.white
cell.EkleButton.setTitleColor(specialRed, for: .normal)
}
return cell
}
Please consider that according to the naming convention variable names are supposed to start with a lowercase letter.

Function does not work correctly until after a row has been selected

I have a tableview inside a viewcontroller. I have a little function to select all rows in the tableview. When I first display the viewcontroller and hit the select all button the function does not work. However, if I firstly select a row and then press the select all button, the function works as it should and all rows are selected. I'm not sure why this is happening. The tableview's delegate and data source have been set up in the storyboard.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:myTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! myTableViewCell
cell.accessoryType = .None
if allJobsSelected {
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor(red: 250/255, green: 182/255, blue: 17/255, alpha: 1)
cell.contentView.backgroundColor = UIColor(red: 250/255, green: 182/255, blue: 17/255, alpha: 1)
cell.selectedBackgroundView = bgColorView
cell.accessoryType = .Checkmark
cell.highlighted = false
cell.selected = true
// cell.accessoryType = .Checkmark
self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
self.tableView(self.tableView, didSelectRowAtIndexPath: indexPath)
}
var job: Jobs!
job = jobs[UInt(indexPath.row)] as! Jobs
cell.reports2JobTitle.text = job.jobTitle
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.allowsMultipleSelection = true
if let cell:myTableViewCell = tableView.cellForRowAtIndexPath(indexPath) as? myTableViewCell {
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor(red: 250/255, green: 182/255, blue: 17/255, alpha: 1)
cell.contentView.backgroundColor = UIColor(red: 250/255, green: 182/255, blue: 17/255, alpha: 1)
cell.selectedBackgroundView = bgColorView
cell.accessoryType = .Checkmark
cell.highlighted = false
self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.Bottom)
}
}
#IBAction func doSelectAll(sender: UIBarButtonItem) {
let totalRows = tableView.numberOfRowsInSection(0)
for row in 0..<totalRows {
tableView.selectRowAtIndexPath(NSIndexPath(forRow: row, inSection: 0), animated: false, scrollPosition: UITableViewScrollPosition.None)
}
}
It would probably be a good idea to move this line:
self.tableView.allowsMultipleSelection = true
to your viewDidLoad
You are not guaranteed that the didSelect is called immediately -- it might be that each select is turning off the previous one and then a single didSelect is being called on the last row.

Resources