ios 滑动行或单击编辑按钮时更改 UITableViewCell 中默认红色删除按钮的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20290766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the color of default red color delete button in UITableViewCell when swiping rows or click on edit button
提问by Hussain Shabbir
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 :
我想改变UITableViewCell
点击编辑按钮或滑动UITableView
行时减号按钮和删除按钮的颜色。到目前为止,我已经实现了这段代码:
-(IBAction)doEdit:(id)sender
{
[[self keyWordsTable] setEditing:YES animated:NO];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
回答by timothykc
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://stackoverflow.com/a/22350817/1758337)
创建一个 uitableviewcell 类(另见https://stackoverflow.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 时才会调用替代颜色,因此我们通过重新加载所有内容来确保发生这种情况。)
回答by clearlight
Swift Example (iOS 8)
Swift 示例(iOS 8)
UITableViewDelegatedocs (editActionsForRowAtIndexPathmethod)
UITableViewDelegate文档(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.
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.
返回值
表示行操作的 UITableViewRowAction 对象数组。您提供的每个操作都用于创建用户可以点击的按钮。
讨论
当您想为表格行之一提供自定义操作时,请使用此方法。当用户在一行中水平滑动时,表格视图将行内容移到一边以显示您的操作。点击其中一个动作按钮会执行与动作对象一起存储的处理程序块。
如果你没有实现这个方法,当用户滑动行时,表格视图会显示标准的附件按钮。
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) {
}
回答by Giang
fist time you call willTransitionToState in .m (customcell)
第一次在 .m 中调用 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;
}
});
}
回答by Leo Natan
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)来更改按钮的颜色以及添加其他按钮。
回答by Ofcourse
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];
}
}
}
}