iosswiftuikitsnapkit

why Xcode do not execute the codes in func tableView?


question: I set several breakpoints inside the function of tableView. However the Xcode didn't execute the code inside the tableView. please tell me how to fix these. I'm new to learn about IOS development and I'm trying to write a demo of Tweeter show page. Looking forward for responses!

enter image description here

Here is code of extension UITablewViewDataSource:

extension WechatMomentViewController: UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.tweetList?.count ?? 0;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let tweetCell = tableView.dequeueReusableCell(withIdentifier: "WechatMomentListCell", for: indexPath) as? WechatMomentListCell else {
            fatalError("there is no WechatMomentList")
        }
        
        let tweet = viewModel.tweetList?[indexPath.row]
        
        for i in tweet?.images ?? [] {
            let flagImage = UIImageView()
            flagImage.sd_setImage(with: URL(string: i.url))
            tweetCell.Images.append(flagImage)
        }
                
        for i in tweet?.comments ?? [] {
            let flagComment = UILabel()
            flagComment.text = "\(i.sender) : \(i.content)"
            tweetCell.comments.append(flagComment)
        }
        tweetCell.senderNick.text = tweet?.sender?.nick
        tweetCell.senderAvatar.sd_setImage(with: URL(string: tweet?.sender?.avatar ?? ""), placeholderImage: UIImage(named: "placeholder.png"))
        tweetCell.content.text = tweet?.content
        
        return tweetCell
    }
}

and here is the code of all ViewController:

import UIKit
import SnapKit
import SDWebImage
import Alamofire

//
class WechatMomentViewController: UIViewController {
    
    let viewModel = WechatMomentViewModel()
    var userAvatar = UIImageView()
    var userProfileImage = UIImageView()
    var userNick = UIButton()
    var TweetCell = UITableViewCell()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        viewModel.delegate = self
        getUserProfile()
        getTweet()
    }
    
    fileprivate func getUserProfile() {
        viewModel.getUserProfile()
        view.addSubview(userProfileImage)
        userAvatar.backgroundColor = UIColor.black
        view.addSubview(userAvatar)
        userAvatar.snp.makeConstraints{ (make) in
            make.height.equalTo(80)
            make.width.equalTo(80)
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview()
        }
        userAvatar.clipsToBounds = true;
        userAvatar.layer.cornerRadius = 10;
        
        view.addSubview(userNick)
        userNick.snp.makeConstraints{ (make) in
            make.width.equalTo(90)
            make.height.equalTo(20)
            make.trailing.equalTo(userAvatar.snp.leading)
            make.centerY.equalTo(userAvatar)
        }
        
        userProfileImage.frame = CGRect(x: 0, y: 0, width: 414, height: 448)
    }
    
    fileprivate func getTweet() {
        viewModel.getTweet()
    }
}

extension WechatMomentViewController: WechatMomentVCProtocol {
    func refreshUI() {
        if let user = viewModel.user,
           let avatar = user.avatar,
           let profileImage = user.profileImage {
            userAvatar.sd_setImage(with: URL(string: avatar))
            userProfileImage.sd_setImage(with: URL(string: profileImage))
            userNick.setTitle(user.nick, for: .normal)
        }
    }

}

extension WechatMomentViewController: UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.tweetList?.count ?? 0;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let tweetCell = tableView.dequeueReusableCell(withIdentifier: "WechatMomentListCell", for: indexPath) as? WechatMomentListCell else {
            fatalError("there is no WechatMomentList")
        }
        
        let tweet = viewModel.tweetList?[indexPath.row]
        
        for i in tweet?.images ?? [] {
            let flagImage = UIImageView()
            flagImage.sd_setImage(with: URL(string: i.url))
            tweetCell.Images.append(flagImage)
        }
                
        for i in tweet?.comments ?? [] {
            let flagComment = UILabel()
            flagComment.text = "\(i.sender) : \(i.content)"
            tweetCell.comments.append(flagComment)
        }
        tweetCell.senderNick.text = tweet?.sender?.nick
        tweetCell.senderAvatar.sd_setImage(with: URL(string: tweet?.sender?.avatar ?? ""), placeholderImage: UIImage(named: "placeholder.png"))
        tweetCell.content.text = tweet?.content
        
        return tweetCell
    }
}

and here is identifire code of WechatMomentListCell:

import Foundation
import UIKit

class WechatMomentListCell: UITableViewCell{
    var content = UILabel()
    var senderAvatar = UIImageView()
    var senderNick = UILabel()
    var Images = [UIImageView()]
    var comments = [UILabel()]
    
    
}

Solution

  • your class should extend UITableViewController not UIViewController . Also you must register you cell.

    class WechatMomentViewController: UITableViewController {
        
            let kCellIdentifier = "CellIdentifier"
        
            override func viewDidLoad() {
                super.viewDidLoad()
                
                
                self.tableView?.register(WechatMomentListCell, forCellReuseIdentifier: kCellIdentifier)
            }
    }