2017-03-11 5 views
1

스토리 보드를 사용하지 않고 프로그래밍 방식으로 tableViewtableViewCell을 디자인했습니다. ViewController 나의 viewDidLoad()이 같은 같습니다tableView Delegate 및 DataSource 메서드를 호출 할 수 없습니다.

tableView.delegate = self 
tableView.dataSource = self 
tableView.register(TicketsTableViewCell.self,forCellReuseIdentifier:cellReuseIdentifier) 
tableView = UITableView(frame: UIScreen.main.bounds, style: .plain) 
self.view.addSubview(tableView) 

을 그리고 내 tableViewCell는 다음과 같은 :

class TicketsTableViewCell: UITableViewCell { 

override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: style, reuseIdentifier: reuseIdentifier) 

    //Other View related stuff 
} 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 
required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 

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

, 내가 그것을 실행할 때, 나는 tableView을 볼 수 있어요되는 것은, 그러나 세포는 아닙니다. 또한, cellForRowAt:에 중단 점을 추가하면 호출되지 않습니다. 내가 잘못하고있는 것은 무엇인가? 재사용 식별자로 잘못하고있는 것이 있습니까? 미리 감사드립니다.

+0

numberOfSectionsInTableView 또는 다른 대리인 메서드 호출을 확인할 수 있습니까? - http://stackoverflow.com/questions/24023613/programmatically-uitableview-using-swift –

+0

데이터 소스 란 무엇입니까? 얼마나 많은 요소가 있습니까? 표에는 이러한 정보가 필요합니다. –

+0

데이터 소스는 사전이므로 'numberOfRowsInSection'도 호출되지 않습니다. @ElTomato –

답변

2

문제는 먼저 delegate을 설정하면 라인 tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)tableView 그래서 먼저 그 라인을 넣어 다시 초기화되어 그 이후 delegatetableViewdatasource을 설정 한 것입니다 위해 도움이 될 수 있습니다 다음과 같이 시도 datasource 및 레지스터 셀.

tableView = UITableView(frame: UIScreen.main.bounds, style: .plain) 
tableView.delegate = self 
tableView.dataSource = self 
tableView.register(TicketsTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier) 
self.view.addSubview(tableView) 
+0

감사합니다. 이것은 효과가 있었다. 내가 만든 작은 실수를 믿을 수 없었다. –

+0

@ shravan.sukumar 환영 인사 :) –

0

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource 
{ 

var tableView : UITableView! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    self.view.backgroundColor = UIColor.whiteColor() 
    self.setUpTableView() 

} 
func setUpTableView() 
{ 
    // Create only one table view. 
    tableView = UITableView(frame: CGRectMake(self.view.frame.size.width/10, self.view.frame.size.height/2, self.view.frame.size.width - self.view.frame.size.width/5, self.view.frame.size.height/2 - 20), style: UITableViewStyle.Grouped) 
    tableView.dataSource = self 
    tableView.delegate = self 
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    tableView.layer.cornerRadius = 10 
    tableView.layer.borderColor = UIColor.blackColor().CGColor 
    tableView.layer.borderWidth = 2 
    self.view.addSubview(tableView) 
} 
//table view data source 

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

    return 3 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell 

    cell.textLabel?.text = "test" 
    cell.textLabel?.numberOfLines = 0 

    return cell 

    } 

}