我正在为iPad实施一个Table并面临一些重大问题。
对于GridView,我实现了自己的UITableViewCell子类,工作正常。
数据显示正确,但是当我想访问单个单元格以转到某个新的详细信息视图时,我遇到了问题。由于一行只包含一个单元格,didSelectRowAtIndexPath只允许我访问完整单元格,但我不知道单个单元格所在的列。
然后我实现了TapGestureRecognizer。这显示了行和列并且有效,但直到我开始滚动...列部分仍然有效,但由于TapRecognizer与didSelectRowAtIndexPath重叠(但不是那么重要的副作用,所以行显示不正确..没有所选行的蓝色突出显示)。
有没有办法找出我滚动了多少像素?或者是否有更好的解决方案?
答案 0 :(得分:2)
我强烈建议您使用UICollectionView
而不是第三方课程。访问所有委托协议有很多好处(例如,在没有UIMenuController
的长按下显示剪切复制粘贴UIGestureRecognizer
,我自己使用一个作为网格。
为了实现网格布局,我做了以下内容......
1)我在.h文件中设置了以下代理:
@interface YourViewControllerWithCollectionView : UIViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> {
}
@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView;
@end
请注意,我没有设置UICollectionViewDelegate
,因为UICollectionViewDelegateFlowLayout
实际上是UICollectionViewDelegate
的子协议,因此无需同时设置它们。
2)在.m文件中,合成集合视图,并在viewDidLoad中声明数据源和委托:(不要忘记连接你的插座,你可能想要放置一个背景单元格上的颜色,你可以看到它)
@synthesize myCollectionView;
viewDidLoad中:
- (void)viewDidLoad
{
self.myCollectionView.delegate = self;
self.myCollectionView.dataSource = self;
//...
}
3)实施数据源
#pragma mark - UICollectionView Datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//the number of cells you want per row
return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//load sublassed UICollectionViewCell called MyCollectionViewCell
static NSString *cellIdentifier = @"cell";
MyCustomCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.title.text = @"Title"
// customize the cell...
return cell;
}
5)实施UICollectionViewDelegateFlowLayout
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//this is what forces the collectionview to only display 4 cells for both orientations. Changing the "-80" will adjust the horizontal space between the cells.
CGSize retval = CGSizeMake((myCollectionView.frame.size.width - 80) / 4, 78);
return retval;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// for the entire section, which we have set to 1, adjust the space at
// (top, left, bottom, right)
// keep in mind if you change this, you will need to adjust the retVal
// in the method above
return UIEdgeInsetsMake(5, 20, 10, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
CGFloat interimSpacing = 0.0f;
return interimSpacing;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
CGFloat lineSpacing = 0.0f;
return lineSpacing;
}
6)最后,但肯定并非最不重要的是,在方向更改时布局无效以重绘单元格:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration{
[self.myCollectionView.collectionViewLayout invalidateLayout];
}
由于您实施了UICollectionViewDelegateFlowLayout
,因此您已有权访问UICollectionViewDelegate
来处理选择等,例如:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//do something when a cell is tapped...
}
可在此处找到更多信息:http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
答案 1 :(得分:0)
答案 2 :(得分:0)
我推荐AGAINST UICollectionView。 UICollectionView易于使用,但目前还不够稳定。我正在为我的应用使用GMGridView。经过几个月的运营,我可以说它足以稳定生产。另一种选择是PSTCollectionView,它是UICollectionView的100%API兼容替代品。但是,它未完成,甚至比UICollectionView包含more bugs。
我对PSTCollectionView的令人不安的问题是:
我对UICollectionView的令人不安的问题是:
检查开放式雷达 https://openradar.appspot.com/search?query=UICollectionView 对于UICollectionView的所有当前问题。
我相信UICollectionView和PSTCollectionView在稳定时会是不错的选择。但目前,GMGridView是更好的选择。