iOS Development

swift – Knowledge coming from the community shouldn’t be seen from the cell in ios uikit

Spread the love


The information I pulled from the community seems as [Project Name.modelName (user ID: id: , title: , body: )]. It would not present up once I put it within the cell. How can I repair this? Thanks

fetch perform

    func fetchPosts(completion: @escaping (Consequence<[Posts],Error>) -> Void){
        let url = URL(string:"https://jsonplaceholder.typicode.com/posts")!
        
        URLSession.shared.dataTask(with: url) { information, res, error in
            guard let information else{ return }
            
            do{
                let posts = strive JSONDecoder().decode([Posts].self, from: information)
                completion(.success(posts))
                print(posts)
                
            }catch{
                completion(.failure(NSError()))
            }
        }.resume()
    }

Community json

[
  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipitnsuscipit recusandae consequuntur expedita et cumnreprehenderit molestiae ut ut quas totamnnostrum rerum est autem sunt rem eveniet architecto"
  }, ...
]

Protocols

protocol UserViewModelOutput: AnyObject{
    func updateView(title:[Posts])
}


protocol PostsService{
    func fetchPosts(completion: @escaping (Consequence<[Posts],Error>) -> Void)
}

TableViewController

class ViewController: UIViewController, UserViewModelOutput {
    
    personal let viewModel: PostViewModel
    var postsArray = [Posts]()
    
    lazy var homeTableView: UITableView = {
        let tableView = UITableView()
        
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.register(HomeTableViewCell.self, forCellReuseIdentifier: HomeTableViewCell.reuseIdentifier)
        tableView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(tableView)
        return tableView
    }()
    
    
    
    
    
    init(viewModel: PostViewModel) {
        self.viewModel = viewModel
        tremendous.init(nibName: nil, bundle: nil)
        self.viewModel.output = self
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been carried out")
    }
    

    
        
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        setupViews()
        fetchUsers()
        view.backgroundColor = .yellow
    }
    
    personal func setupViews() {
        NSLayoutConstraint.activate([
            homeTableView.topAnchor.constraint(equalTo: view.topAnchor),
            homeTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            homeTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            homeTableView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
    }
    
    func fetchUsers(){
        viewModel.fetchPost()
    }
    
    func updateView(title: [Posts]) {
        self.postsArray = title
    }
    
}

extension ViewController:UITableViewDataSource, UITableViewDelegate{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        print(postsArray.rely)
        return postsArray.rely
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = homeTableView.dequeueReusableCell(withIdentifier: HomeTableViewCell.reuseIdentifier, for: indexPath)
        cell.textLabel?.textual content = self.postsArray[indexPath.row].title
        
        return cell
    }
    
    
}

I added the info to the array, however it doesn’t seem and the array rely seems to be 0 once I print.
After I print the incoming json information, it seems as [ProjectName.modelName (userId: id: , title: , body: )]. The place am I making a mistake? Thanks to your assist

Leave a Reply

Your email address will not be published. Required fields are marked *