You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS开发求助:TableView原型单元格内容无法完整显示

博客/论坛APP UITableView原型单元格内容无法完整显示问题解决

问题描述

我正在开发一款博客/论坛类APP,首页使用UITableView和Prototype Cell展示其他用户的帖子。运行APP时,原型单元格内的内容无法完整显示,外观类似默认单元格但实际并非如此。修改单元格背景色调试能看到部分效果,但仍不符合预期。实际运行后仅能看到usernameLabel的内容(如Atil、ibrahimmbyrrm),其他内容均不显示,且该问题在我所有项目中都存在。

相关代码

FeedViewController.swift

import UIKit
import Parse

class FeedViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
   
    var postDizisi = [Post]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        
        verileriAl()

    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return postDizisi.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "protCell", for: indexPath) as! FeedTableViewCell
        cell.textView.text = postDizisi[indexPath.row].content
        cell.usernameLabel.text = postDizisi[indexPath.row].username
        return cell
    }
    
    @objc func verileriAl() {
        print("done")
        let query = PFQuery(className: "Post")
        query.addDescendingOrder("createdAt")
        query.findObjectsInBackground { (objects, error) in
            if error != nil {
                self.createAlert(title: "HATA", message: error?.localizedDescription ?? "HATA")
            }else {
                if objects!.count > 0 {
                    self.postDizisi.removeAll(keepingCapacity: false)
                    
                    for object in objects! {
                        if let username = object.object(forKey: "username") as?  String {
                            if let userContent = object.object(forKey: "content") as? String {
                                    let post = Post(username: username, content: userContent)
                                    self.postDizisi.append(post)
                                
                            }
                        }else {
                            print("hata aldık")
                        }
                    }
                    self.tableView.reloadData()
                }
            }
        }
    }
    
    func createAlert(title : String , message : String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        let okButton = UIAlertAction(title: "OK", style: UIAlertAction.Style.default)
        alert.addAction(okButton)
        present(alert, animated: true)
    }
    override func viewWillAppear(_ animated: Bool) {
        NotificationCenter.default.addObserver(self, selector: #selector(verileriAl), name: NSNotification.Name(rawValue: "veriGirildi"), object: nil)
    }

}

UploadViewController.swift

import UIKit
import Parse

class UploadViewController: UIViewController {

    @IBOutlet weak var shareButton: UIButton!
    @IBOutlet weak var usernameLabel: UILabel!
    
    @IBOutlet weak var postTextView: UITextView!
    override func viewDidLoad() {
        super.viewDidLoad()
        usernameLabel.text = PFUser.current()?.username
        
        
    }

    @IBAction func shareButtonClicked(_ sender: Any) {
        let post = PFObject(className: "Post")
        if PFUser.current() != nil {
            post["username"] = PFUser.current()?.username
            post["content"] = postTextView.text
            
            post.saveInBackground { (success, error) in
                if error != nil {
                    let alert = UIAlertController(title: "HAT", message: error?.localizedDescription ?? "HATA", preferredStyle: UIAlertController.Style.alert)
                    let okButton = UIAlertAction(title: "OK", style: UIAlertAction.Style.default)
                    alert.addAction(okButton)
                    self.present(alert, animated: true)
                }else {
                    self.postTextView.text = ""
                    self.tabBarController?.selectedIndex = 0
                    
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "veriGirildi"), object: nil)
                }
            }
        }
        
    }
    
}

解决步骤

  • 确认自定义单元格类关联

    1. 打开Storyboard,选中原型单元格
    2. 在Identity Inspector中,Class选择FeedTableViewCell,Module选择当前项目
    3. 确保单元格Identifier为protCell,与代码中dequeueReusableCell的参数一致
  • 修复Auto Layout约束
    给单元格内的usernameLabel和textView添加完整约束:

    • usernameLabel:添加顶部、左、右约束(距离单元格边缘8-16pt)
    • textView:添加顶部(与usernameLabel底部间距8pt)、左、右、底部约束(距离单元格边缘8-16pt)
    • 检查Storyboard中无约束冲突警告
  • 启用自动行高
    在FeedViewController的viewDidLoad中添加:

    tableView.rowHeight = UITableView.automaticDimension
    tableView.estimatedRowHeight = 100 // 可根据实际内容调整预估高度
    
  • 检查Outlet关联
    打开FeedTableViewCell.swift,确认textView和usernameLabel的IBOutlet已正确连接到Storyboard控件,无断开或错误关联。

  • 验证数据有效性
    在cellForRowAt中添加打印语句,确认内容数据正常:

    print("Content: \(postDizisi[indexPath.row].content)")
    

    若数据为空,检查verileriAl中Parse的字段读取逻辑是否正确。

内容的提问来源于stack exchange,提问作者İbrahim Bayram

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 07:10:23