Swift:如果为空则隐藏标签
Posted
技术标签:
【中文标题】Swift:如果为空则隐藏标签【英文标题】:Swift : Hide Labels if Empty 【发布时间】:2015-08-05 01:24:09 【问题描述】:我是 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
【问题讨论】:
【参考方案1】:你可以试试这个:
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
s 时,使用开关看起来会更好:
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
【讨论】:
我忘了为非空标签设置 hidden = false 。这很重要,因为可以重复使用单元格。 抱歉回复晚了,我会尝试固定表格视图单元格的顶部和底部,并在每次隐藏标签时从约束中减去标签的高度。 抱歉,我不完全确定该怎么做。我一直在尝试将我的项目放到 github 上。 @JShuart 我可以快速创建一个示例项目来演示。 感谢 Caleb,我终于可以上传到 github...这是我的链接 github.com/jshuart/CultureShock-v.15.git以上是关于Swift:如果为空则隐藏标签的主要内容,如果未能解决你的问题,请参考以下文章
检查 DateTime 类型的值是不是在视图中为空,如果为空则显示空白