2015-08-05 3 views
0

Я совершенно не знаком с Swift development, я работаю над проектом, который анализирует файл JSON на CustomCell внутри UITableView. Проблема, с которой я сталкиваюсь, заключается в том, что у меня есть метки, которые пусты (из-за моей необязательной развертки), и я не хочу, чтобы они отображались в моей ячейке, если они пусты. Вот мой код:Swift: Hide Labels if Empty

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 
    } 


} 

ответ

0

Вы можете дать этому попытку:

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 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

+0

Я забыл установить скрытые = ложь для не пустых этикеток. Это важно из-за повторного использования ячеек. – Caleb

+0

Я пробовал это, но он по-прежнему показывает пустое пространство: – JShuart

+0

Извините за поздний отклик, я попытался бы прикрепить верхнюю и нижнюю часть ячейки представления таблицы и вычесть высоту метки из ограничения каждый раз, когда вы скроете ярлык. – Caleb