Custom Table View Row Action (Image)

Custom table view row action (image)

naturally a pattern will be repeated to fill all space.

The only way I could imaging is to increase height of the image to exceed the most probable maximum cell height.


I used this code and an image with transparent background and height of 120px in which the icon occupies the first 44x44 px

import UIKit

class ViewController: UITableViewController {
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 30
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = "\(indexPath.row)"
return cell
}

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {

let moreClosure = { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
println("More closure called")
}

let moreAction = UITableViewRowAction(style: .Normal, title: " ", handler: moreClosure)

if let image = UIImage(named: "star.png"){
moreAction.backgroundColor = UIColor(patternImage: image)

}
return [moreAction]
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return min((44.0 * CGFloat(indexPath.row) + 1), 120.0)
}
}

result:

Sample Image

Sample Image

With an image that has a opaque background it looks better.

Sample Image


visit GitHub for an example project.

How to add image in UITableViewRowAction?

Finally in iOS 11, SWIFT 4 We can add add image in UITableView's swipe action with help of UISwipeActionsConfiguration

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

let action = UIContextualAction(style: .normal, title: "Files", handler: { (action,view,completionHandler ) in
//do stuff
completionHandler(true)
})
action.image = UIImage(named: "apple.png")
action.backgroundColor = .red
let configuration = UISwipeActionsConfiguration(actions: [action])

return configuration
}

Sample Image

WWDC video at 28.34

Apple Doc

Note: I have used 50*50 points apple.png image with 50 tableview row height

UITableViewRowAction image for title

iOS 11.0

Swift

Apple introduced flexible way to declare row actions with great benefits.

extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let askAction = UIContextualAction(style: .normal, title: nil) { action, view, complete in
print("Ask!")
complete(true)
}

// here set your image and background color
askAction.image = IMAGE
askAction.backgroundColor = .darkGray

let blockAction = UIContextualAction(style: .destructive, title: "Block") { action, view, complete in
print("Block")
complete(true)
}

return UISwipeActionsConfiguration(actions: [blockAction, askAction])
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.textLabel?.text = "row: \(indexPath.row)"
}
}

Example:

Sample Image

iOS 8.0

You need to set UIImage to backgroundColor of row action, concretely by:

Swift:

UIColor(patternImage: UIImage(named: "IMAGE_NAME"))

Objective-C:

[UIColor colorWithPatternImage:[UIImage imageNamed:@"IMAGE_NAME"]];

Swift - enlarge custom table row image by selecting row

Replace this

let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! myCell

with

let cell = tableView.cellForRow(at:indexPath) as! myCell

as tableView.dequeueReusableCell is supposed to return another cell for usage not your clicked cell , you should only use it inside cellForRowAt

How to change image of imageview in tableview custom cell with pagination.

if you need to toggle image and after scrolling also that should be in last toggle state means you need to use an array to store index position and toggle state by comparing index position and scroll state inside cellfoeRowAtIndex you can get the last toggle state that is one of the possible way to retain the last toggle index even when you scroll tableview otherwise you will lost your last toggle position

 if self.toggleStatusArray[indexPath.row]["toggle"] as! String == "on"{
cell.favouriteButton.setImage(#imageLiteral(resourceName: "FavSelectedBtnTabBar"), for: .normal)
} else {
cell.favouriteButton.setImage(#imageLiteral(resourceName: "UnselectedFavIcon"), for: .normal)
}

cell.favouriteButtonHandler = {()-> Void in

if self.toggleStatusArray[indexPath.row]["toggle"] as! String == "on"{
//Assign Off status to particular index position in toggleStatusArray

} else {

//Assign on status to particular index position in toggleStatusArray
}

}

Hope this will help you

How To Upload Image Using Custom UITableView

Use the didSelectRow method and you already have the method you need to call in your code. Here's how:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) -> Int {
if indexPath.row == 0 {
showActionSheet(title: "Your title")
}
}


Related Topics



Leave a reply



Submit