我有一个程序,将搜索数据库并填充集合视图。目前工作。每个项目都有一个“关注”按钮,当点击该按钮时,我需要它执行一个操作,但我需要该操作才能访问indexPath.item
。程序崩溃的原因是因为按钮的功能隐藏在集合视图方法中,但这是我可以访问indexPath.item
的唯一方法。如何修复崩溃并仍然可以访问indexPath.item
?如何访问我的按钮功能中的indexPath.item?
func followuser(sender: UIButton){
let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.collectionview)
if var indexPath :NSIndexPath = self.collectionview.indexPathForItemAtPoint(pointInTable){
print(pointInTable)
}
var relation: PFRelation = (PFUser.currentUser()?.relationForKey("Friendship"))!
//relation.addObject(users[indexPath.item])
print(relation)
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionview.dequeueReusableCellWithReuseIdentifier("searchcell", forIndexPath: indexPath) as! searchcustomcell
cell.follow.addTarget(self, action: "followuser:", forControlEvents: UIControlEvents.TouchUpInside)
当在Swift/Objective-C中命名方法时,命名约定是该方法以小写字母开头。例如,在你的'func FollowUser'应该是'func followUser'。 – Adrian
谢谢我会更新我的代码。 – xxtmanxx
请按照我在回答和评论中列出的内容提供更多信息,或删除问题。 –