ios - Only cellForRowAt indexPath not called all other dataSources and delegates are called -
i trying make framework , preferring in code figured out way main problem facing can't put data in table because cellforrowatindexpath
not getting called.
i have set delegate
, datasource
.
i have added numberofrowsinsection
, returns 10.
i have added heightforrowatindexpath
, returns 10.
i have added cellforforatindexpath
, return cell.
the main problem these other methods called have added print statements in of them except cellforforatindexpath
.
the problem can't try debugging because method not called.
class popcontrol: nsobject, uitableviewdatasource, uitableviewdelegate { let tableview = uitableview() func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) { print("row selected") } func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { print("cell row @ index path called") let cell = uitableviewcell() cell.textlabel?.text = "abcd" cell.detailtextlabel?.text = "efgh" return cell } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { print("number of rows in section called") return 10 } func tableview(_ tableview: uitableview, heightforrowat indexpath: indexpath) -> cgfloat { print("height row @ index path called") return 10 } func maketable(){ let vc = uiapplication.shared.keywindow?.rootviewcontroller?.childviewcontrollers.last vc?.view.backgroundcolor = uicolor.black print(vc) tableview.datasource = self tableview.delegate = self vc?.view.addsubview(tableview) tableview.translatesautoresizingmaskintoconstraints = false tableview.topanchor.constraint(equalto: (vc?.view.topanchor)!, constant: 100).isactive = true tableview.bottomanchor.constraint(equalto: (vc?.view.bottomanchor)!, constant: -20).isactive = true tableview.leftanchor.constraint(equalto: (vc?.view.leftanchor)!, constant: 20).isactive = true tableview.rightanchor.constraint(equalto: (vc?.view.rightanchor)!, constant: -20).isactive = true print("datasource tableview is") print(tableview.datasource.debugdescription.description) print("delegate is") print(tableview.delegate.debugdescription.utf8cstring) }
i using way because want call maketable()
function different class. mean don't have intention of using class uiviewcontroller
or uitableviewcontroller
. want use present view controller table view anywhere in code.
everything works cellforrowatindexpath
not called. mean tableview gets rendered no cells displayed.
the problem might not reusing cell, try reuse uitableviewcell
add follow code in maketable
tableview.register(uitableviewcell.self, forcellreuseidentifier: "cell")
at cellforrowat
func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { // create new cell if needed or reuse old 1 let cell:uitableviewcell = self.tableview.dequeuereusablecell(withidentifier:"cell") uitableviewcell! // set text data model cell.textlabel?.text = "abc" return cell }
Comments
Post a Comment