如何为含动态内容的TableView正确设置行数避免崩溃?
解决TableView动态+静态单元格共存的崩溃问题
我明白你的痛点——既要保证Firebase加载的用户信息单元格能正常显示(哪怕加载中),又要让那两个静态单元格始终存在,还不能因为数组为空导致崩溃。这里有几个靠谱的解决方案,你可以根据自己的代码习惯选择:
方案一:固定总行数,同一Section区分单元格类型
这种方法最简单,适合你想把所有单元格放在同一个Section的场景:
设置总行数:不管
userModel有没有数据,直接返回1 + 2 = 3(对应1个用户信息单元格 + 2个静态单元格)。如果你的userModel是多条用户数据,就改成userModel.count + 2,后续判断逻辑对应调整即可。在
cellForRowAt中做类型判断:override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { switch indexPath.row { case 0: // 处理用户信息单元格 let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserCell if !userModel.isEmpty { // 加载完成,填充动态数据 let userData = userModel[indexPath.row] cell.avatarImageView.image = userData.avatar cell.usernameLabel.text = userData.username cell.watchTimeLabel.text = userData.watchTime } else { // 加载中,显示占位提示 cell.avatarImageView.image = UIImage(named: "placeholder-avatar") cell.usernameLabel.text = "加载中..." cell.watchTimeLabel.text = "--" } return cell case 1: // 商品静态单元格 let cell = tableView.dequeueReusableCell(withIdentifier: "ProductCell", for: indexPath) as! ProductCell cell.productLabel.text = "我的商品" return cell case 2: // 退出静态单元格 let cell = tableView.dequeueReusableCell(withIdentifier: "LogoutCell", for: indexPath) as! LogoutCell cell.logoutButton.setTitle("退出登录", for: .normal) return cell default: return UITableViewCell() } }数据加载完成后刷新:当Firebase返回数据并更新
userModel后,调用tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)或tableView.reloadData()刷新用户信息单元格。
方案二:分Section管理(更清晰易维护)
如果你的单元格逻辑较复杂,分Section能让代码结构更清晰:
定义Section枚举:
enum TableSection: Int, CaseIterable { case userInfo = 0 case product case logout }设置Section数量和每行的行数:
override func numberOfSections(in tableView: UITableView) -> Int { return TableSection.allCases.count } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { guard let tableSection = TableSection(rawValue: section) else { return 0 } switch tableSection { case .userInfo: // 不管有没有数据,都显示1行(加载中显示占位,加载完成显示内容) return 1 // 若为多条用户数据,改为return userModel.count case .product: return 1 case .logout: return 1 } }按Section处理单元格内容:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let tableSection = TableSection(rawValue: indexPath.section) else { return UITableViewCell() } switch tableSection { case .userInfo: let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserCell if !userModel.isEmpty { let userData = userModel[0] // 单条数据直接取第一个元素 cell.avatarImageView.image = userData.avatar cell.usernameLabel.text = userData.username cell.watchTimeLabel.text = userData.watchTime } else { // 加载中占位 cell.avatarImageView.image = UIImage(named: "placeholder-avatar") cell.usernameLabel.text = "加载中..." cell.watchTimeLabel.text = "--" } return cell case .product: let cell = tableView.dequeueReusableCell(withIdentifier: "ProductCell", for: indexPath) as! ProductCell cell.productLabel.text = "我的商品" return cell case .logout: let cell = tableView.dequeueReusableCell(withIdentifier: "LogoutCell", for: indexPath) as! LogoutCell cell.logoutButton.setTitle("退出登录", for: .normal) return cell } }
关键注意事项
- 永远不要直接访问数组的
indexPath.row而不做判空!不管用哪种方案,都要先检查userModel是否为空,再去取数据。 - Firebase加载是异步操作,一定要在数据返回并更新
userModel后刷新TableView,否则用户看不到最新内容。 - 如果
userModel是多条数据,方案一的行数改为userModel.count + 2,并在case 0..<userModel.count范围内处理动态单元格,剩余行处理静态单元格即可。
这样就能完美解决你遇到的问题:既不会因为数组为空崩溃,又能同时显示动态和静态单元格~
内容的提问来源于stack exchange,提问作者Jordan Lewallen
相关产品推荐
相关产品推荐

