微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

自定义 UITableViewCell 获取 API

如何解决自定义 UITableViewCell 获取 API

自定义 UITableViewCell 获取 API。

有两个问题:

1)。仅最后一个单元格的 UILable 设置为 API 响应文本:Table view with custom table view cell

2)。由于API调用顺序与API响应顺序不同,如何保证每个cell的API调用得到对应的API响应。

import UIKit
import TinyConstraints

extension WatchedCompanyTableViewCell: CompanyDetailDataDelegate {
    func updateCompanyDetail(newCompanyDetail: String) {
        do {
            let companyDetail: [CompanyDetail] = try JSONDecoder().decode([CompanyDetail].self,from: newCompanyDetail.data(using: .utf8)!)
            dispatchQueue.main.async {
                self.testLabel.text = companyDetail[0].company_ticker
            }
            print(companyDetail[0].company_ticker)
        } catch {
            print("Failed to decode company details!")
        }
    }
}

class WatchedCompanyTableViewCell: UITableViewCell {
            
    private var company: Company?
    
    private let companyTickerLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(name: "Avenir-Medium",size: 14)
        label.font = UIFont.boldSystemFont(ofSize: 14)
        return label
    }()
    
    private let companyNameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(name: "Avenir",size: 14)
        return label
    }()
    
    private let testLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(name: "Avenir",size: 14)
        label.backgroundColor = .systemteal
        return label
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let stackView = UIStackView(arrangedSubviews: [companyTickerLabel,companyNameLabel])
        stackView.distribution = .fillEqually
        stackView.axis = .vertical
        stackView.spacing = 3
        contentView.addSubview(stackView)
        stackView.leading(to: contentView,offset: 10)
        stackView.centerY(to: contentView)
        
        contentView.addSubview(testLabel)
        testLabel.width(100)
        testLabel.height(50)
        testLabel.trailing(to: contentView,offset: -10)
    }
    
    public func setCell(_ c: Company) {
        self.company = c
        
        guard self.company != nil else {
            return
        }

        self.companyTickerLabel.text = company!.company_ticker
        self.companyNameLabel.text   = company!.company_name
        
        print(company!.company_ticker)
        APIFunctions.functions.fetchCompanyDetail(companyTicker: self.company!.company_ticker)
    }
    
    override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?) {
        super.init(style: style,reuseIdentifier: Constants.WATCHED_COMPANY_CELL_ID)
        APIFunctions.functions.companyDetailDeleagate = self
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。