I've just come across this new API on iOS8. I cannot however find any solution on if it is possible to use images instead of text and to allow left and right swipe? Is this even possible? The only implementation I've found is this:
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *moreAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:#"Button1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
// maybe show an action sheet with more options
[self.tableView setEditing:NO];
}];
moreAction.backgroundColor = [UIColor blueColor];
UITableViewRowAction *moreAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:#"Button2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
[self.tableView setEditing:NO];
}];
moreAction2.backgroundColor = [UIColor blueColor];
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:#"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
return #[deleteAction, moreAction, moreAction2];
}
No, I don't think it's possible to have a right swipe (with a UITableViewCell, anyway). However, you can use an image for the button by setting its background color using colorWithPatternImage:. The width of the button seems to be determined by the length of the title, so if you want to change the width (and not have any visible title), use a title that consists of as many spaces as you need.
in Swift it would be:
deleteAction.backgroundColor = UIColor(patternImage: UIImage(named:"sample")!)
When you are adding images to the swiped view, this answer may be helpful.
Related
I have used editActionsForRowAtIndexPath to add custom UITableViewRowAction.
I want to auto drag to show this two option(Delete & Cancel) by just to click UITableViewCell (didSelectRowAtIndexPath).
Currently dragging cell from right to left is working but how can I show this without dragging(by just clicking any button on cell),
Is there any method to show this option of a particular cell.
eg.
My Code:
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:#"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
// delete action
}];
deleteAction.backgroundColor = [UIColor redColor];
UITableViewRowAction *cancelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:#"Cancel" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
//cancel action
}];
return #[deleteAction, cancelAction];
}
Note: without using custom design.
i'm using tableview with two actions, delete and edit. when i swipe to show the action the first one returned in the array of editActionsForRowAtIndexPath: is called automatically
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:#"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
// delete ...
}];
deleteAction.backgroundColor = [UIColor redColor];
UITableViewRowAction *edit = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:#" Edit " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
// edit ...
}];
edit.backgroundColor = [UIColor colorWithRed:0x2F/255.0 green:0x83/255.0 blue:0xFB/255.0 alpha:1];
return #[deleteAction,edit];
}
here the delete action is called. I want the actions to show when swipe but not to be called without tapping.
For this you need to swipe table row slightly.Then it will show both the action options and actions will be called on tapping only.
I Want Set Movable Tableview Cell with Button Action.And how can i manage button action.
How Can i implement this stuff.
Here i have Attached image i want like this Image.
Thank you in advance
You can do it withoutUsing any third party control, using UITableViewRowAction of default UITableView..
Below is the code snippet...
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:#" Edit " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
// maybe show an action sheet with more options
// [self callBlockUser];
}];
editAction.backgroundColor = [UIColor lightGrayColor];
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:#"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
// maybe show an action sheet with more options
// [self callBlockUser];
}];
deleteAction.backgroundColor = [UIColor redColor];
return #[deleteAction,editAction];
}
I used the localization and RTl layout to change the tableview when i tested on simulator it works good but when I tested on real device it doesn't work. Why is this?
(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:#"Edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// show UIActionSheet
NSLog(#"im pressed the edit");
}];
editAction.backgroundColor = [UIColor grayColor];
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:#"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// flag the row
NSLog(#"im pressed the delete/");
}];
deleteAction.backgroundColor = [UIColor redColor];
return #[editAction, deleteAction];
}
I created an instance of UITableViewRowAction in
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
and I have a handler block when this row action is triggered:
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
You have a row action parameter that can be used so you do not create retain cycles and you have an index path. I've tried setting the backgroundColor of that new row action like this:
[action setBackgroundColor:[UIColor greenColor]];
But this did not change anything. I also tried setNeedsDisplay on the button object of the row action, but without any luck.
Any idea how I can change the backgroundColor while the row actions are still visible? Thanks
xCoder
Here is my implementation of the same and it works well. It should solve your problem as well..
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *one = [UITableViewRowAction rowActionWithStyle:0 title:#"one" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(#"my first action");
}];
UITableViewRowAction *two = [UITableViewRowAction rowActionWithStyle:1 title:#"two" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(#"my second action");
}];
UITableViewRowAction *three = [UITableViewRowAction rowActionWithStyle:1 title:#"three" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(#"my third action");
}];
one.backgroundColor = [UIColor purpleColor];
two.backgroundColor = [UIColor greenColor];
three.backgroundColor = [UIColor brownColor];
return #[one, two, three];
}