2016-10-02 2 views
0

테이블보기에 섹션을 삭제하거나 추가 할 수있는 권한을 부여하려고합니다. nib에서 사용자 정의 헤더보기를 작성하고 레이블과 두 개의 버튼을 추가했습니다. 문제는 삭제하거나 섹션을 추가 할 때 점을 연결할 수 없다는 것입니다. 이 지금까지 내 코드는 다음과 같습니다Custom header button을 사용하여 tableview에서 섹션을 수정 하시겠습니까?

//Setup Header 
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerID) as! CustomHeader 
    //Configure header... 
    header.addBtn.addTarget(self, action:Selector(("add")), for: .touchUpInside) 
    header.addBtn.tag = section 

    switch section { 
    case 0: 
     header.sectionLBL.text = "Detail 1" 
    case 1: 
     header.sectionLBL.text = "Detail 2" 
    default: 
     break 
    } 
    return header 
} 

@IBAction func addButton(sender: UIButton) { 
    //Add section code here... 
} 

@IBAction func delete(sender:UIButton){ 
    //delete section code here... 
} 

나는 또한 내가 버튼을 사용하여 작업 할 수있는 커밋 스타일을 얻을 것입니다 방법을 알아 내려고 노력했다 : 당신은 스토리 보드 장면이나 펜촉을 연결할 수 있습니다

//Setup Editing Style for table view 
    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(true) 


    } 

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
//Editing styles for deletion or insertion... 
} 

답변

1

만 그들의 소속 클래스에.

1) CustomHeader 클래스에 프로토콜을 추가하고 클래스가 NIB에 할당 보장 :

protocol CustomHeaderButtonPressDelegate { 
    func didPressAdd() 
    func didPressDelete() 
} 

class CustomHeader: UIView { 
    var delegate:CustomHeaderButtonPressDelegate? 
    @IBAction func addButton(sender: UIButton) { 
     delegate?.didPressAdd() 
    } 

    @IBAction func delete(sender:UIButton){ 
     delegate?.didPressDelete() 
    } 
} 

2) 지정 그는 NIB를 새 클래스에

enter image description here

투자 은행 Inspector에서 "Class"를 UIView에서 CustomHeader으로 변경하십시오.

3) 프로그래밍 방식 CustomHeaderIBAction

4) 세트 대리자

class YourClass: UITableViewController, CustomHeaderButtonPressDelegate { 
... 
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerID) as! CustomHeader 
    header.delegate = self 
... 
} 
... 
func didPressAdd() { 
    //handlePress 
} 
func didPressDelete() { 
//handlePress 
} 
에 버튼 목표를 연결
관련 문제