首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

为什么UITableViewCell的cellForRowAt方法不调用?

UITableViewCell的cellForRowAt方法不调用的原因可能有以下几种:

  1. 数据源未正确设置:在使用UITableView时,需要设置数据源对象,并实现UITableViewDataSource协议中的方法,包括numberOfRowsInSection和cellForRowAt等。如果数据源对象未正确设置,或者这些方法未被正确实现,就会导致cellForRowAt方法不被调用。
  2. 注册cell未完成:在使用UITableView时,需要先注册要使用的UITableViewCell类或Nib文件,通过register(_:forCellReuseIdentifier:)方法进行注册。如果忘记注册或者注册的标识符与cellForRowAt方法中的标识符不匹配,就会导致cellForRowAt方法不被调用。
  3. UITableView的delegate未设置:UITableView的delegate属性需要设置为正确的对象,并实现UITableViewDelegate协议中的方法。如果delegate未设置,或者delegate对象未正确实现相关方法,也会导致cellForRowAt方法不被调用。
  4. UITableView的数据源方法返回值错误:在实现UITableViewDataSource协议中的方法时,需要确保返回正确的数值。例如,numberOfRowsInSection方法需要返回正确的行数,否则cellForRowAt方法可能不会被调用。
  5. UITableView的reloadData方法未调用:如果在数据源更新后未调用UITableView的reloadData方法,就不会触发cellForRowAt方法的调用。
  6. UITableView的estimatedRowHeight属性设置不当:如果UITableView的estimatedRowHeight属性设置为非零值,而同时又使用了自动计算行高的机制(例如使用UITableViewAutomaticDimension),就可能导致cellForRowAt方法不被调用。此时,可以尝试将estimatedRowHeight属性设置为0,或者关闭自动计算行高的机制。

以上是UITableViewCell的cellForRowAt方法不调用的一些可能原因,具体原因需要根据具体情况进行排查。如果以上解决方法无效,可以进一步检查代码逻辑、调试程序,或者提供更多相关信息以便进行进一步分析和解答。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

IOS UITableView UITableViewCell控件

import UIKit class ViewController:UIViewController,UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. let screenRect = UIScreen.main.bounds let tableRect = CGRect(x:0, y:20, width: screenRect.size.width, height:screenRect.size.height - 20) let tableView = UITableView(frame:tableRect) tableView.dataSource = self self.view.addSubview(tableView) } func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) -> Int{ return 20 } func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) -> UITableViewCell { let identifier = “reusedCell” var cell =tableView.dequeueReusableCell(withIdentifier:identifier) if(cell == nil) { cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier:identifier) } cell?.textLabel?.text = “命运负责洗牌,玩牌的是我们自己!” return cell! } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

03

IOS UIRefreshControl刷新控件

import UIKit class ViewController:UIViewController,UITableViewDelegate,UITableViewDataSource{ @IBOutlet weak var tabvLayout:UITableView! var refreshControl = UIRefreshControl() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.automaticallyAdjustsScrollViewInsets = false //添加刷新 refreshControl.addTarget(self, action:#selector(refreshData), for: UIControlEvents.valueChanged) refreshControl.attributedTitle =NSAttributedString(string:”松开后自动刷新”) tabvLayout.addSubview(refreshControl) refreshData() } // 刷新数据 func refreshData() { self.tabvLayout.reloadData() self.refreshControl.endRefreshing() } // MARK:- UITableViewDataSource func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) -> Int { return 10; } func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) -> UITableViewCell { let cell = UITableViewCell(style:UITableViewCellStyle.value1, reuseIdentifier:“newsCell”) let date = NSDate() let timeFormatter = DateFormatter() timeFormatter.dateFormat = “yyy-MM-dd ‘at’ HH:mm:ss.SSS” //(时间格式) let strNowTime = timeFormatter.string(from:date as Date) as String cell.textLabel?.text = strNowTime let rect = CGRect(x:0,y:cell.frame.height-1,width:self.view.frame.size.width,height:1) let label = UILabel(frame:rect) label.backgroundColor = UIColor.lightGray() cell .addSubview(label) return cell; } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

03
领券