2016-09-12 4 views
0

프로그래밍 방식으로 셀을 만들고 각 셀에 삭제 단추를 추가하고 있습니다. 문제는 내가 그들의 숨겨진 상태를 토글하고 싶다는 것입니다. 아이디어는 모든 버튼의 상태를 동시에 전환하는 편집 버튼을 갖는 것입니다. 어쩌면 나는 잘못된 방향으로 가고있을 것인가?Swift : UICollectionView 셀의 단추 숨기기

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("verticalCell", forIndexPath: indexPath) as! RACollectionViewCell 
    let slide = panelSlides[indexPath.row] 
    cell.slideData = slide 
    cell.slideImageView.setImageWithUrl(NSURL(string: IMAGE_URL + slide.imageName + ".jpg")!) 
    cell.setNeedsLayout() 

    let image = UIImage(named: "ic_close") as UIImage? 
    var deleteButton = UIButton(type: UIButtonType.Custom) as UIButton 
    deleteButton.frame = CGRectMake(-25, -25, 100, 100) 
    deleteButton.setImage(image, forState: .Normal) 
    deleteButton.addTarget(self,action:#selector(deleteCell), forControlEvents:.TouchUpInside) 
    deleteButton.hidden = editOn 
    cell.addSubview(deleteButton) 
    return cell 
} 


@IBAction func EditButtonTap(sender: AnyObject) { 
    editOn = !editOn 
    sidePanelCollectionView.reloadData() 
} 
+0

버튼의 '숨겨진'속성은 어디에서 토글하려고합니까? 당신은 실제로 어떤 도움을 찾고 있습니까? – rmaddy

+0

버튼에 태그를 사용하여 시도했지만 작동하지 않았습니다. 나는 모든 버튼을 참조하는 방법을 잘 모르겠습니다. –

+0

셀이 숨겨진 속성을 읽고 설정하는 부울 변수가있는 경우 (deleteButton.hidden = myBool) 그리고 매번 눈에 띄는 셀을 다시로드합니다. – ohr

답변

1

나는 싶은 것은 인덱스로 모든 데이터를 반복하고 각 인덱스 UICollectionView 당신에 cellForItemAtIndexPath: 전화라고 생각합니다. 그런 다음 기존 셀을 가져 와서 특정 유형 as! RACollectionViewCell으로 캐스팅 한 다음 버튼 숨김 값을 이렇게 설정합니다.

(내가 엑스 코드에 아니에요 사과는 바로 지금이를 확인하려면하지만이 요점이다) :

for (index, data) in myDataArray.enumerate() { 
    let cell = collectionView.cellForRowAtIndexPath(NSIndexPath(row: index, section: 0)) as! RACollectionViewCell 
    cell.deleteButton.hidden = false 
} 

당신은 아마 또한 뷰 컨트롤러에 isEditing 부울 변수의 일종이 필요 추적 스크롤하는 동안 새로 구성된 셀은 단추가 있거나 없거나 계속 표시되도록 편집 상태에 있습니다. 위의 코드가 필요하다면 스크롤이 발생해도 계속 작동하도록 할 수 있습니다. 매번 새로운 삭제 버튼을 만드는 대신 스토리 보드에 버튼을 놓고 참조를 설정해야합니다. cell.deleteButton.hidden = !isEditing

관련 문제