gpt4 book ai didi

json - swift : Hide Labels if Empty

转载 作者:可可西里 更新时间:2023-11-01 02:19:33 33 4
gpt4 key购买 nike

我是 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com