2017-05-02 3 views
0

그래,이 사이트를 내 문제의 해결책을 찾기 위해 수색 해왔다. 하나를 찾았지만 어떤 이유로 든 코드가 의도 한대로 작동하지 않습니다. 내 목표는 테이블 뷰에 섹션이있는 것입니다. 각 섹션 아래에는 각 섹션에있는 셀의 양 (예 : 한 섹션에는 1 개, 다른 섹션에는 세 개의 셀)이 있습니다. 여기 각 셀의 양이 다른 섹션을 만드는 방법

import UIKit 

class cat1: UIViewController, UITableViewDelegate, UITableViewDataSource { 

let proc = [["this"], ["Ye", "it", "will"]] 

let infor = [["info 1"] , ["info 3", "info 2" , "info 4"]] 
let arrayforsec = [ "test", "testtt"] 


override func viewDidLoad() { 


    super.viewDidLoad() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
     } 



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return proc[section].count 
      } 



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


    let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as UITableViewCell 
    let cellText = proc[indexPath.section][indexPath.row] 
    let descriptiontxt = infor[indexPath.section][indexPath.row] 


    cell.detailTextLabel?.numberOfLines = 0 
    cell.detailTextLabel?.lineBreakMode = .byWordWrapping 

    cell.textLabel?.text = cellText 
    cell.detailTextLabel?.text = descriptiontxt 

    return (cell) 
} 

func tableView(_ tableView: UITableView, titleForHeaderInSection section :Int) -> String?{ 
       return arrayforsec[section] 



     } 


func numberOfSectionsInTableView(in tableView: UITableView) -> Int { 

    return proc.count 
} 

} 

내 목표는 2 diffferent 헤더, 테스트 및 testtt

테스트 제목이와 자막 정보 1 등

와 셀을해야합니다을 가지고 내 코드입니다

무엇이 엉망이됩니까? How do I populate two sections in a tableview with two different arrays using swift?

(/ 40 upvotes w 응답)

+0

'UITableViewDataSource' 또는'UITableViewDelegate' 메소드가 호출되고 있습니까? – AdamPro13

+0

네, 그렇게 생각합니다. 표는 첫 번째 섹션과 셀만 채 웁니다. 섹션으로 '테스트', 설명으로 'this', 부제로 '정보 1', 나머지는 표시되지 않습니다. –

+0

@GaryMakin that 's 그가'UITableViewController'를 사용하는 대신 뷰 컨트롤러의 서브 뷰로'UITableView'를 추가했다면 사실이 아닙니다. – AdamPro13

답변

1

당신이 잘못 함수의 이름이 여기

내가 상담 솔루션입니다. 그것은해야한다 : 이것은 당신이 참조 된 문제보다 새로운 스위프트의 버전에서 이름이 바뀌 었습니다

func numberOfSections(in tableView: UITableView) -> Int { 
    return proc.count 
} 

. Swift는 정기적으로 변경되었으므로 기능 및 기호의 이름을 두 번 확인하는 것이 좋습니다.

+0

예! 고마워, 너는 방금 두통에서 나를 구해줬다. –

관련 문제