How to change UITableViewRowAction title color?
I'm afraid that there's no way to change the title color of the UITableViewRowAction.
The only things you can change on the action are:
- backgroundColor
- style (destructive (red backgroundcolor, ...)
- title
For more info, please refer to the Apple Doc UITableViewRowAction
Change the color of 'UITableViewRowAction'
You can use the backgroundColor
property of the UITableViewRowAction
. For example, rateAction.backgroundColor = UIColor.blueColor()
.
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Aceitar" , handler: {(action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
// Put your shareAction handler stuff here
})
var rateAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Recusar" , handler: {(action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
// Put your rateAction handler stuff here
})
shareAction.backgroundColor = UIColor.redColor()
rateAction.backgroundColor = UIColor.blueColor()
return [shareAction, rateAction]
}
How do I change font color for a specific cell from UITableViewRowAction?
I'll assume you implemented tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
, then you can do this inside your action:
let action = UITableViewRowAction(style: .normal, title: "Action") { (action, indexPath) in
let cell = tableView.cellForRow(at: indexPath)
cell?.textLabel?.textColor = .red
tableView.setEditing(false, animated: true)
}
return [action]
If you've subclassed UITableViewCell
, then you'll have to do
let cell = tableView.cellForRow(at: indexPath) as! MySubclassTableViewCell
and edit the appropiate label inside your class.
Alternatively, you could store the indexPath outside of the closure and reload the tableView.
How to change the title for UITableViewRowAction
Here's a quick and dirty example.
Say you have a class Restaurant with a name and likes value:
class Restaurant {
var name: String?
var likes: Int = 0
}
You initialize a bunch of Restaurant
objects, and put them in an array called dataSource
. Your table view data source methods will look like this:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataSource.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .Default, reuseIdentifier: "cell");
cell.textLabel?.text = dataSource[indexPath.row].name
return cell
}
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// This can be empty if you're not deleting any rows from the table with your edit actions
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
// First, create a share action with the number of likes
let shareAction = UITableViewRowAction(style: .Default, title: "\(self.dataSource[indexPath.row].likes)") { (action, indexPath) -> Void in
// In your closure, increment the number of likes for the restaurant, and slide the cell back over
self.dataSource[indexPath.row].likes++
self.tableView.setEditing(false, animated: true)
}
return [shareAction] // return your array of edit actions for your cell. In this case, we're only returning one action per row.
}
I'm not going to write a scrollable cell from scratch, since this question has a bunch of options you can use.
I was, however, intrigued by Andrew Carter's attempt to iterate through subviews to access the UIButton
in the edit action directly. Here is my attempt:
First, create a reference to the UITableViewCell
(or an array of cells), you wish to modify, for this example, I'll be using a single cell:
var cellRef: UITableViewCell?
// ...
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .Default, reuseIdentifier: "cell");
cell.textLabel?.text = dataSource[indexPath.row].name
cellRef = cell;
return cell
}
In your share action, iterate through the button's subviews. We're looking for the UITableViewCellDeleteConfirmationView
and _UITableViewCellActionButton
objects (private headers linked for reference).
let shareAction = UITableViewRowAction(style: .Default, title: "\(self.dataSource[indexPath.row].likes)") { (action, indexPath) -> Void in
var deleteConfirmationView: UIView? // UITableViewCellDeleteConfirmationView
if let subviews = self.cellRef?.subviews {
for subview in subviews {
if NSClassFromString("UITableViewCellDeleteConfirmationView") != nil {
if subview.isKindOfClass(NSClassFromString("UITableViewCellDeleteConfirmationView")!) {
deleteConfirmationView = subview
break
}
}
}
}
if let unwrappedDeleteView = deleteConfirmationView {
if unwrappedDeleteView.respondsToSelector("_actionButtons") {
let actionbuttons = unwrappedDeleteView.valueForKey("_actionButtons") as? [AnyObject]
if let actionButton = actionbuttons?.first as? UIButton { // _UITableViewCellActionButton
actionButton.setTitle("newText", forState: .Normal)
}
}
}
}
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:
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"]];
Related Topics
How to Allow User to Pick the Image with Swift
Draw a Circle of 1000M Radius Around Users Location in Mkmapview
Update Label from Background Timer
Restricting App Installations from Appstore Only to Users with iPhone 5/5S/5C
Custom Uitableviewcell Programmatically Using Swift
Presenting Camera Permission Dialog in iOS 8
Save and Load from Keychain | Swift
App Not Sized Properly iOS 8 iPhone Simulator
What's a Redirect URI? How Does It Apply to iOS App for Oauth2.0
How to Use a Custom Font with Dynamic Text Sizes in iOS7
Check If Uicolor Is Dark or Bright
What's the Uiscrollview Contentinset Property For
Pull to Refresh Uitableview Without Uitableviewcontroller
iOS Protocol/Delegate Confusion