2016-07-16 2 views
0

신속한 프로그래밍에 익숙하지 않습니다. 나는 하나의 사용자 정의 셀과 tableview을 가지고 있습니다. 페이지 상단에 세그먼트 컨트롤이 있습니다. 내 사용자 정의 셀에는 두 개의 레이블과 텍스트 필드가 있습니다. 페이지가로드 될 때 첫 번째 세그먼트는 세그먼트 컨트롤에서 선택된 상태가되고 테이블 행 수는 5이어야합니다.업데이트 세그먼트 제어 선택 섹션의 행 수

세그먼트에서 두 번째 옵션을 선택하면 여섯 번째 행 즉 여섯 번째 행을로드하고 하나의 텍스트 필드를 숨겨야합니다 두 번째 줄에서. 5 행으로 테이블 뷰를로드 할 수 있습니다. 그리고 사용자가 세그먼트에서 선택하면 6 행이있는 표를 다시로드 할 수 없습니다. 여기 내 코드는

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

let numberOfRowsAtSection: [Int] = [5, 6] 
var selectedOption: Bool = false 
override func viewDidLoad() { 
     super.viewDidLoad() 
     reportTable.delegate = self 
     reportTable.dataSource = self 
} 
func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    var rows: Int = 0 
    if tableView == self.reportTable && selectFromOptions.selectedSegmentIndex == 0 && selectedOption == true { 
     selectedOption = false; 
     if section == 0 { 
      rows = 5 
     } 
    } else if selectFromOptions.selectedSegmentIndex == 1 && selectedOption == true { 
     if section == 1 { 
      rows = 6 
     } 
    } 
return rows 
} 

@IBAction func optionChanges(sender: UISegmentedControl) { 
    switch selectFromOptions.selectedSegmentIndex { 
    case 0: 
     selectedOption = true 
     reportTable.reloadData() 
    case 1: 
     selectedOption = true 
     reportTable.reloadData() 

    default: 
     break; 
    } 
} 

어떻게 달성 할 수 있습니까? 미리 감사드립니다.

답변

0

코드에서 실수로 if section == 1 섹션을 확인했습니다. 하나의 섹션 만 있고 그 인덱스는 항상 0입니다. 중단 점을 selectedOption에 설정하고 단계별로 어떤 값이 전달되고 어떤 코드 경로가 내려 갔는지 확인하여이를 발견 할 수 있어야합니다.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    var rows: Int = 0 
    if tableView == self.reportTable && selectFromOptions.selectedSegmentIndex == 0 && selectedOption == true { 
     selectedOption = false; 
     if section == 0 { 
      rows = 5 
     } 
    } else if selectFromOptions.selectedSegmentIndex == 1 && selectedOption == true { 
     if section == 0 { 
      rows = 6 
     } 
    } 
    return rows 
} 

내가 코드의 전체 맥락이없는,하지만 위의 몇 가지 불필요한 조건이있을 수 있습니다 것 같아 :

나는이 일을한다고 생각합니다. 원하는 결과가 나오지 않습니까?

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    switch selectFromOptions.selectedSegmentIndex { 
    case 0: 
     return 5 
    case 1: 
     return 6 
    default: 
     return 0 
    } 
} 
+0

감사합니다. 섹션 문제 부분을 보지 못했습니다 : p. 위에서 언급 한 것처럼 작동했습니다. – user579911