Data modeling for the Main page

Dark Mode

Our team spent tones of time, talking with server developers. The API didn’t came out like what we expected. We requested what we wanted and server developers spent times making it.

It was boring to wait, but we tried to be patient and refine some UI. And finally, the server environment for making data models were made. So today, I started to make data logics right away.

My part was to make data models and spread them to the table-veiw of the Main page, which I made it before.

    static func main(tagNum: Int, completion: CompletionForMain?)
    {
        Alamofire.request("http://explog-shz.ap-northeast-2.elasticbeanstalk.com/post/\(tagNum)/list", method: .get).responseJSON { (response) in
            
            let mainModel = try! JSONDecoder().decode(MainDataModel.self, from: response.data!)
            completion!(mainModel)
            
        }
    }

By getting datas from server by Alamofire, I used Codable to make data models. It was first time for me to use codable. So it took a little bit more then I expected.

import Foundation

struct MainDataModel: Codable
{
    var posts: [Posts]
}

struct Posts: Codable
{
    var pk: Int
    var author: Author
    var title: String
    var start_date: String
    var continent: String
}

struct Author: Codable
{
    var username: String
}

Also as I had hard time using codingkeys, I just made the name same as server-side. But I might change it later on.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableViews[currentIndex].dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
        
        cell.selectionStyle = .none
        
        AuthService.main(tagNum: tableView.tag) { (data) in

            let data = data as? MainDataModel
            let number = (data?.posts.count)! - (indexPath.item + 1)
            
            if let realData = data
            {
                cell.dateTitle.text = realData.posts[number].start_date
                cell.nickNameTitle.text = realData.posts[number].author.username
                cell.title.text = realData.posts[number].title
            }
        }
        
        return cell
    }

After finishing modeling datas, I spread datas in table-view with simple logic.

댓글

Please enter your comment!
Please enter your name here