【问题标题】:display array in a label in swift快速在标签中显示数组
【发布时间】:2018-05-11 06:44:26
【问题描述】:

我想要两个从 Parse 中快速获取的显示对象数据。我尝试过以这种方式使用标签,但它只显示对象中的最后一个元素。请问如何让它显示标签中对象中的所有元素。就像一个元素到一个标签。谢谢

 let query = PFQuery(className: "Questionnaire")
 query.findObjectsInBackground { (objects, error) -> Void in
    if error == nil {

     // There were no errors in the fetch
        if let returnedObjects = objects {
     // var text = ""
     // Objects Array is not nil
     // loop through the array to get each object

             for object in returnedObjects {
                 print(object["question"] as! String)
                 // text.append(object["question"] as! String)
                self.Label.text = (object["question"] as! String)


             }


        }
    }
 }

【问题讨论】:

  • 你可以使用tableview来显示多个标签

标签: swift object parse-platform


【解决方案1】:

您可以在一行中像这样将所有question,分隔符连接起来,您可以将分隔符更改为任何(empty, -,...etc)

if let returnedObjects = returnedObjects {
    self.Label.text = returnedObjects.map {($0["question"] as? String) ?? nil}.compactMap({$0}).joined(separator: ",")
}

【讨论】:

    【解决方案2】:

    为此使用 tableview。

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

    【讨论】:

      【解决方案3】:

      如果使用UILabel很重要

      var concatenatedString = ""
      
      for object in returnedObjects {
      
          concatenatedString += object["question"] as! String
      }
      
      self.Label.text = concatenatedString
      

      【讨论】:

        【解决方案4】:

        您正在遍历数组并将每个值设置为Label.text。但是,设置Label.text 将替换之前标签上的内容。这就是为什么您只能看到最后一项。

        一种解决方案是显示数组的字符串表示:

        self.Label.text = "\(object)"
        

        另一个解决方案是在 Suganya Marlin 建议的表格视图中显示项目。您需要遵守UITableViewDatasource 并实现各种方法。这是guide

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-11-11
          • 1970-01-01
          • 1970-01-01
          • 2018-10-11
          • 2015-11-12
          • 1970-01-01
          • 1970-01-01
          • 2016-01-15
          相关资源
          最近更新 更多