Swift TableView for iOS

后端 未结 2 934
猫巷女王i
猫巷女王i 2021-01-03 14:52

I have referred to this question, and tried to implement the various answers. I\'m doing something dumb, as cellForRow is not being called, and I\'m just getting a blank tab

相关标签:
2条回答
  • 2021-01-03 15:47

    You need to connect Delegate DataSource of TableView, both are connected with the tableview then can only NumberOfRowsInSection and CellForRowAtIndexpath called.

    0 讨论(0)
  • 2021-01-03 15:51

    Here is working code. No need to register cell in -viewDidLoad(). Also UITableView is added as subview in Storyboard.

    If someone has this kind of requirement.

    import UIKit
    
    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
        @IBOutlet weak var tableView: UITableView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.tableView.delegate = self
            self.tableView.dataSource = self
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 10
        }
    
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
            var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as UITableViewCell?
    
            if (cell == nil) {
                cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
            }
    
            cell?.textLabel?.text = NSString(format: "%d", indexPath.row) as String
            return cell!
        }
    }
    
    0 讨论(0)
提交回复
热议问题