- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我是 Swift 开发的新手,我正在开发一个将 JSON 文件解析为 UITableView 内的 CustomCell 的项目。我遇到的问题是我的标签是空的(因为我可以选择展开),如果是空的,我不希望它们显示在我的单元格中。这是我的代码:
import UIKit
class ScheduleTableViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var myTableView: UITableView!
var nodeCollection = [Node]()
var service:NodeService!
override func viewDidLoad() {
super.viewDidLoad()
service = NodeService()
service.getNodes {
(response) in
self.loadNodes(response["nodes"] as! NSArray)
}
}
func loadNodes(nodes:NSArray){
for node in nodes {
var node = node["node"]! as! NSDictionary
var field_class_day_value = node["field_class_day_value"] as! String
var field_class_time_start_value = node["field_class_time_start_value"] as! String
var field_class_time_end_value = node["field_class_time_end_value"] as! String
var field_class_flex_header_value = node["field_class_flex_header_value"] as! String
var title = node["title"] as!String
var field_ages_value = node["field_ages_value"] as? String
var field_class_footer_value = node["field_class_footer_value"] as? String
var field_class_flex_footer_value = node["field_class_flex_footer_value"] as! String
var field_class_instructor_nid = node["field_class_instructor_nid"] as? String
if (field_class_day_value == "1"){
field_class_day_value = "Monday"
}else if (field_class_day_value == "2"){
field_class_day_value = "Tuesday"
}else if (field_class_day_value == "3"){
field_class_day_value = "Wednesday"
}else if (field_class_day_value == "4"){
field_class_day_value = "Thrusday"
}else if (field_class_day_value == "5"){
field_class_day_value = "Friday"
}else if (field_class_day_value == "6"){
field_class_day_value = "Saturday"
}else{
field_class_day_value = "Sunday"
}
//convert time
var dataStringStartTime = field_class_time_start_value
var dataStringEndTime = field_class_time_end_value
var dateFormatter = NSDateFormatter()
var dateFormatter2 = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter2.dateFormat = "h:mm a"
let dateValueStartTime = dateFormatter.dateFromString(dataStringStartTime) as NSDate!
let dateValueEndTime = dateFormatter.dateFromString(dataStringEndTime) as NSDate!
let class_time_start_value = dateFormatter2.stringFromDate(dateValueStartTime)
let class_time_end_value = dateFormatter2.stringFromDate(dateValueEndTime)
let class_time_final = "\(class_time_start_value) - \(class_time_end_value)"
let title_final = title.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
var nodeObj = Node(field_class_day_value: field_class_day_value,
class_time_final: class_time_final,
field_class_flex_header_value: field_class_flex_header_value,
title_final: title_final,
field_ages_value: field_ages_value,
field_class_footer_value: field_class_footer_value,
field_class_flex_footer_value: field_class_flex_footer_value,
field_class_instructor_nid: field_class_instructor_nid)
nodeCollection.append(nodeObj)
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nodeCollection.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
let node = nodeCollection[indexPath.row]
cell.lbl_day_value.text = node.field_class_day_value
cell.lbl_class_time_final.text = node.class_time_final
cell.lbl_flex_header_value.text = node.field_class_flex_header_value
cell.lbl_title.text = node.title_final
cell.lbl_ages_value.text = node.field_ages_value
cell.lbl_footer_value.text = node.field_class_footer_value
cell.lbl_flex_footer_value.text = node.field_class_flex_footer_value
cell.lbl_instructor_nid.text = node.field_class_instructor_nid
return cell
}
}
最佳答案
你可以试一试:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
let node = nodeCollection[indexPath.row]
cell.lbl_day_value.text = node.field_class_day_value
cell.lbl_class_time_final.text = node.class_time_final
cell.lbl_flex_header_value.text = node.field_class_flex_header_value
cell.lbl_title.text = node.title_final
cell.lbl_ages_value.text = node.field_ages_value
cell.lbl_footer_value.text = node.field_class_footer_value
cell.lbl_flex_footer_value.text = node.field_class_flex_footer_value
cell.lbl_instructor_nid.text = node.field_class_instructor_nid
//Get all your labels and check if they are now empty
for view in cell.subviews {
if let label = view as? UILabel {
if label.text!.isEmpty {
label.hidden = true
}
else {
label.hidden = false
}
}
}
return cell
}
此外,作为一种标准做法,当您有这样的 elseif
时,使用 switch 看起来会好很多:
switch field_class_day_value {
case "1":
field_class_day_value = "Monday"
case "2":
field_class_day_value = "Tuesday"
case "3":
field_class_day_value = "Wednesday"
case "4":
field_class_day_value = "Thrusday"
case "5":
field_class_day_value = "Friday"
case "6":
field_class_day_value = "Saturday"
default:
field_class_day_value = "Sunday"
}
此处示例:https://mega.nz/#!Q1xT3YJR!TP_BOCBVt6mCg1YAafo6EHQKIPqYKzT6scU6ZAPtgWg
关于json - swift : Hide Labels if Empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31821946/
所以我有这个表格..有 2 个字段。 “Youtube”和“link”我想做如果你已经填了YouTube,应该这样做: if(!empty($youtube)) { if ($pos === fal
我有一个要验证的输入: 这是 JS: jQuery("#input").live('change', function() { if("#input:not(
假设我在 someNode 中注册了一些带有 id 的小部件(比如 id1、id2)。现在,如果我这样做,domConstruct.empty(someNode),这将通过执行 someNode.in
我需要确定 IQueryable 方法是返回数据,还是像这样将其应用于 RadGrid 的数据源时返回“空”: RadGrid.DataSource = Method(x); if (
我有以下不能正常工作的 $_GET['category'] 也可以等于 0。 if ( empty( $_GET['category'] ) ){ // do something } else
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
这个问题在这里已经有了答案: In C#, should I use string.Empty or String.Empty or "" to intitialize a string? (31
使用下面的内容,我检查每个输入字段是否为空 if ( $("input:empty").length > 0 ) { $(":text[value=]").css('background'
这是有原因的吗?我问是因为如果您需要使用大量空字符,那么您会遇到与使用大量空字符串时相同的情况。 编辑:这种用法的原因是: myString.Replace ('c', '') 因此从 myStrin
我对 Apache POI 有一个大问题。 A B C D 1 text text text text 2
在阅读 MarkLogic 时 Query Performance and Tuning Guide ,我了解了 empty greatest 和 empty least 以及如何将其与 order
我正在使用.NET 3.5。我正在创建一个简单的类,并希望确保处理后的类不应该为 null 或者也不应该是新的.. 所以如果我们可以像这样测试它 Dim objClass as new Class()
我有这个 HTML 页面 foo pre:empty { display: none; } some text here in the pre element 而且我想知道为
我知道我能做到 unless [1].empty? 但是我想知道有没有办法呢? 最佳答案 以及 davidrac 提到的 #any?,以及 ActiveSupport有 #present?这更像是其他
在调试某些东西时,我看到了 STL vector::empty() 实现: bool empty() const {return (size() == 0); } 我相信,每当我们探测
我有一个 NavigationDrawer 来启动 fragment 。我有一个来自 TabPageIndicator 的包含 4 个选项卡的 fragment ,其中 fragment 包含 Lis
我正在使用 Jackson XML 2.8.9,不幸的是我找不到任何方法将空/空集合序列化为空节点。 负责序列化为 XML 的方法: protected byte[] toXml(final Coll
嗨,所有 SO 用户我对 null 和 Empty 的理解有点困惑。我知道 null 是特例,它不等于自身。我想知道如何确定它是空的,你告诉它是空的依据是什么,空也一样 它是基于内存分配还是什么? 如
这个问题在这里已经有了答案: What is the difference between String and string in C#? (66 个答案) 关闭 10 年前。 C# 中的 str
如果要创建一个空的T类型的IEnumerable,可以使用静态泛型方法创建 Enumerable.Empty() 参见 here了解更多信息。 为什么 Microsoft 选择此选项而不是对泛型类型使
我是一名优秀的程序员,十分优秀!