Я пытаюсь создать приложение для социальных сетей, такое как приложение, используя фиктивный API от jsonplaceholder.com
В настоящее время я могу получить и просмотреть заголовок и текст сообщения в виде таблицы. Теперь я хотел бы включить имя пользователя и расположение каждого сообщения в табличном представлении, как в Instagram.
Это API сообщений: https://jsonplaceholder.typicode.com/posts
Это API пользователей : https://jsonplaceholder.typicode.com/users
Это то, что я сделал для получения сообщений
import UIKit
импорт SkeletonView
класс HomeViewController: UIViewController {
@IBOutlet weak var tblPostsTableView: UITableView!
var postsAray = [PostsModel] ()
var usersArray = [UserModel] ()
struct User {
private let name : String?
private let location : String?
}
var userDict: [Int : String] = [:]
override func viewDidLoad() {
super.viewDidLoad()
SkeletonAppearance.default.multilineHeight = 25
SkeletonAppearance.default.multilineSpacing = 0.5
self.view.layoutSkeletonIfNeeded()
self.view.showAnimatedSkeleton()
initViewController()
}
override func viewWillAppear(_ animated: Bool) {
}
override func viewDidLayoutSubviews() {
}
private func initViewController(){
self.tblPostsTableView.delegate = self
self.tblPostsTableView.dataSource = self
self.tblPostsTableView.register(UINib(nibName: PostsTableViewCell().classString(), bundle: nil), forCellReuseIdentifier: PostsTableViewCell().classString())
invokeAPI()
}
private func invokeAPI() {
HomeViewPresenter.shared.getPosts(successHandler: { (respArray) in
self.postsAray = respArray
self.fetchUsers()
}, failureHandler: { (message) in
print(message)
}) { (message, error) in
print(message)
}
}
func fetchUsers(){
HomeViewPresenter.shared.getUsers(withID: nil, successHandler: { (usersArray) in
self.usersArray = usersArray
self.view.hideSkeleton()
for item in self.usersArray {
self.userDict.updateValue(item.name!, forKey: item.id!)
}
print(self.userDict)
self.tblPostsTableView.reloadData()
self.view.hideSkeleton()
}, failureHandler: { (message) in
print(message)
}) { (message, err) in
print(message)
}
}
}
// MARK: - Расширение TableView
расширение HomeViewController: SkeletonTableViewDelegate, SkeletonTableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postsAray.count
}
func collectionSkeletonView(_ skeletonView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier {
return PostsTableViewCell().classString()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: PostsTableViewCell().classString(), for: indexPath) as! PostsTableViewCell
let uid = self.postsAray[indexPath.row].userId
cell.lblUsername.text = self.userDict[uid!]
cell.lblTitle.text = self.postsAray[indexPath.row].title
cell.txtLabel?.text = self.postsAray[indexPath.row].body!
// self.view.hideSkeleton ( ) возвращаемая ячейка
}
}
Я хочу показать имя пользователя и местоположение вместе с каждым сообщением.