我已经使用XIB添加了一个表视图,默认情况下,我在自定义表格视图单元格上滑动后会收到删除文本,我们可以使用方法更改该文本标题。我不想使用“SWTableViewCell”源代码。如何在任何图标图像中替换删除标题,如下图所示:
我尝试过使用自定义editAccessoryView。
这是代码(在表cellForRowAtIndexPath中):
UIView *editingCategoryAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,80)];
editingCategoryAccessoryView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_highlighted.png"]];
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
deleteButton.backgroundColor = [UIColor clearColor];
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:@"DELETE"];
[attributedString1 addAttribute:NSKernAttributeName
value:@(1.5)
range:NSMakeRange(0, [@"DELETE" length])];
deleteButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
UIColor *btnTextColor = [UIColor whiteColor];
// Set up attributes
NSDictionary *btnAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Cabin-Bold" size:17.0f], NSFontAttributeName,
btnTextColor, NSForegroundColorAttributeName,
nil, NSKernAttributeName, nil];
[attributedString1 setAttributes:btnAttrs
range:NSMakeRange(0, attributedString1.length)];
[attributedString1 addAttribute:NSKernAttributeName
value:@(1.5)
range:NSMakeRange(0, [@"DELETE" length])];
[deleteButton setAttributedTitle:attributedString1 forState:UIControlStateNormal];
[deleteButton setFrame:cell.frame];
[deleteButton setTag:indexPath.row];
[deleteButton addTarget:self action:@selector(removeCategoryClicked:) forControlEvents:UIControlEventTouchUpInside];
// UIImageView
UIImageView *ivDeleteIcon = [[UIImageView alloc] initWithFrame:CGRectMake(21, 31, 13, 15)];
ivDeleteIcon.image = [UIImage imageNamed:@"Delete-White.png"];
ivDeleteIcon.highlightedImage = [UIImage imageNamed:@"Delete-Grey.png"];
[editingCategoryAccessoryView addSubview:ivDeleteIcon];
[editingCategoryAccessoryView addSubview:deleteButton];
cell.editingAccessoryView = editingCategoryAccessoryView;
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES; //YES here makes a red delete button appear when I swipe
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
if (editing)
editingFromEditButton = YES;
[super setEditing:(BOOL)editing animated:(BOOL)animated];
editingFromEditButton = NO;
// Other code you may want at this point...
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingFromEditButton){
//editCell = YES;
return UITableViewCellEditingStyleNone;
}
// Otherwise, we are at swipe to delete
[[tableView cellForRowAtIndexPath:indexPath] setEditing:YES animated:YES];
return UITableViewCellEditingStyleNone;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
以上代码工作正常,但只是它没有被正确解雇。 请帮我实现这个目标。谢谢!
答案 0 :(得分:0)
可以通过自定义editAccessoryView,但我使用了SWTableViewCell。首先,您需要在应用中添加SWTableViewCell类。这是我的代码:
自定义表格视图单元格.h文件(使用超类“SWTableViewCell”创建自定义表格视图单元格),如下所示:
#import <UIKit/UIKit.h>
#import "SWTableViewCell.h"
@interface UpdateTableCell : SWTableViewCell {
}
@property(nonatomic,retain)IBOutlet UILabel *lblTitle;
@property(nonatomic,retain)IBOutlet UILabel *lblViewMore;
@end
自定义表格视图单元格.m文件:
#import "UpdateTableCell.h"
@implementation UpdateTableCell
@synthesize lblTitle, lblViewMore;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
@end
在View Controller.h文件中:
#import <UIKit/UIKit.h>
#import "SWTableViewCell.h"
@interface MyBriefcaseViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, SWTableViewCellDelegate>
{
IBOutlet UITableView *tblBriefcase;
}
@property (nonatomic, retain) NSMutableArray *arrayBriefcase;
@end
查看Controller.m文件:
#import "MyBriefcaseViewController.h"
#import "UpdateTableCell.h"
#import "SWTableViewCell.h"
@implementation MyBriefcaseViewController
-(UpdateTableCell *)getNewCell
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"UpdateTableCell" owner:nil options:nil];
UpdateTableCell *cell;
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UpdateTableCell class]])
{
cell= (UpdateTableCell *)currentObject;
return cell;
}
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
UpdateTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self getNewCell];
}
//optionally specify a width that each set of utility buttons will share
[cell setRightUtilityButtons:[self rightButtons] WithButtonWidth:85.0f];
cell.delegate = self;
//do something here..
return cell;
}
- (NSArray *)rightButtons
{
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor redColor] normalIcon:[UIImage imageNamed:@"Delete-White.png"] selectedIcon:[UIImage imageNamed:@"Delete-Grey.png"]];// here you can change delete image icon and background color of editing view as per your requirement
return rightUtilityButtons;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//Set background color of cell here if you don't want default white
}
#pragma mark - SWTableViewDelegate
- (void)swipeableTableViewCell:(SWTableViewCell *)cell scrollingToState:(SWCellState)state
{
switch (state) {
case 0:
NSLog(@"utility buttons closed");
break;
case 1:
NSLog(@"left utility buttons open");
break;
case 2:
NSLog(@"right utility buttons open");
break;
default:
break;
}
}
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{
// Delete button was pressed
NSIndexPath *cellIndexPath = [tblBriefcase indexPathForCell:cell];
// do something here as per your requirement
}
- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell
{
//allow just one cell's utility button to be open at once
return YES;
}
- (BOOL)swipeableTableViewCell:(SWTableViewCell *)cell canSwipeToState:(SWCellState)state
{
return YES;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!tableView.isEditing) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//do something here...
}
我已经在我的答案中附上了我所需的所有代码来实现这一目标。当然,它会奏效。谢谢大家!!
快乐的编码.. :)