2017-02-20 5 views
1

이것은 나를 괴롭 히고 있습니다. 몇 시간 동안 읽고 있었으므로 모든 것을 시도하고이 버튼 선택기를 작동시킬 수 없습니다. 이것은 어렵지 않아야합니다.컬렉션 내부의 버튼에 대한 셀렉터

CellForItemAt 안에 단추 태그를 설정하고 단추를 호출 해보십시오. 그리고 괄호 조합의 다른 수와 난 여전히 인식 할 수없는 선택기를 얻을 : 나는 시도했다

cell.deleteCellButton.tag = indexPath.item 
cell.deleteCellButton.addTarget(self, action: #selector(deleteCellButtonTapped(sender:)), for: UIControlEvents.touchUpInside) 

는 (_ :) "deleteCellButtonTapped". 나는 왜 autocomplete가 추천하는지 (발신자 :) 나는 전에 이것을 본 적이 없다는 것을 모른다.

다음 내 버튼 기능 :

func deleteCellButtonTapped(sender: UIButton!) { 
    self.packArray.remove(at: sender.tag) 
     print(packArray.count) 
    self.outerCollectionView.deleteItems(at: [IndexPath(item: sender.tag, section: 0)]) 

    self.outerCollectionView.reloadData() 
    self.outerCollectionView.layoutIfNeeded() 
} 
+0

#selector (자기 작동

class MyClassViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { .... // Other implementation methods func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { ....// create cell object and dequeue cell.deleteCellButton.addTarget(self, action: #selector(MyClassViewController.deleteCellButtonTapped(_:)), for: .touchUpInside) return cell } func deleteCellButtonTapped(_ sender: Any) { // your method implementation print("Selector method called") } } 

희망 deleteCellButtonTapped (보낸 사람 :)) 나를 위해 일했다. – Anuraj

답변

0

는 스위프트 3 사용하고 가정 및 func deleteCellButtonTapped(sender: UIButton!)는 같은 클래스에 있습니다

addTarget(self, action: #selector(deleteCellButtonTapped(sender:)), for: .touchUpInside) 

가 잘 작동합니다.

0

선택기 방법을 참조하면 해당 클래스가 저에게 적합합니다.

당신이 할 수있는 것은 클래스 이름에 의해 접두어가 붙은 액세스 선택기 방법입니다.

클래스 이름이 MyClassViewController 인 것으로 가정합니다. 그리고 코드는 다음과 같이 표시됩니다.이 잘