2017-09-26 6 views
0

셀에 두 개의 단추를 넣을 사용자 지정 UITableViewCell 하위 클래스가 있는데 두 단추 모두 클릭 할 수있게하려면 지금까지는 아무런 클릭도 트리거 할 수 없습니다 ,하지만 난 내 UITableViewController 하위 클래스에서 해제). 기본적으로 두 개의 버튼 중 하나를 선택하면 두 버튼을 제거하고 선택 가능한 선택 목록 (버튼) 목록으로 셀의 내용을 업데이트해야합니다. 프로그래밍 방식으로 모든 작업을 수행하고 있습니다 (IB 없음).사용자 지정 UITableViewCell Swift에서 여러 단추 누르기

My TableViewCell with Initial Two Buttons

나는 많은 주위를 보았다하고 tableViewCell에 하나 개 이상의 버튼을 처리 아무것도 발견하지 않았습니다. 현재 내가있는 UITableViewCell의로 awakeFromNib() 내 버튼에 목표를 추가하려고했습니다 : 나는 시도했다

for button in initialChoiceButtons{ 
      button.isUserInteractionEnabled = true 
      button.addTarget(self, action: #selector(initialChoicePressed(sender:)), for: UIControlEvents.touchUpInside) 
     } 

한 가지는 내 사용자 지정 셀 cellForRowAt 내있는 tableView에 있습니다 전면에 내 버튼을 가지고 셀의 :

for button in (cell as! FormDropDownTableViewCell).initialChoiceButtons{ 
        cell.bringSubview(toFront: button) 
       } 

나는 정말 어려움을 겪고 있고 쉽게 느껴집니다. 나는 모든 것을위한 scrollview 내부의 stackView를 사용하는 직전이다 ...

+0

그래서 난 그냥있어이 기본적으로 cellForRowAt 내 버튼에 대상을 추가하여 tableviewcell 내 여러 버튼 작업을 자신에게 이야기하고 내 tableviewcontroller에 내 액션 메소드를 이동 즐겼다. 이 문제는 내 tableviewcell과 tableviewcontroller 사이의 기능을 구분하고 싶습니다. 지금이 작업을하기 위해서 나는 내 tableviewcontroller 내 액션 함수에서 이것을해야만한다 : (sender.superview! .superview! .superview! .superview as! FormDropDownTableViewCell) .showSelectionButtons() –

+0

^^ (보낸 사람은 UIButton) 그리고 superviews는 VerticalStackView, HorizontalStackView, ContentView, UITableViewCell입니다. 거기에 내 tableviewcell 내 실제 작업 메서드를 유지할 수있는 더 나은 방법이 있어야합니다 –

답변

0

좋아, 그래서 나는 tableviewcell에서 버튼 탭을 분리하는 다소 깨끗한 방법을 알아 냈다. 내 tableviewcell에있는 모든 버튼에 대해 내 targetview에서 내 tableviewcontroller 호출합니다. 내 FormDropDownTableViewCell에서

protocol UIButtonSelectorDelegate{ 
    func handleTap(button:DelegatingButton) //will select different functions based on DelegatingButton.actionType 
} 
class DelegatingButton:UIButton{ 
    var selectorDelegate:UIButtonSelectorDelegate? 
    var actionType:String = "default" 
} 

은 내가 UIButtonSelectedDelegate을 준수하고과 같이 handleTap 구현 :

button.addTarget(self, action: #selector(handleButtonTaps), for: UIControlEvents.touchUpInside) 
:

func handleTap(button:DelegatingButton){ 
     switch button.actionType{ 
     case "initialChoiceSelect": 
      initialChoicePressed(initialChoice:button) //specific method for certain button.actionType also in my FormDropDownTableViewCell 
     case "cardTypeSelect": 
      cardTypeSelected(selectedCardType:button) 
     default: 
      break 

     } 
    } 

지금 내가 그렇게처럼 내 tableviewcontroller에 cellForRowAt의 모든 버튼의 대상-작업을 추가

및 tableviewcontroller의 handleButtonTaps func은 간단합니다.

는 = P .. 좋아