Ale*_*one 8 objective-c uitableview ios6 uicollectionview
我正在使用构建我的第一个应用程序,UICollectionView并注意到在删除对象方面我无能为力.对于UITableView应用程序,有滑动删除方法:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用GMGridView它时,它的行为类似于iPhone主屏幕上的长按 - 可以显示视图星形,并且可以显示删除按钮,该按钮用于删除视图.我当然可以尝试复制这种行为,但我不确定用户是否会"得到它".
我对让用户删除对象的选项UICollectionView感兴趣- 我是否必须实现自己的删除手势/控件,或者是否存在我缺少的东西(或开源)?
我在包含CollectionView的视图控制器中插入了此代码并以此方式执行.您可能已经使用轻击手势来检测所选单元格.
- (IBAction)didLongPressCellToDelete:(UILongPressGestureRecognizer*)gesture {
CGPoint tapLocation = [gesture locationInView:self.myCollectionView];
NSIndexPath *indexPath = [self.myCollectionView indexPathForItemAtPoint:tapLocation];
if (indexPath && gesture.state == UIGestureRecognizerStateBegan) {
NSLog(@"image with index %d to be deleted", indexPath.item);
self.itemToBeDeleted = indexPath.item;
UIAlertView *deleteAlert = [[UIAlertView alloc]
initWithTitle:@"Delete?"
message:@"Are you sure you want to delete this image?"
delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];
[deleteAlert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"selected button index = %d", buttonIndex);
if (buttonIndex == 1) {
// Do what you need to do to delete the cell
[self.myCollectionView reloadData];
}
}
Run Code Online (Sandbox Code Playgroud)
默认UICollectionViewCell只有一个空白视图(没有标题,没有删除按钮,没有imageView)
子类UICollectionViewCell并在其上添加删除按钮.setHidden = NO当你想显示它时(例如向下滑动)
使用自定义委托删除数据并重新加载collectionView
示例使用向右滑动删除UICollectionViewCell在storyBoard中垂直滚动:
//Cell.h
@class MyCell;
@protocol MyCellDelegate
-(void)deleteButtonClick:(MyCell *)cell;
@end
@interface MyCell : UICollectionViewCell
@property (weak , nonatomic) id<MyCellDelegate> delegate;
@property (weak,nonatomic) IBOutlet *delButton;
@end
//Cell.m
-(void)awakeFromNib{
[self.delButton setHidden:YES]
//add Swipe right to here
}
-(void)handleSwipeRight:(UISwipeGestureRecognizer *)swipe {
[self.delButton setHidden:NO];
}
-(IBAction)clickDelBut{
[self.delegate deleteButtonClick:self];
}
//ViewController.m
//In cellForItemsAtIndexPath cell.delegate = self.
-(void)deleteButtonClick:(MyCell *)cell{
//get indexPath, delete data and reload collectionView here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18551 次 |
| 最近记录: |