Sas*_*ent 49 ios uicollectionview uicollectionviewcell
我如何才能找到indexPath
用于cell
在中间UICollectionView
?
我有水平滚动,只有一个大cell
的可见(cell
两侧的其他部分也是可见的).我需要删除cell
位于中心(手段 - 当前cell
)而不触摸它.
只需按"废纸篓"按钮并确认删除.但现在它只删除了第一个cell
s.
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.destructiveButtonIndex) {
initialPinchPoint = ????????
self.tappedCellPath = [self.collectionView indexPathForItemAtPoint:initialPinchPoint];
Technique *technique = [arrayOfTechnique objectAtIndex:self.tappedCellPath.row];
[self deleteData:[NSString stringWithFormat:@"DELETE FROM TECHNIQUES WHERE TECHNIQUENAME IS '%s'",[technique.techniquename UTF8String]]];
[arrayOfTechnique removeObjectAtIndex:self.tappedCellPath.row];
//[arrayOfTechnique removeObjectAtIndex:[custom_layout ]];
[self removeImage:technique.techniquename];
[self.collectionView performBatchUpdates:^{
[self.collectionView deleteItemsAtIndexPaths:[NSArrayarrayWithObject:self.tappedCellPath]];
} completion:nil];
[self checkArrayCount];
}
}
Run Code Online (Sandbox Code Playgroud)
mic*_*tox 35
就像你自己一样,indexPathForItemAtPoint
是找到你感兴趣的元素的索引路径的好方法.如果你的问题是:我怎么知道这一点的坐标?然后你应该尝试(使用你在代码片段中给出的相同名称):
initialPinchPoint = CGPointMake(self.collectionView.center.x + self.collectionView.contentOffset.x,
self.collectionView.center.y + self.collectionView.contentOffset.y);
Run Code Online (Sandbox Code Playgroud)
Jla*_*lam 33
这将是更好的代码,因为它更清晰,更容易阅读,所有内容偏移计算是多余的:
NSIndexPath *centerCellIndexPath =
[self.collectionView indexPathForItemAtPoint:
[self.view convertPoint:[self.view center] toView:self.collectionView]];
Run Code Online (Sandbox Code Playgroud)
这也是您实际尝试的正确表示:
1.获取视图控制器视图的中心点 - 也就是视觉中心点
2.将其转换为您感兴趣的视图的坐标空间 - 其中是集合视图
3.获取存在于给定位置的索引路径.
MCR*_*MCR 25
这是我在Swift 3中所做的
private func findCenterIndex() {
let center = self.view.convert(self.collectionView.center, to: self.collectionView)
let index = collectionView!.indexPathForItem(at: center)
print(index ?? "index not found")
}
Run Code Online (Sandbox Code Playgroud)
Bob*_*j-C 21
迅速:
extension UICollectionView {
var centerPoint : CGPoint {
get {
return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y);
}
}
var centerCellIndexPath: IndexPath? {
if let centerIndexPath: IndexPath = self.indexPathForItemAtPoint(self.centerPoint) {
return centerIndexPath
}
return nil
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
if let centerCellIndexPath: IndexPath = collectionView.centerCellIndexPath {
print(centerCellIndexPath)
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特3:
extension UICollectionView {
var centerPoint : CGPoint {
get {
return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y);
}
}
var centerCellIndexPath: IndexPath? {
if let centerIndexPath = self.indexPathForItem(at: self.centerPoint) {
return centerIndexPath
}
return nil
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢micantox!
我有多个UICollectionView的可见单元格,需要将单元格放在集合视图的中心.类似于Adobe Content Viewer的东西.如果有人在类似的情况下挣扎:
#pragma mark - UIScrollViewDelegate methods
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
CGPoint centerPoint = CGPointMake(self.pageContentCollectionView.center.x + self.pageContentCollectionView.contentOffset.x,
self.pageContentCollectionView.center.y + self.pageContentCollectionView.contentOffset.y);
NSIndexPath *centerCellIndexPath = [self.pageContentCollectionView indexPathForItemAtPoint:centerPoint];
[self.pageContentCollectionView scrollToItemAtIndexPath:centerCellIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
我做的像水平UICollectionView我使用Swift 2.x.
private func findCenterIndex() {
let collectionOrigin = collectionView!.bounds.origin
let collectionWidth = collectionView!.bounds.width
var centerPoint: CGPoint!
var newX: CGFloat!
if collectionOrigin.x > 0 {
newX = collectionOrigin.x + collectionWidth / 2
centerPoint = CGPoint(x: newX, y: collectionOrigin.y)
} else {
newX = collectionWidth / 2
centerPoint = CGPoint(x: newX, y: collectionOrigin.y)
}
let index = collectionView!.indexPathForItemAtPoint(centerPoint)
print(index)
}
override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
findCenterIndex()
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
38834 次 |
最近记录: |