3

我想在带有 ImageView 的自定义 AQGridViewCell 中添加按钮。当我单击编辑按钮时,它在 ImageGridViewCell 上显示删除按钮,如下图所示。我在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;
}

当视图加载没有显示删除按钮时。在单击删除按钮时,它显示每个网格单元格的删除,然后单击完成按钮,删除按钮没有从我的网格中隐藏查看单元格视图我的形象

4

2 回答 2

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];
}
于 2013-11-26T12:56:36.033 回答
0

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

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

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

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

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

为此,请遵循此程序。

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

您可以创建一个块来删除指定的单元格。为此,请为 AQGridViewCell 创建一个扩展 AQGridViewCell_Extension.h,例如

#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 *)单元格atIndex:(NSUInteger)索引;现在,在 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];

}

我希望这可以帮助你。

于 2013-10-05T11:43:56.627 回答