在滑动行或单击编辑按钮时,更改UITableViewCell中默认红色删除按钮的颜色 [英] Change the color of default red color delete button in UITableViewCell when swiping rows or click on edit button
问题描述
我想在点击编辑按钮或刷卡 UITableView
UITableViewCell 的按钮>行。到目前为止,我已经实现了这个代码:
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 :
-(IBAction)doEdit:(id)sender
{
[[self keyWordsTable] setEditing:YES animated:NO];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
推荐答案
iOS 8和9 (此帖子的道具)
注意:如果您正在使用现有的iOS 7项目,则需要将目标更新为iOS 8获得此功能。还记得设置UITableviewDelegate。
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.
现在所有的魔法都在这里发生(你想要的按钮也很多!!!!):
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)
**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.");
}
设置自定义文字标签而不是删除。
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"Your Label";
}
为按钮第1部分设置自定义颜色 - 警告,这在技术上涉及寻找私人苹果API。但是,您不能使用属于UIKIT的公共方法搜索来修改子视图。
创建一个uitableviewcell类(另请参阅 https://stackoverflow.com/a/22350817/1758337 )
Create a uitableviewcell class (see also 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];
}
}
}
}
另一个注意:无法保证此方法在将来的更新中有效。还要注意提及或使用私有 UITableViewCellDeleteConfirmationView
类可能导致AppStore拒绝。
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.
设置自定义颜色按钮第2部分
返回你的uitableviewcontroller
back in your uitableviewcontroller
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
[YourTableView reloadData];
}
(直到下次调用layoutSubviews时才会调用备用颜色在tablecell上,所以我们通过重新加载所有内容来确保这一点。)
(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.)
这篇关于在滑动行或单击编辑按钮时,更改UITableViewCell中默认红色删除按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!