How to change text of Label in UITableView based on button pressed? - ios

I have a UITableView in ViewController which displays Default, five times on running in the simulator based on the following code.
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
#IBOutlet weak var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int
{
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! OutsideCell
myCell.titleLabel.text = "Default"
return myCell
}
#IBAction func firstButtonPressed(_ sender: Any){
}
#IBAction func secondButtonPressed(_ sender: Any) {
}
}
I also have 2 buttons at the bottom of ViewController and
I like to display text as First Button when firstButtonPressed and Second Button when secondButtonPressed instead of Default in UITableView. How is this possible?

You can loop through each cell of the table view when the button is tapped.
for cell in myTableView.visibleCells() {
cell.titleLabel?.text = "First Button"
}

You should use a generic method as eventButtonPressed and use the tag attribute to determine which button is pressed through the storyboard.
#IBAction func eventButtonPressed(_ sender: UIButton) {
if (sender.tag == 0) {
//First Button
} else if (sender.tag == 1) {
//Second Button
}
}
You can use switch operand if you have many cases.
switch sender.tag {
case 0:
print("First button pressed")
case 1:
print("Second button pressed")
default:
print("Default button pressed")
}
Hope it helps,

Related

Change button title in section of tableview on specific row

I have view controller in which there are multiple section of tableview. In section 0 I have multiple row . Each row having button named as Add Comments when I click on button it pushes me to other view controller having text field when i wrote something and press done button then through delegate I passes textfield data and set it in button title. But problem is my button present in all row changes value. I want only selected row in section changes its button title. below is my code of first viewcontroller
class MyTabViewController: UIViewController {
var addCommentsValueStore: String = "Add Comments"
#IBOutlet weak var tabTableView : ContentWrappingTableView!
#IBAction func addCommentsAction(_ sender: UIButton) {
guard let nextVC = MyCommentsRouter.getMyCommentsViewScreen() else { return }
nextVC.passAddCommentsDelegate = self
self.navigationController?.pushViewController(nextVC, animated: true)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.section == 0)
{
let indetifier = "MyTabTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: indetifier, for: indexPath) as! MyTabTableViewCell
cell.addCommentsButton.setTitle(addCommentsValueStore, for: UIControl.State.normal)
}
return cell
}
extension MyTabViewController: AddCommentsDelegate{
func passAddComments(instruction: String) {
addCommentsValueStore = instruction
print(addCommentsValueStore)
}
}
}
below is code of second view controller:
import UIKit
protocol AddCommentsDelegate{
func passAddComments(instruction: String)
}
class MyCommentsViewController: UIViewController {
#IBOutlet var addCommentsTextField: UITextField!
var passAddCommentsDelegate: AddCommentsDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func backActionClick(_ sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
#IBAction func DoneActionClick(_ sender: Any) {
let dataToBeSent = addCommentsTextField.text
self.passAddCommentsDelegate?.passAddComments(instruction: dataToBeSent!)
self.navigationController?.popViewController(animated: true)
}
}
by using tag on the cell button, you can fix your problem.
on your cellForRowAt delegate method, put this line:
cell.addCommentsButton.tag = indexPath.item
now you know exactly which button did select, then you can use this tag to specify which row in your tableView should change its title.
your implementation has some problems:
first of all, the addCommentsValueStore have to be an array of strings.
var addCommentsValueStore: [String] = []
then tell the delegate to show the right title:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: MyTabTableViewCell!
if (indexPath.section == 0) {
cell = tableView.dequeueReusableCell(withIdentifier: "MyTabTableViewCell", for: indexPath) as! MyTabTableViewCell
cell.tag = indexPath.item
cell.addCommentsButton.setTitle(addCommentsValueStore[indexPath.item], for: UIControl.State.normal)
}
return cell
}
the AddCommentsDelegate should return the index too:
protocol AddCommentsDelegate {
func passAddComments(instruction: String, atIndex: Int)
}
then every time you want to pass comment to another viewController, you should pass the index too.
by using this index, you will specify where you should change the row button title.
#UIBotton func DoneActionClick(_ sender: UIButton) {
let dataToBeSent = addCommentsTextField[sender.tag].text
self.passAddCommentsDelegate?.passAddComments(instruction: dataToBeSent!, atIndexPath: sender.tag)
self.navigationController?.popViewController(animated: true)
}

is it possible to handle UICollectionViewCell if i click UIButton outside UICollectionView

First, I'm sorry but i'm not good at English.
Anyway, i want to select all cells in UICollectionViewCell when i click UIButton outside UICollectionView. But i don't how can i do.
I think it will be possible using collections of UIButton like 5 stars. i just try it.
well.. click button below. The cells in blue is false.
Main view
if you click 'Everyday' Button, All cells will be red. The cells in red means it is chosen.
Selected Everyday
if you click 'Weekend' Button, the two rightmost cells will be red. now you know this UICollectionView means 'a week'. so, when 'Weekend' is selected, other 5 days will be blue.
Selected Weekend
i made this with UICollectionView. but i don't know how i control with button outside. if you need more information and code, or you don't understand i'll edit it or leave a comment. please tell me how.
ViewController code about UIButton and UICollectionView is here. and there is a imageview in CollectionViewCell... that's all...
import UIKit
class CreateViewController: UIViewController {
#IBOutlet weak var titleTextField: UITextField!
#IBOutlet weak var MotivTextField: UITextField!
#IBOutlet weak var repeatCollectionView: UICollectionView!
#IBAction func everydayBtn(_ sender: Any) {
// empty
}
#IBAction func weekdayBtn(_ sender: Any) {
// empty
}
#IBAction func weekendBtn(_ sender: Any) {
// empty
}
var selectedDay = [Bool]()
override func viewDidLoad() {
super.viewDidLoad()
for _ in 0...7 {
self.selectedDay.append(false)
}
repeatCollectionView.delegate = self
repeatCollectionView.dataSource = self
repeatCollectionView.isScrollEnabled = false
}
}
extension CreateViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? RepeatDayCVCell {
cell.dayBtn.backgroundColor = UIColor.red
}
}
}
extension CreateViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 7
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RepeatDayCVCell", for: indexPath) as! RepeatDayCVCell
if self.selectedDay[indexPath.row] == true {
cell.dayBtn.backgroundColor = UIColor.red
}
else {
cell.dayBtn.backgroundColor = UIColor.blue
}
return cell
}
}
You will have to update your data model and reload the collection view on each button action.
#IBAction func weekendBtn(_ sender: Any) {
for index in 0..<selectedDay.count {
if index < 5 {
selectedDay[index] = false
} else {
selectedDay[index] = true
}
}
repeatCollectionView.reloadData()
}
#IBAction func weekdayBtn(_ sender: Any) {
for index in 0..<selectedDay.count {
if index < 5 {
selectedDay[index] = true
} else {
selectedDay[index] = false
}
}
repeatCollectionView.reloadData()
}
#IBAction func everydayBtn(_ sender: Any) {
for index in 0..<selectedDay.count {
selectedDay[index] = true
}
repeatCollectionView.reloadData()
}
If you want to update the collection view when tapping on cell, update the data in selectedDay at that index and reload that row.
Pls excuse the formatting.
You can manipulate your model, I mean about array selectedDay. It depends on the case - you can set true/false at specific indexes. After an update the array, you have to reload data using reloadData() method to see the effect at UI layer.
#IBAction func everydayBtn(_ sender: Any) {
for i in 0...7 {
self.selectedDay[i] = true
}
repeatCollectionView.reloadData()
}
#IBAction func weekdayBtn(_ sender: Any) {
for i in 0...7 {
self.selectedDay[i] = i < 5 ? true : false
}
repeatCollectionView.reloadData()
}
#IBAction func weekendBtn(_ sender: Any) {
for i in 0...7 {
self.selectedDay[i] = i > 5 ? true : false
}
repeatCollectionView.reloadData()
}
Method collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) is unneeded now. If you tap on any cell, it changes the colour to red.

Table view cell elements not able to click and get data

I have one table view and inside that i placed one main view. And inside that main view i placed one button.And when ever use click on my cell button. I need to get the cell title label.This is what i need. But i tried following below code. Not sure what i am missing out. It not at all calling my cell.add target line.
Code in cell for row at index:
cell.cellBtn.tag = indexPath.row
cell.cellBtn.addTarget(self, action:#selector(self.buttonPressed(_:)), for:.touchUpInside)
#objc func buttonPressed(_ sender: AnyObject) {
print("cell tap")
let button = sender as? UIButton
let cell = button?.superview?.superview as? UITableViewCell
let indexPath = tableView.indexPath(for: cell!)
let currentCell = tableView.cellForRow(at: indexPath!)! as! KMTrainingTableViewCell
print(indexPath?.row)
print(currentCell.cellTitleLabel.text)
}
I even added a breakpoint, still it not at calling my cell.addTarget line
Tried with closure too. In cell for row at index:
cell.tapCallback = {
print(indexPath.row)
}
In my table view cell:
var tapCallback: (() -> Void)?
#IBAction func CellBtndidTap(_ sender: Any) {
print("Right button is tapped")
tapCallback?()
}
Here that print statement is getting print in console.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var list = [String]()
#IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
cell.saveButton.tag = indexPath.row
//cell.saveButton.accessibilityIdentifier = "some unique identifier"
cell.tapCallback = { tag in
print(tag)
}
return cell
}
}
class MyTableViewCell: UITableViewCell {
// MARK: - IBOutlets
#IBOutlet weak var saveButton: UIButton!
// MARK: - IBActions
#IBAction func saveTapped(_ sender: UIButton) {
tapCallback?(sender.tag)
}
// MARK: - Actions
var tapCallback: ((Int) -> Void)?
}
Actually this is not a good programming practice to add the button (which contains in table view cell) target action in view controller. We should follow the protocol oriented approach for it. Please try to under stand the concept.
/*This is my cell Delegate*/
protocol InfoCellDelegate {
func showItem(item:String)
}
/*This is my cell class*/
class InfoCell: UITableViewCell {
//make weak reference to avoid the Retain Cycle
fileprivate weak var delegate: InfoCellDelegate?
//Outlet for views
#IBOutlet var showButton: UIButton?
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
//This is the public binding function which will bind the data & delegate to cell
func bind(with: DataModel?, delegate: InfoCellDelegate?, indexPath: IndexPath) {
//Now the bind the cell with data here
//.....
//Assign the delegate
self.delegate = delegate
}
//Button action
#IBAction func rowSelected(sender: UIButton) {
self.delegate?.showItem(item: "This is coming from cell")
}
}
/*Now in your ViewController you need to just confirm the InfoCellDelegate & call the bind function*/
class ListViewController: UIViewController {
//Views initialisation & other initial process
}
//Table view Delegate & Data source
extension ListViewController: UITableViewDataSource, UITableViewDelegate {
/**
Configure the table views
*/
func configureTable() {
//for item table
self.listTable.register(UINib.init(nibName: "\(InfoCell.classForCoder())", bundle: nil), forCellReuseIdentifier: "\(InfoCell.classForCoder())")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "InfoCell") as! InfoCell
cell.bind(with: DataModel, delegate: self, indexPath: indexPath)
return cell
}
}
extension ListViewController: InfoCellDelegate {
func showItem(item) {
print(item)
}
}

How to detect one button in tableview cell

How to detect one button in UITableviewCell, I have 10 UIButton in UITableViewCell, next when I click on UIButton then it detects multiple buttons, (as like odd number list). my UITableView is with paging enabled. Here is my all code.
TableView
class HomeViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
#IBOutlet weak var homeTableView: UITableView!
let mainArray = [["1","2","3","4"],["5","6","7","8"],["9","10","11","12"],["13","14","15","16"]]
override func viewDidLoad() {
super.viewDidLoad()
self.homeTableView.delegate = self
self.homeTableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return mainArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mainArray[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.view.frame.size.height
}
}
TableViewCell
class HomeTableViewCell: UITableViewCell {
#IBOutlet weak var bookMarkBtn: UIButton!
#IBAction func bookMarkBtnAction(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if(sender.isSelected == true)
{
sender.setImage(UIImage(named:"favorite_blue"), for: UIControlState.normal)
}
else
{
sender.setImage(UIImage(named:"favorite_white"), for: UIControlState.normal)
}
}
}
To detect a UIButton in a UITableViewCell, you can follow any of the below approaches:
1. Use UIButton IBOutlets
You can create an IBOutlet corresponding to each UIButton in the UITableViewCell and use those outlets to identify which button action is performed.
Example:
class CustomCell: UITableViewCell
{
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
#IBOutlet weak var button3: UIButton!
#IBOutlet weak var button4: UIButton!
#IBOutlet weak var button5: UIButton!
#IBAction func onTapButton(_ sender: UIButton)
{
if sender === button1
{
//button1 specific code here
}
else if sender === button2
{
//button2 specific code here
}
//and so on..
}
}
2. Use UIButton Tag property
You can provide a tag value to each of the UIButton present in the UITableViewCell and then use that tag to identify the specific button.
Example:
class CustomCell: UITableViewCell
{
#IBAction func onTapButton(_ sender: UIButton)
{
if sender.tag == 1
{
//button1 has a tag = 1
//button1 specific code here
}
else if sender.tag == 2
{
//button2 has a tag = 2
//button2 specific code here
}
//and so on..
}
}
Edit:
For setting different images in selected/unselected state of UIButton, you can use storyboard for that:
For Unselected state:
For Selected state:
Let me know if you still face any issues.
In your cellForRowAt method, do add tag number to the buttons
cell.bookMarkBtn.tag = indexPath.row;
Then
#IBAction func bookMarkBtnAction(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if(sender.tag == 0)
{
...
} else if (sender.tag == 1)
{
...
}
}
Create protocol
protocol HomeTableViewCellDelegate {
func bookMarkBtnTapped(btn: UIButton)
}
class HomeTableViewCell: UITableViewCell {
#IBOutlet weak var bookMarkBtn: UIButton!
//add delegate var for protocol
var delegate: HomeTableViewCellDelegate?
#IBAction func bookMarkBtnAction(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if(sender.isSelected == true)
{
sender.setImage(UIImage(named:"favorite_blue"), for: UIControlState.normal)
}
else
{
sender.setImage(UIImage(named:"favorite_white"), for: UIControlState.normal)
}
//set this which button is pressed
self.delegate?.bookMarkBtnTapped(btn: sender)
}
}
HomeViewController implement HomeTableViewCellDelegate method
class HomeViewController: HomeTableViewCellDelegate {
func bookMarkBtnTapped(btn: UIButton) {
// here btn is book mark button tapped by user from tableview cell
}
}
Use button tag for this.
In tableViewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
cell.bookMarkBtn.tag = indexPath.row
cell.bookMarkBtn.addTarget(self, action: #selector(self. bookMarkBtnAction), for: .touchUpInside)
return cell
}
#objc func bookMarkBtnAction(sender: UIButton) {
if sender.tag == 0 { //or which indexpath do you want.
//code
} else if sender.tag == 1 {
//code
}
..
}
remove #IBAction func bookMarkBtnAction(_ sender: UIButton) from tableviewcell class

iOS swift UIButton in TableView Cell

I have a tableView with custom cell. in my custom cell I have a like button. for like Button I wrote a function to change state from .normal to .selected like this:
FeedViewCell
class FeedViewCell: UITableViewCell {
#IBOutlet weak var likeButton: UIButton!
var likes : Bool {
get {
return UserDefaults.standard.bool(forKey: "likes")
}
set {
UserDefaults.standard.set(newValue, forKey: "likes")
}
}
override func awakeFromNib() {
super.awakeFromNib()
self.likeButton.setImage(UIImage(named: "like-btn-active"), for: .selected)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
#IBAction func likeBtnTouch(_ sender: AnyObject) {
print("press")
// toggle the likes state
self.likes = !self.likeButton.isSelected
// set the likes button accordingly
self.likeButton.isSelected = self.likes
}
}
FeedViewController :
class FeedViewController: UIViewController {
#IBOutlet var feedTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Register Cell Identifier
let feedNib = UINib(nibName: "FeedViewCell", bundle: nil)
self.feedTableView.register(feedNib, forCellReuseIdentifier: "FeedCell")
}
func numberOfSectionsInTableView(_ tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.feeds.count
}
func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FeedCell", for: indexPath) as! FeedViewCell
return cell
}
}
But my problem is when I tap like button in cell with indexPath.row 0 the state of button in cell with indexPath.row 3 change state too.
where is my mistake?
thanks
You didn't post all your code, but I can tell you that for this to work the #IBAction func likeBtnTouch(_ sender: AnyObject) { } definition must be inside the FeedViewCell class definition to make it unique to a particular instance of the cell.
As a rule of thumb, I normally ensure that all the UI elements inside my cell are populated in cellForRowAtIndexPath when using dequeued cells. Also it should be set from an external source. I.o.w not from a property inside the cell. Dequeuing cells reuse them, and if not setup properly, it might have some leftovers from another cell.
For example, inside cellForRowAtIndexPath:
self.likeButton.isSelected = likeData[indexPath.row]

Resources