UItableView
中的我们可以使用这些代码从indexPath
获取UITableView
extension UIView{
func getButtonIndexPath(tableView:UITableView) -> NSIndexPath{
let buttonOriginInTableView = self.convertPoint(CGPointZero, toView: tableView)
return tableView.indexPathForRowAtPoint(buttonOriginInTableView)!
}
}
但它在UICollectionView
extension UIView{
func getIndexPath(uc:UICollectionView) -> NSIndexPath?{
let senderP = CGPointMake(0, self.frame.height / 2)
let point : CGPoint = self.convertPoint(senderP, toView:uc)
let ind = uc.indexPathForItemAtPoint(point)
print("what i click is", point, ind)
return ind
}
我把它放在UICollectionView
delBtn.addTarget(self, action: #selector(FavViewController.DelFavAction(_:)), forControlEvents: .TouchUpInside)
,功能是
func DelFavAction(sender:UIButton){
guard let indexPath = sender.getIndexPath(self.collectionView) else {
return
}
print(indexPath)
}
但大多数情况下错误indexPath.item
,这是怎么回事?
与此代码有关吗?
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return rectArr[indexPath.item].size
}
答案 0 :(得分:0)
你写的这行:
delBtn.tag = indexPath.item;
下面写下其他一行:
func DelFavAction(sender:UIButton){
// sender.tag to convert uibutton;
// for objectvie C write: UIButton *btn = (UIButton *)sender;
}
并获取项目:
rel_status==$relation1