2013-03-11 5 views
3

Я хочу добавить кнопку в свой пользовательский AQGridViewCell с ImageView. когда я нажимаю кнопку «Изменить», показывает кнопку удаления на ImageGridViewCell, как показано ниже. Я добавил кнопку удаления в 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,

вы можете создать блок для удаления указанной ячейки. Для этого создать расширение AQGridViewCell_Extension.h для AQGridViewCell как

#import "AQGridViewCell.h" 

typedef void(^AQGridViewCellDeleteBlock)(AQGridViewCell*); 

@interface AQGridViewCell() 

@property(nonatomic, copy) AQGridViewCellDeleteBlock deleteBlock; 

@end 

импорта «AQGridViewCell_Extension.h» в IBImageGridViewCell.m и AQGridView.m

Теперь создайте селектор для обработки кнопки удаления и вызовите блок для удаления ячейки.

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

Создайте метод делегата быть реализован в классе, чтобы удалить ячейки Добавить это AQGridView.h под @protocol AQGridViewDelegate

- (Недействительными) 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]; 

} 

Я надеюсь, что это поможет вам.

0

Другой способ сделать это без изменения класса AQGridView

Добавить кнопку удаления в файле IBImageGridViewCell.xib и изменить IBImageGridViewCell.ч, как

@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]; 
}