How do I make custom action in my tableView cell? - ios

I'm making a custom action which perform an animation of a GitHub repo.
Here is my code of VC which has the TableView:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
var done1=UITableViewRowAction(style: .normal, title: "done", handler: UITableViewRowAction!, indexPath: indexPath){
CustomCell.makeanimate(CustomCell)
}
return(done1)
}
and here is the code of CustomClass :
func makeanimate() {
self.checkTest.setOn(true, animated: true)
}
I want to call makeanimate when I tap done1, but I get an error saying use of instance member 'makeanimate' on type 'customCell'

I believe makeanimate is an instance method, but you are calling it as if it was a class method in your code. You need to get a cell instance and then call makeanimate via that cell.

Related

how to call manually cellforrowat of tableview manually in swift?

I am trying to call (cellForRowAt indexPath: IndexPath)
method manually from a button how can i accomplish that for the record i already have tried reload data method on the UITableView.
It did not work.
self.myTableView.reloadData()
Why i need to do that is because i am trying to implement a solution from stackoverflow the solution looks like this:
func configureVisibleCells(for tableView: UITableView?, animated: Bool) {
self.tableView(tableView, configureRowsAtIndexPaths: tableView?.indexPathsForVisibleRows, animated: animated)
}
func tableView(_ tableView: UITableView?, configureRowsAtIndexPaths indexPaths: [Any]?, animated: Bool) {
for indexPath in indexPaths as? [IndexPath] ?? [] {
let cell: UITableViewCell? = tableView?.cellForRow(at: indexPath)
if cell != nil {
self.tableView(tableView, configureCell: cell, forRowAt: indexPath, animated: animated)
}
}
}
func tableView(_ tableView: UITableView?, configureCell cell: UITableViewCell?, forRowAt indexPath: IndexPath?, animated: Bool) {
// Cell configuration
}
"Configure the Cell in the
tableView(_ tableView: UITableView?, configureCell cell: UITableViewCell?, forRowAt indexPath: IndexPath?, animated: Bool) method and call this method in your tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell .
and when you need to reload visible cells call the method
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell"
Now i need to call it as the guy who posted the solution said so.
Do not call cellForRowAtIndexPath manually. That is to be called only by the system otherwise it will result in undefined behaviour. Instead what you can do is call a function that will in turn will call cellForRowAtIndexPath internally. This is because before calling cellForRowAtIndexPath, system needs to call other methods to be able to layout the cell properly.
For example: number of rows, sections, height, etc.
You can use reloadRows(at:with:) function of UITableView to reload the rows of visible cells.
Have a look at this: documentation.

UITableView Swipe to Delete Button Not Showing Up

I have done this hundreds of time but currently, I have no idea why the Delete button is not showing up when I swipe from right to left.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
}
}
I have implemented the commit tableView delegate which makes the delete button appears automatically. But for some reason it does not do anything. Delete button never shows up.
Every time I swipe the segue gets fired and it takes me to a separate screen.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showOrderDetails" {
guard let indexPath = tableView.indexPathForSelectedRow else {
return
}
let order = self.orders[indexPath.row]
let placeOrderTVC = segue.destination as! PlaceOrderTableViewController
placeOrderTVC.order = order
}
}
I've had this before, you need to implement the editActionsForRowAt delegate method. It seems sometimes this is required and other times it isn't.
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let result = UITableViewRowAction(style: .destructive, title: "Delete") {
(_, indexPath) in
//Delete from your data structure here
}
return [result]
}
The canEditRowAt datasource method is defaulted to true so I've never been required to implement that method.
Try this code, you need to write canEditRowAt delegate also
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == UITableViewCellEditingStyle.delete) {
// handle delete
}
}

Change the text of the cell delete button

i'm trying to change the delete button of a cell.
I have 2 functions :
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let titleBtn = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Supprimer") { (action , indexPath ) -> Void in
self.isEditing = false
//ackAction.backgroundColor = UIColor.orange
}
return [titleBtn]
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
But, when I run my app, the text of the button is changed but the delete doesn't work (i can't delete data from my array and the row of my tableview). Before to add this functions all worked perfectly.
A detail: in the canEditRowAt function, I tried to return false too...
Thanks by advance
if you want to change the text of the delete button, conform this method in the UITableViewDelegate:
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?
{
return "Your new title"
}
To delete the item from the array conform this method
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if (editingStyle == UITableViewCellEditingStyle.delete)
{
yourDataSourceArray.remove(at: indexPath.row)
yourTableView.reloadData()
}
}
You can either use default delete action and change its title like this.
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?
{
return "MyAction"
}
or you can create your own action buttons like this.
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .default, title: "Delete") {action in
//handle delete
}
let editAction = UITableViewRowAction(style: .normal, title: "Edit") {action in
//handle edit
}
return [deleteAction, editAction]
}

tableView's commit method stops being called if editActionsForRowAt is present

My tableView's commit method gets called fine when the user swipes and deletes a row, but if I attempt to customize the color/text of the action by using editActionsForRowAt then it stops being called.
Here's some code
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
return true
}
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle
{
return .delete
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
// delete code goes here
}
With the above code then tableView:commit will get called. But if I attempt to change the background color and text by adding the method below then it stops getting called.
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
{
let delete = UITableViewRowAction(style: .destructive, title: "Remove") { (action, indexPath) in
}
delete.backgroundColor = UIColor.lightGray
return [delete]
}
Why does adding the editActionsForRowAt method stop the commit method from getting called?
When using editActionsForRowAt the commit function won't be called. Instead of putting your code in the commit function, put it in the closure on the UITableViewRowAction:
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
{
let delete = UITableViewRowAction(style: .destructive, title: "Remove") { (action, indexPath) in
// delete code should go here instead
}
delete.backgroundColor = UIColor.lightGray
return [delete]
}

Swift Trying to link a tap event on a table view cell to a controller method

I am new to swift and havne't programmed in objective C. So i'm new :-)
trying to do something fairly simple. Link a table view cell click to call a method in the controller like so
http://prntscr.com/4m9kf7
Edit:
i have these methods in my MasterController
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
I have added breakpoints in all 4 of them.
and when clicking on the cell it doesn't stop in either of them.
See Screen shot:
http://prntscr.com/4m9ql0
You need to implement the didSelectRowAtIndexPath delegate method and put your handling code inside it.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//CODE TO BE RUN ON CELL TOUCH
}
For Swift 4:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("row selected: \(indexPath.row)")
}

Resources