我如何在AQGridViewCell中实现删除按钮

时间:2013-03-11 15:32:45

标签: iphone uibutton custom-cell aqgridview

我想在ImageView中使用Customized AQGridViewCell添加按钮。当我单击编辑按钮时,它在ImageGridViewCell上显示删除按钮,如下图所示.i在 cellForItemAtIndex 方法中添加了删除按钮。这是我的代码

 - (AQGridViewCell *) gridView: (AQGridView *) gridView cellForItemAtIndex: (NSUInteger) index
{
static NSString *photoCellIdentifier = @"IBImageGridViewCell";
IBImageGridViewCell *cell = (IBImageGridViewCell *)[self.gridView dequeueReusableCellWithIdentifier:photoCellIdentifier];
if (cell == nil) {
    cell = [[IBImageGridViewCell alloc] initWithFrame:CGRectMake(3.0, 3.0, 100.0, 120.0) reuseIdentifier:photoCellIdentifier];
    cell.selectionStyle = AQGridViewCellSelectionStyleNone;

}
PTKEntry *entry = [_objects objectAtIndex:index];

UIButton *deletebutton = [UIButton buttonWithType:UIButtonTypeCustom];
[deletebutton addTarget:self
                 action:@selector(deleteimage:)
       forControlEvents:UIControlEventTouchDown];

[deletebutton viewWithTag:index];

deletebutton.frame = CGRectMake(70,0,30,30);

UIImage * buttonImage = [UIImage imageNamed:@"delete.png"];

[deletebutton setImage:buttonImage forState:UIControlStateNormal];

if (self.gridView.editing) {
    deletebutton.hidden=NO;

}
else{
    deletebutton.hidden=YES;

}


[cell.contentView addSubview:deletebutton];
[cell.contentView bringSubviewToFront:deletebutton];


    if (entry.data && entry.data.photo) {

        cell.imageView.image = entry.data.photo;

        NSLog(@"load table");

    } else {

        cell.imageView.image = nil;
         NSLog(@"Not load table");
    }



return cell;
}

当视图加载时没有显示删除按钮。然后单击“删除”按钮显示每个网格单元格的删除,并单击“完成”按钮,删除按钮未从我的网格中隐藏查看单元格视图my image

2 个答案:

答案 0 :(得分:0)

您可以在AQGridView中创建委托方法,并在您的类中实现它,如

- (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index

这是一种委托方法。 如果您创建委托方法

-(void) gridView:(AQGridView *)argGridView deleteCell:(AQGridViewCell *)cell atIndex:(NSUInteger)index; 

这将作为didSelectItemAtIndex调用:当您单击删除按钮时。

为此,请遵循以下步骤。

添加方法canHideDelete:显示和隐藏自定义单元格IBImageGridViewCell中的删除按钮。 当您单击指定的单元格,然后[单元格canHideDelete:NO]显示删除按钮。 在你的IBImageGridViewCell中,

您可以创建一个块来删除指定的单元格。 去做这个, 创建一个扩展AQGridViewCell_Extension.h到AQGridViewCell,如

#import "AQGridViewCell.h"

typedef void(^AQGridViewCellDeleteBlock)(AQGridViewCell*);

@interface AQGridViewCell ()

@property(nonatomic, copy) AQGridViewCellDeleteBlock deleteBlock;

@end

在IBImageGridViewCell.m和AQGridView.m中导入“AQGridViewCell_Extension.h”

现在,创建一个选择器来处理删除按钮并调用一个块来删除单元格。

    -(void)deleteButtonAction
{
    self.deleteBlock(self);
}

创建要在类中实现的委托方法以删除单元格 将其添加到@protocol AQGridViewDelegate

下的AQGridView.h中

- (void)gridView:(AQGridView *)gridView deleteCell:(AQGridViewCell *)cell atIndex:(NSUInteger)index; 现在,在AQGridView.m中, 改变方法

- (AQGridViewCell *) createPreparedCellForIndex: (NSUInteger) index usingGridData: (AQGridViewData *) gridData
{
    [UIView setAnimationsEnabled: NO];
    AQGridViewCell * cell = [_dataSource gridView: self cellForItemAtIndex: index];
    cell.separatorStyle = _flags.separatorStyle;
    cell.editing = self.editing;
    cell.displayIndex = index;

    cell.frame = [self fixCellFrame: cell.frame forGridRect: [gridData cellRectAtIndex: index]];
    if ( _backgroundView.superview == self )
        [self insertSubview: cell aboveSubview: _backgroundView];
    else
        [self insertSubview: cell atIndex: 0];
    [UIView setAnimationsEnabled: YES];
    __block AQGridView *localAQGridView = self;
    // DELETE BUTTON BLOCK - TO CALL DELEGATE METHOD
    cell.deleteBlock = ^(AQGridViewCell *argCell)
    {
        NSInteger index = [localAQGridView indexForCell:argCell];
        //NSLog(@"Cell to be deleted is %d", index);
        [localAQGridView.delegate gridView:localAQGridView deleteCell:argCell atIndex:index];

    };

    return ( cell );
}

实施以下方法以删除班级中的单元格

-(void) gridView:(AQGridView *)argGridView deleteCell:(AQGridViewCell *)cell atIndex:(NSUInteger)index
{
    NSLog(@"ON deleting cell at %d", index);
    [mediaItemsArray removeObjectAtIndex:index];
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];

    [argGridView beginUpdates];
    [argGridView deleteItemsAtIndices:indexSet withAnimation:AQGridViewItemAnimationFade];
    [argGridView endUpdates];

}

我希望这会对你有所帮助。

答案 1 :(得分:0)

在不修改AQGridView类的情况下执行此操作的另一种方法

在IBImageGridViewCell.xib文件中添加删除按钮并将您的IBImageGridViewCell.h修改为

@class IBImageGridViewCell;
@protocol CustomGridCellViewDelegate<NSObject>

@optional

-(void) onDeleteButtonTouched:(IBImageGridViewCell *)sender;

@end


@interface IBImageGridViewCell : AQGridViewCell

+ (id) cellFromNib;

@property (nonatomic,assign) id <CustomGridCellViewDelegate> delegate;

@property (nonatomic, readonly, retain) IBOutlet UIView *contentView;

@property (weak, nonatomic) IBOutlet UIButton *deleteButton;

@property (nonatomic, copy) NSString *reuseIdentifier;

- (IBAction)deleteButtonAction:(UIButton *)sender;

@end

在您的IBImageGridViewCell.m文件中添加

- (IBAction)deleteButtonAction:(UIButton *)sender
{
    [self.delegate onDeleteButtonTouched:self];
}

现在在mainViewController.h中添加委托

@interface mainViewController : UIViewController<CustomGridCellViewDelegate>

在mainViewController.m文件中

方法

- (AQGridViewCell *) gridView: (AQGridView *) gridView cellForItemAtIndex: (NSUInteger) index

添加

cell.delegate=self;
cell.deleteButton.tag =index;

处理删除按钮操作

-(void)onDeleteButtonTouched:(NTGridViewCell *)sender
{
    NSLog(@"Selected Button:%d",sender.deleteButton.tag);
    [yourArrayList removeObjectAtIndex:sender.deleteButton.tag];
    [self.gridView reloadData];

    //***Animated delete 
//    [yourArrayList removeObjectAtIndex:sender.deleteButton.tag];
//    NSIndexSet* set = [NSIndexSet indexSetWithIndex:sender.deleteButton.tag];
//    [self.gridView beginUpdates];
//    [self.gridView deleteItemsAtIndices:set  withAnimation:AQGridViewItemAnimationFade];
//    [self.gridView endUpdates];
}