I have created a button which I have assigned to my accessoryView. I get to see the created button, but when I press on it, nothing happens. Can anyone see what I'm doing wrong?
let accessoryButton = UIButton(type: .roundedRect)
accessoryButton.setTitle("message", for: .normal)
accessoryButton.sizeToFit()
accessoryButton.addTarget(self, action: #selector(goToMessaging(sender:)), for: UIControlEvents.touchUpInside)
cell.accessoryView = accessoryButton as UIView
#objc func goToMessaging(sender: UIButton){
print("message him")
}
try this
#objc func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//let cell = ...
cell.accessoryType = UITableViewCellAccessoryType.detailButton
cell.tintColor = .red
}
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
print("accessory Button Tapped = \(indexPath.row)")
}
Could it be because you are casting the button to a UIView?
accessoryButton as UIView
You could try adding a tap gesture recognizer to the accessoryView if it has to be a UIView.
Related
I have a custom cell FeedCell.swift and the controller FeedViewController.swift. And I set a btn in each cell so that I can navigate to detailController.swift. But in custom cell I can't use self.navigation. What should I do?
You can set an outlet of Button in cell and addTarget it like :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! yourCell
cell.yourButton.addTarget(self, action: #selector(goDetail), for: .touchUpInside)
return cell
}
#objc func goDetail(){
let vc = // Declare your viewController
self.navigationController?.pushViewController(vc, animated:true)
}
Btw you can add this action when a cell selected instead of using button
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let vc = // Declare your viewController
self.navigationController?.pushViewController(vc, animated:true)
}
If you want to send which cell's button clicked just add cell.yourButton.tag = indexPath.row in cellForRowAt and modify goDetail with
#objc func goDetail(sender : UIButton){
sender.tag // while give your selected cell index
}
I want to delete specific row when i tap a button in that cell. but here rows are deleting on the index base. if i tap button in fifth row button but 0th index row is deleting.
please help me.
here is my code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EmployeeTableViewCell
//cell.empImage.image = UIImage(named: countryArray[indexPath.row])
cell.empName.text = namesArray[indexPath.row]
cell.cellExpendButton.tag = indexPath.row
cell.cellExpendButton.addTarget(self, action: #selector(expandcollapseButtonClicked), for: .touchUpInside)
cell.removeRowButton.addTarget(self, action: #selector(removerowButtonClicked), for: .touchUpInside)
return cell
}
#objc func removerowButtonClicked(sender : UIButton!) {
namesArray.remove(at: sender.tag)
self.tableView.reloadData()
}
I want to remove cell in which button i tapped.
Set removeRowButton.tag by indexPath.row, now you are setting cell.cellExpendButton.tag = indexPath.row, by default tag of any component is 0
cell.removeRowButton.tag = indexPath.row
I'm trying to make two different buttons for each cell that I create in my table view. One of the buttons is a + button that will increment a label. In my testing however I cannot get the function to work. My current error says
Argument of #selector does not refer to an '#objc' method, property, or initializer
I feel like I'm implementing the .addTarget method completely wrong but I am new. Here is my code:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = items[indexPath.section]
let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell") as! AddItemCell
cell.setCell(item: item)
let itemAmount = cell.itemAmount as UILabel?
cell.addButton.addTarget(self, action: #selector(addItem(sender:cell.addButton,forLabel:itemAmount!)), for: .touchUpInside)
}
#objc func addItem(sender: UIButton, forLabel label:UILabel) {
print("Add Button Clicked")
}
You are using selector syntax incorrectly:
action: #selector(addItem(sender:cell.addButton,forLabel:itemAmount!))
Just say:
action: #selector(addItem)
Then, however, you will face a new problem. You think that somehow you can cause this button to call something called addItem(sender:forLabel:). You can't. Change the declaration of addItem to addItem(_ sender:UIButton). That is the only kind of function a button tap can call.
You will thus have the sender (the button), but you must figure out from there what the label is. (And this should be easy, because, knowing the button, you know the cell, and knowing the cell, you know the label.) You cannot pass the label as a parameter in response to the button tap — but you don't need to.
You need to create callback function in you cell
class AddItemCell: UITableViewCell {
var buttonClickCallback:(() -> Void)?
#IBAction func onButtonClick(_ sender:Any) {
buttonClickCallback?()
}
}
and assign buttonClickCallback in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell method
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = items[indexPath.section]
let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell") as! AddItemCell
cell.setCell(item: item)
let itemAmount = cell.itemAmount as UILabel?
cell.buttonClickCallback = {
self.addItem(sender:cell.addButton,forLabel:itemAmount!)
}
}
I have the button working correctly, I just can't figure out how to disable it on tap. I'm not sure if I can reference it from the addSomething(sender: UIButton) function like I reference the sender.tag.
Any idea? Thanks for any help.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ExploreCell
// Configure the cell...
myCell.configureCell(teams[indexPath.row])
myCell.addSomethingButton.tag = indexPath.row
myCell.addSomethingButton.addTarget(self, action: #selector(self.addSomething), forControlEvents: .TouchUpInside)
myCell.addSomethingButton.enabled = true
//disable cell clicking
myCell.selectionStyle = UITableViewCellSelectionStyle.None
return myCell
}
What do you need to do is to store all tapped buttons in an array to check whether the button of this tag (current indexPath.row) has been tapped:
class ViewController: UIViewController {
var tappedButtonsTags = [Int]()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ExploreCell
// Configure the cell...
myCell.configureCell(teams[indexPath.row])
myCell.addSomethingButton.tag = indexPath.row
// here is the check:
if tappedButtonsTags.contains(indexPath.row) {
myCell.addSomethingButton.enabled = false
} else {
myCell.addSomethingButton.addTarget(self, action: #selector(self.addSomething), forControlEvents: .TouchUpInside)
myCell.addSomethingButton.enabled = true
}
//disable cell clicking
myCell.selectionStyle = UITableViewCellSelectionStyle.None
return myCell
}
// I just Implemented this for demonstration purposes, you can merge this one with yours :)
func addSomething(button: UIButton) {
tappedButtonsTags.append(button.tag)
tableView.reloadData()
// ...
}
}
I Hope this helped.
I have a tableview that contains a button in the left of cell. the run works well and when i select a button a made it change its image background. The problem is when I select a buttonButtonOne selection , it select other buttons when I scroll tableview down, Other buttons selected my code is this way:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableview.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PlayerCell
cell.BtnAdd.tag = indexPath.row
cell.BtnAdd.addTarget(self, action: #selector(CreateTeamTwo.PlayerAdded(_:)), forControlEvents: .TouchUpInside)
return cell
}
func PlayerAdded(sender: UIButton){
// let buttonTag = sender.tag
sender.setImage(UIImage(named: "btn_buy_minus.png"), forState: UIControlState.Normal)
}
can you help me please ?
Take one Array to store selected indexPaths Buttons tags. and in selector methods store all the sender tags and reload the tableView. and in cellForRow method check if current indexpath is contain in selected array then show minus image otherwise show plus image.
var selected = [Int]()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableview.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PlayerCell
cell.BtnAdd.tag = indexPath.row
cell.BtnAdd.addTarget(self, action: #selector(CreateTeamTwo.PlayerAdded(_:)), forControlEvents: .TouchUpInside)
if selected.contains(indexPath.row) {
cell.btn.setImage(UIImage(named: "btn_buy_minus.png"), forState: .Normal)
} else {
// here set the plus image
cell.btn.setImage(UIImage(named: "btn_buy_plus.png"), forState: .Normal)
}
return cell
}
func PlayerAdded(sender: UIButton){
selected.append(sender.tag)
self.tableView.reloadData()
}