Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json Swift:如果为空,则隐藏标签_Json_Swift_Uitableview_Labels - Fatal编程技术网

Json Swift:如果为空,则隐藏标签

Json Swift:如果为空,则隐藏标签,json,swift,uitableview,labels,Json,Swift,Uitableview,Labels,我是Swift开发的新手,我正在从事一个项目,该项目将JSON文件解析为UITableView中的CustomCell。我遇到的问题是,我的标签是空的(因为我可以选择展开),如果标签是空的,我不希望它们显示在我的单元格中。这是我的密码: import UIKit class ScheduleTableViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource { @IBOutle

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


}

此处示例:

我忘记为非空标签设置hidden=false。这一点很重要,因为重复使用单元格。很抱歉,响应太晚,我会尝试固定表视图单元格的顶部和底部,并在每次隐藏标签时从约束中减去标签的高度。很抱歉,我不完全确定如何做。我一直试图在github上启动我的项目。@JShuart我可以很快创建一个示例项目来演示。谢谢Caleb,我终于可以上传到github了……这是我的链接