3

i want to add button in my Customized AQGridViewCell with ImageView. when i Click Edit button its showing delete button on the ImageGridViewCell like below image.i added delete button in cellForItemAtIndex method. here my code

 - (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;
}

when the view loading didn't show delete button. and while click delete button it showing delete for each grid cell and click done button that delete button didn't hidden from my grid View cell view here my image

Vijay
  • 579
  • 1
  • 11
  • 22

2 Answers2

0

You can create delegate method in AQGridView and implement it in your class like

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

This a delegate method approach. If you create delegate method

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

This will get invoked as didSelectItemAtIndex: when you click on delete button.

To do this follow this procedure.

Add a method canHideDelete: to show and hide delete button in your custom cell IBImageGridViewCell. When you click on a specified cell then [cell canHideDelete:NO] to show delete button. In your IBImageGridViewCell,

you can create a block to delete a specified cell. To do this, create an extension AQGridViewCell_Extension.h to AQGridViewCell like

#import "AQGridViewCell.h"

typedef void(^AQGridViewCellDeleteBlock)(AQGridViewCell*);

@interface AQGridViewCell ()

@property(nonatomic, copy) AQGridViewCellDeleteBlock deleteBlock;

@end

import "AQGridViewCell_Extension.h" in IBImageGridViewCell.m and AQGridView.m

Now, create a selector to handle delete button and call a block to delete cell.

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

Create a delegate method to be implemented in your class to delete cell Add this to AQGridView.h under @protocol AQGridViewDelegate

-(void) gridView:(AQGridView*) gridView deleteCell:(AQGridViewCell*) cell atIndex:(NSUInteger) index; Now, in AQGridView.m, change method

- (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 );
}

Implement following method to delete a cell in your class

-(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];

}

I hope this helps you.

satyanarayana
  • 265
  • 4
  • 14
0

Another way of doing it without modifying the AQGridView class

Add a delete button in your IBImageGridViewCell.xib file and Modify your IBImageGridViewCell.h as

@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

In your IBImageGridViewCell.m file add

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

Now in your mainViewController.h add the delegate

@interface mainViewController : UIViewController<CustomGridCellViewDelegate>

In your mainViewController.m file

In the method

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

Add

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

Handle the delete button action

-(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];
}
Francis F
  • 3,157
  • 3
  • 41
  • 79