简体   繁体   English

如何在Swift 3.0的uitableviewcell中合并两个结构并打印两个结构的值

[英]how can I merge two structs and print values of both in uitableviewcell in swift 3.0

Below are my two structs, and I want to print values from both structs in UITableViewCell 下面是我的两个结构,我想从UITableViewCell中的两个结构中打印值

struct MainCell: Decodable
{
    let job_id: String
    let job_desig: String
    let job_desc: String
    let job_location: String
    let job_emp_gender: String
    let job_skills: String
    let company_id: Int

}
struct Company: Decodable{
    let company_id: Int
    let company_name: String
}

var mainCellData = [MainCell]()
var companyData = [Company]()

- TableView Methods -TableView方法

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return mainCellData.count + companyData.count
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell:JobDetails_TableViewCell = tableView.dequeueReusableCell(withIdentifier: "jobCell") as! JobDetails_TableViewCell

        for jobs in mainCellData {
            cell.lblDesig.text = jobs.job_desig
            cell.lblDesc.text = jobs.job_desc
            cell.lblLocation.text = jobs.job_location
            cell.comName.text = jobs.name.company_name
        }

        return cell
    }

As I want to print job_desig, job_desc and job_location from my first struct (struct MainCell: Decodable) and company_name from my second struct (struct Company: Decodable) 因为我想从第一个结构(结构MainCell:Decodable)中打印job_desig,job_desc和job_location,从第二个结构(结构公司:Decodable)中打印company_name

Can anybody help me with my issue? 有人可以帮我解决我的问题吗?

  1. Your numberOfRowsInSection doesn't match your requirement. 您的numberOfRowsInSection与您的要求不符。
  2. Get rid of the for loop in cellForRowAt . 摆脱cellForRowAt中的for循环。
  3. You don't need to merge anything. 您无需合并任何内容。 You need to look up the company name based on its id. 您需要根据其ID查找公司名称。

Your code should look like this: 您的代码应如下所示:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return mainCellData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:JobDetails_TableViewCell = tableView.dequeueReusableCell(withIdentifier: "jobCell") as! JobDetails_TableViewCell

    let data = mainCellData[indexPath.row]
    cell.lblDesig.text = data.job_desig
    cell.lblDesc.text = data.job_desc
    cell.lblLocation.text = data.job_location
    cell.comName.text = companyData.first { $0.company_id == data.company_id }?.company_name

    return cell
}

The real trick is getting the company name. 真正的诀窍是获得公司名称。 The idea is that you have data.company_id which is of course the company_id for the row being displayed. 这个想法是您有data.company_id ,它当然是要显示的行的company_id。 You need to iterate through the companyData array and find a Company that has the same company_id . 您需要遍历companyData数组并找到具有相同company_idCompany When a match is found, get the company_name from that matching company. 找到匹配项后,从该匹配公司获取company_name

The code companyData.first { $0.company_id == data.company_id }?.company_name means: 代码companyData.first { $0.company_id == data.company_id }?.company_name表示:

Iterate through the companyData array and find the first entry where the company_id equals data.company_id . 遍历companyData数组,并找到第一个条目,其中company_id等于data.company_id If a match is found, return its company_name . 如果找到匹配项,则返回其company_name

You don't need to add count for numberOfRow, It is not good practice to manage two array for tableview you should combine in one struct like below 您不需要为numberOfRow添加计数,管理两个用于tableview的数组不是一个好习惯,您应该将其合并到一个结构中,如下所示
You can create one struct to maintain your all datasource 您可以创建一个结构来维护所有数据源

struct MainStruct {

    var mainCellData :MainCell
    var companyData :Company?

}

EDIT 编辑

and fill your data source like this in viewDidLoad or whenever you got your array 并在viewDidLoad中或每次获取数组时填充您的数据源

var data = [MainStruct]()
for object in mainCellData {
   let obj = companyData.first{$0.company_id == object.company_id }
    data.append(MainStruct(mainCellData: object, companyData: obj))
}

Now 现在

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
}

and

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:JobDetails_TableViewCell = tableView.dequeueReusableCell(withIdentifier: "jobCell") as! JobDetails_TableViewCell

    let data = data[indexPath.row]
    cell.lblDesig.text = data.mainCellData.job_desig
    cell.lblDesc.text = data.mainCellData.job_desc
    cell.lblLocation.text = data.mainCellData.job_location
    cell.comName.text = data.companyData?.company_name

    return cell
}

Hope it is helpful 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM