2013-03-11 19 views
3

我想用ImageView在我的Customized AQGridViewCell中添加按鈕。當我點擊編輯按鈕其ImageGridViewCell上的顯示刪除按鈕就像下面的image.i中添加刪除按鈕cellForItemAtIndex方法。這裏我的代碼我如何在AQGridViewCell中實現刪除按鈕

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

回答

0

可以在AQGridView創建委託方法,並在你的類像

實現它在我的網格視圖電池視圖中隱藏

- (無效)的GridView:(AQGridView *)GRIDVIEW didSelectItemAtIndex:(NSUInteger)指數

此委託方法的方法。 如果您創建的委託方法

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

這將被調用爲didSelectItemAtIndex:當你點擊刪除按鈕。

要做到這一點,請按照此過程。

添加方法canHideDelete:在自定義單元格IBImageGridViewCell中顯示和隱藏刪除按鈕。 當你點擊指定的單元格時,[cell canHideDelete:NO]顯示刪除按鈕。 在您的IBImageGridViewCell,

您可以創建一個塊來刪除指定的單元格。 要做到這一點, 創建像IBImageGridViewCell.m

#import "AQGridViewCell.h" 

typedef void(^AQGridViewCellDeleteBlock)(AQGridViewCell*); 

@interface AQGridViewCell() 

@property(nonatomic, copy) AQGridViewCellDeleteBlock deleteBlock; 

@end 

進口「AQGridViewCell_Extension.h」和AQGridView.m

擴展AQGridViewCell_Extension.h到AQGridViewCell現在,創建一個選擇器來處理刪除按鈕,調用一個塊來刪除單元格。

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

創建委託方法在您的類中實現刪除單元格 這AQGridView.h下添加@protocol AQGridViewDelegate

- (空)的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]; 

} 

我希望這可以幫助您刪除單元格。

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文件在您mainViewController.h添加

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

現在添加委託

@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]; 
} 
相關問題