I wanted to change the color of minus button and delete button of UITableViewCell
when click on edit button or swiping UITableView
rows. I have implemented this code so far :
我想在单击编辑按钮或滑动UITableView行时更改减号按钮的颜色并删除UITableViewCell的按钮。到目前为止,我已实现此代码:
-(IBAction)doEdit:(id)sender
{
[[self keyWordsTable] setEditing:YES animated:NO];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
5 个解决方案
#1
93
iOS 8 and 9 (props to this post)
iOS 8和9(这篇文章的道具)
Note: If you are working with an existing iOS 7 project, you'll need to update the target to iOS 8 to get this functionality. Also remember to set the UITableviewDelegate.
注意:如果您正在使用现有的iOS 7项目,则需要将目标更新到iOS 8才能获得此功能。还记得设置UITableviewDelegate。
All the magic now happens here (as many buttons as you want too!!!!):
所有的魔法现在都在这里发生(你想要的按钮也很多!!!!):
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@"Action to perform with Button 1");
}];
button.backgroundColor = [UIColor greenColor]; //arbitrary color
UITableViewRowAction *button2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@"Action to perform with Button2!");
}];
button2.backgroundColor = [UIColor blueColor]; //arbitrary color
return @[button, button2];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// you need to implement this method too or nothing will work:
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
(iOS 7)
(IOS 7)
**activate the delete button on swipe**
// make sure you have the following methods in the uitableviewcontroller
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"You hit the delete button.");
}
set custom text label instead of delete.
设置自定义文本标签而不是删除。
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"Your Label";
}
set custom color for button part 1 - warning, this technically involves poking at the private apple API. However, you are not prevented from modifying a subview using a public method search that is part of UIKIT.
设置按钮第1部分的自定义颜色 - 警告,这在技术上涉及戳私人苹果API。但是,不会阻止使用属于UIKIT的公共方法搜索来修改子视图。
Create a uitableviewcell class (see also https://*.com/a/22350817/1758337 )
创建一个uitableviewcell类(另请参阅https://*.com/a/22350817/1758337)
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView *subview in self.subviews) {
//iterate through subviews until you find the right one...
for(UIView *subview2 in subview.subviews){
if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
//your color
((UIView*)[subview2.subviews firstObject]).backgroundColor=[UIColor blueColor];
}
}
}
}
Another note: there's no guarantee this approach will work in future updates. Also beware that mentioning or using the private UITableViewCellDeleteConfirmationView
class may lead to AppStore rejection.
另一个注意事项:无法保证此方法在将来的更新中有效。还要注意,提及或使用私有UITableViewCellDeleteConfirmationView类可能会导致AppStore拒绝。
set custom color for button part 2
为按钮部分2设置自定义颜色
back in your uitableviewcontroller
回到你的uitableviewcontroller
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
[YourTableView reloadData];
}
(The alternate color won't be called until the next time layoutSubviews is called on the tablecell, so we ensure this happens by reloading everything.)
(直到下一次在tablecell上调用layoutSubviews时才会调用备用颜色,因此我们通过重新加载所有内容来确保这种情况发生。)
#2
21
Swift Example (iOS 8)
UITableViewDelegate docs (editActionsForRowAtIndexPath method)
UITableViewDelegate docs(editActionsForRowAtIndexPath方法)
Return Value
回报价值
An array of UITableViewRowAction objects representing the actions for the row. Each action you provide is used to create a button that the user can tap.
一组UITableViewRowAction对象,表示行的操作。您提供的每个操作都用于创建用户可以点按的按钮。
Discussion
讨论
Use this method when you want to provide custom actions for one of your table rows. When the user swipes horizontally in a row, the table view moves the row content aside to reveal your actions. Tapping one of the action buttons executes the handler block stored with the action object.
如果要为其中一个表行提供自定义操作,请使用此方法。当用户在一行中水平滑动时,表格视图会将行内容移到一边以显示您的操作。点击其中一个操作按钮可执行与操作对象一起存储的处理程序块。
If you do not implement this method, the table view displays the standard accessory buttons when the user swipes the row.
如果未实现此方法,则表视图会在用户滑动行时显示标准附件按钮。
Working example in Swift:
Swift中的工作示例:
@available(iOS 8.0, *)
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let button1 = UITableViewRowAction(style: .Default, title: "Happy!") { action, indexPath in
print("button1 pressed!")
}
button1.backgroundColor = UIColor.blueColor()
let button2 = UITableViewRowAction(style: .Default, title: "Exuberant!") { action, indexPath in
print("button2 pressed!")
}
button2.backgroundColor = UIColor.redColor()
return [button1, button2]
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
#3
3
fist time you call willTransitionToState in .m (customcell)
第一次在.m(customcell)中调用willTransitionToState
- (void)willTransitionToState:(UITableViewCellStateMask)state{
NSLog(@"EventTableCell willTransitionToState");
[super willTransitionToState:state];
[self overrideConfirmationButtonColor];
}
Check version iOS, it's here, i'm using iOS 7 - iOS8
检查iOS版本,就在这里,我使用的是iOS 7 - iOS8
//at least iOS 8 code here
- (UIView*)recursivelyFindConfirmationButtonInView:(UIView*)view
{
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) {
// iOS 8+ code here
for(UIView *subview in view.subviews) {
if([NSStringFromClass([subview class]) rangeOfString:@"UITableViewCellActionButton"].location != NSNotFound)
return subview;
UIView *recursiveResult = [self recursivelyFindConfirmationButtonInView:subview];
if(recursiveResult)
return recursiveResult;
}
}
else{
// Pre iOS 8 code here
for(UIView *subview in view.subviews) {
if([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"]) return subview;
UIView *recursiveResult = [self recursivelyFindConfirmationButtonInView:subview];
if(recursiveResult) return recursiveResult;
}
}
return nil;
}
-(void)overrideConfirmationButtonColor
{
dispatch_async(dispatch_get_main_queue(), ^{
UIView *confirmationButton = [self recursivelyFindConfirmationButtonInView:self];
if(confirmationButton)
{
UIColor *color = UIColorFromRGB(0xFF7373);
confirmationButton.backgroundColor = color;
}
});
}
#4
2
Not possible using public API.
使用公共API是不可能的。
For the delete button, you can use a custom implementation, such as SWTableViewCell, to change the color of the button, as well as add others.
对于删除按钮,您可以使用自定义实现(如SWTableViewCell)来更改按钮的颜色,以及添加其他按钮。
#5
0
Old Question, but am sure there are some people who support iOS 7. To change the delete button background colour, you need to create "UITableViewCell" class or extend it. then you can use
旧问题,但我确定有些人支持iOS 7.要更改删除按钮背景颜色,您需要创建“UITableViewCell”类或扩展它。那你可以用
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView *subview in self.subviews) {
for(UIView *childView in subview.subviews){
if ([childView isKindOfClass:[UIButton class]]) {
childView.backgroundColor = [UIColor blueColor];
}
}
}
}
#1
93
iOS 8 and 9 (props to this post)
iOS 8和9(这篇文章的道具)
Note: If you are working with an existing iOS 7 project, you'll need to update the target to iOS 8 to get this functionality. Also remember to set the UITableviewDelegate.
注意:如果您正在使用现有的iOS 7项目,则需要将目标更新到iOS 8才能获得此功能。还记得设置UITableviewDelegate。
All the magic now happens here (as many buttons as you want too!!!!):
所有的魔法现在都在这里发生(你想要的按钮也很多!!!!):
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@"Action to perform with Button 1");
}];
button.backgroundColor = [UIColor greenColor]; //arbitrary color
UITableViewRowAction *button2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@"Action to perform with Button2!");
}];
button2.backgroundColor = [UIColor blueColor]; //arbitrary color
return @[button, button2];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// you need to implement this method too or nothing will work:
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
(iOS 7)
(IOS 7)
**activate the delete button on swipe**
// make sure you have the following methods in the uitableviewcontroller
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"You hit the delete button.");
}
set custom text label instead of delete.
设置自定义文本标签而不是删除。
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"Your Label";
}
set custom color for button part 1 - warning, this technically involves poking at the private apple API. However, you are not prevented from modifying a subview using a public method search that is part of UIKIT.
设置按钮第1部分的自定义颜色 - 警告,这在技术上涉及戳私人苹果API。但是,不会阻止使用属于UIKIT的公共方法搜索来修改子视图。
Create a uitableviewcell class (see also https://*.com/a/22350817/1758337 )
创建一个uitableviewcell类(另请参阅https://*.com/a/22350817/1758337)
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView *subview in self.subviews) {
//iterate through subviews until you find the right one...
for(UIView *subview2 in subview.subviews){
if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
//your color
((UIView*)[subview2.subviews firstObject]).backgroundColor=[UIColor blueColor];
}
}
}
}
Another note: there's no guarantee this approach will work in future updates. Also beware that mentioning or using the private UITableViewCellDeleteConfirmationView
class may lead to AppStore rejection.
另一个注意事项:无法保证此方法在将来的更新中有效。还要注意,提及或使用私有UITableViewCellDeleteConfirmationView类可能会导致AppStore拒绝。
set custom color for button part 2
为按钮部分2设置自定义颜色
back in your uitableviewcontroller
回到你的uitableviewcontroller
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
[YourTableView reloadData];
}
(The alternate color won't be called until the next time layoutSubviews is called on the tablecell, so we ensure this happens by reloading everything.)
(直到下一次在tablecell上调用layoutSubviews时才会调用备用颜色,因此我们通过重新加载所有内容来确保这种情况发生。)
#2
21
Swift Example (iOS 8)
UITableViewDelegate docs (editActionsForRowAtIndexPath method)
UITableViewDelegate docs(editActionsForRowAtIndexPath方法)
Return Value
回报价值
An array of UITableViewRowAction objects representing the actions for the row. Each action you provide is used to create a button that the user can tap.
一组UITableViewRowAction对象,表示行的操作。您提供的每个操作都用于创建用户可以点按的按钮。
Discussion
讨论
Use this method when you want to provide custom actions for one of your table rows. When the user swipes horizontally in a row, the table view moves the row content aside to reveal your actions. Tapping one of the action buttons executes the handler block stored with the action object.
如果要为其中一个表行提供自定义操作,请使用此方法。当用户在一行中水平滑动时,表格视图会将行内容移到一边以显示您的操作。点击其中一个操作按钮可执行与操作对象一起存储的处理程序块。
If you do not implement this method, the table view displays the standard accessory buttons when the user swipes the row.
如果未实现此方法,则表视图会在用户滑动行时显示标准附件按钮。
Working example in Swift:
Swift中的工作示例:
@available(iOS 8.0, *)
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let button1 = UITableViewRowAction(style: .Default, title: "Happy!") { action, indexPath in
print("button1 pressed!")
}
button1.backgroundColor = UIColor.blueColor()
let button2 = UITableViewRowAction(style: .Default, title: "Exuberant!") { action, indexPath in
print("button2 pressed!")
}
button2.backgroundColor = UIColor.redColor()
return [button1, button2]
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
#3
3
fist time you call willTransitionToState in .m (customcell)
第一次在.m(customcell)中调用willTransitionToState
- (void)willTransitionToState:(UITableViewCellStateMask)state{
NSLog(@"EventTableCell willTransitionToState");
[super willTransitionToState:state];
[self overrideConfirmationButtonColor];
}
Check version iOS, it's here, i'm using iOS 7 - iOS8
检查iOS版本,就在这里,我使用的是iOS 7 - iOS8
//at least iOS 8 code here
- (UIView*)recursivelyFindConfirmationButtonInView:(UIView*)view
{
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) {
// iOS 8+ code here
for(UIView *subview in view.subviews) {
if([NSStringFromClass([subview class]) rangeOfString:@"UITableViewCellActionButton"].location != NSNotFound)
return subview;
UIView *recursiveResult = [self recursivelyFindConfirmationButtonInView:subview];
if(recursiveResult)
return recursiveResult;
}
}
else{
// Pre iOS 8 code here
for(UIView *subview in view.subviews) {
if([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"]) return subview;
UIView *recursiveResult = [self recursivelyFindConfirmationButtonInView:subview];
if(recursiveResult) return recursiveResult;
}
}
return nil;
}
-(void)overrideConfirmationButtonColor
{
dispatch_async(dispatch_get_main_queue(), ^{
UIView *confirmationButton = [self recursivelyFindConfirmationButtonInView:self];
if(confirmationButton)
{
UIColor *color = UIColorFromRGB(0xFF7373);
confirmationButton.backgroundColor = color;
}
});
}
#4
2
Not possible using public API.
使用公共API是不可能的。
For the delete button, you can use a custom implementation, such as SWTableViewCell, to change the color of the button, as well as add others.
对于删除按钮,您可以使用自定义实现(如SWTableViewCell)来更改按钮的颜色,以及添加其他按钮。
#5
0
Old Question, but am sure there are some people who support iOS 7. To change the delete button background colour, you need to create "UITableViewCell" class or extend it. then you can use
旧问题,但我确定有些人支持iOS 7.要更改删除按钮背景颜色,您需要创建“UITableViewCell”类或扩展它。那你可以用
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView *subview in self.subviews) {
for(UIView *childView in subview.subviews){
if ([childView isKindOfClass:[UIButton class]]) {
childView.backgroundColor = [UIColor blueColor];
}
}
}
}