如何将数据从变量传递到数组?

时间:2017-09-11 00:12:55

标签: ios swift

在我的应用程序中,我从数据库客户的信息加载。 在检查用户配置文件后,我使用全局变量和发件人传递所有数据。

在我的tableview中我必须显示来自变量的数据...问题是我不能将变量放到数组中,所以我试图实现一个struct方法,但它不起作用。

//
//  SchedaVolontario.swift
//  Amesci
//
//  Created by Gianluca Caliendo on 06/07/17.
//  Copyright © 2017 Amesci. All rights reserved.
//

import UIKit

class SchedaVolontario: UIViewController, UITableViewDataSource, UITableViewDelegate {


    var cognome : String?
    var sede : String?
    var progetto : String?
    var datainizioservizio : String?
    var datafineservizio : String?
    var licenze : String?
    var malattie : String?
    var CodvolontarioString : String?


    struct nomestruct {
        var nome : String?
    }
    struct cognomestruct {
        var cognome : String?
    }
    struct sedestruct {
        var sede : String?
    }
    struct progettostruct {
        var progetto : String?
    }
    struct datainizioserviziostruct {
        var datainizioservizio : String?
    }
    struct datafineserviziostruct {
        var datafineservizio : String?
    }
    struct licenzestruct {
        var licenze : String?
    }
    struct malattiestruct {
        var malattie : String?
    }
    struct codvolontariostruct {
        var CodvolontarioString : String?
    }


    var list = [nomestruct.self, cognomestruct.self, sedestruct.self, progettostruct.self, datainizioserviziostruct.self, datafineserviziostruct.self, licenzestruct.self, malattiestruct.self, codvolontariostruct.self] as [Any]


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


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        cell.textLabel?.text = list[indexPath.row] as? String

        return cell
    }


    override var prefersStatusBarHidden : Bool {
        return true
    }


}

3 个答案:

答案 0 :(得分:0)

是的,您可以将变量放入数组中。最简单的是他们是一个普通的类型。例如:

var list = [nomestruct.nome!, cognomestruct.cognome!, sedestruct.sede!, progettostruct.progretto!, datainizioserviziostruct.datainizioservizio!, datafineserviziostruct.datafineservizio!, licenzestruct.licenze!, malattiestruct.malattie!, codvolontariostruct.codvolontarioString!] as [String]

从最初的声明(不是结构)来判断,你也可以这样做:

var list = [self.sede!,self.progretto!, self.datainizioservizio!, self.datafineservizio!, self.licenze!, self.malattie!, self.codvolontarioString!] as [String]

这不是非常优化的代码,因为A)我不知道你在何时或何时实际设置这些结构值和B)我正在取消引用可选字符串,如果字符串设置为nil,您的代码在尝试访问该字符串时将崩溃。

除非您想在以后再次引用的结构中添加额外的数据或属性,否则我不会对此使用结构。

您应该编辑问题,以便在使用数据填充结构时显示,以及如何执行此操作。

答案 1 :(得分:0)

它不起作用,因为您填充了类型列表,而不是值。您应该包含对tableView的引用,并在每次填充列表时重新加载它。试试这个:

import UIKit

class SchedaVolontario: UIViewController, UITableViewDataSource, UITableViewDelegate {

  var nome: String?
  var cognome : String?
  var sede : String?
  var progetto : String?
  var datainizioservizio : String?
  var datafineservizio : String?
  var licenze : String?
  var malattie : String?
  var codvolontarioString : String?

  var guestInfoList: [String] = []

  // Connect this outlet to your table view in storyboard
  @IBOutlet weak var guestInfoTableView: UITableView!

  override func viewDidLoad() {
    super.viewDidLoad()

    populateGuestInfoList()
    guestInfoTableView.reloadData()
  }

  func populateGuestInfoList() {
    if let nome = nome {
      guestInfoList.append(nome)
    }

    if let cognome = cognome {
      guestInfoList.append(cognome)
    }

    if let sede = sede {
      guestInfoList.append(sede)
    }

    if let progetto = progetto {
      guestInfoList.append(progetto)
    }

    if let datainizioservizio = datainizioservizio {
      guestInfoList.append(datainizioservizio)
    }

    if let datafineservizio = datafineservizio {
      guestInfoList.append(datafineservizio)
    }

    if let licenze = licenze {
      guestInfoList.append(licenze)
    }

    if let malattie = malattie {
      guestInfoList.append(malattie)
    }

    if let codvolontarioString = codvolontarioString {
      guestInfoList.append(codvolontarioString)
    }
  }

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


  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.text = guestInfoList[indexPath.row]
    return cell
  }
}

答案 2 :(得分:0)

这里我已经完成了Code使用struct添加对象并将直接String值传递给字符串数组,看看并告诉我你是否遇到任何问题

1)这里是其编写的代码

//using direct string values
var cognome : String?
var sede : String?
var progetto : String?

cognome = "1"
sede = "2"
progetto = "3"

var listt = [String]()
listt = [cognome!,sede!,progetto!]

//uisng Structs
struct progettostruct {
  static var progetto : String?
}
struct datainizioserviziostruct {
   static var datainizioservizio : String?
}

progettostruct.progetto = "4"
datainizioserviziostruct.datainizioservizio = "5"
listt.append(progettostruct.progetto!)
listt.append(datainizioserviziostruct.datainizioservizio!)

2)在这里,您可以查看控制台输出以及我编写的代码

Enumerable#any?