luke luke - 4 months ago 11
Swift Question

hiding tableview cells when the data fetched is nil thus reducing cell size

I have a tableview inside a collection view cell. The tableview displays data which has been fetched. Although, the data fetched is sometimes nil and ends up looking like this:enter image description here

In this case the company has no facebook name. At the moment I'm hard coding the tableview cell count:

var arrayForContactList : NSMutableArray = []
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
setupContactList()
}

func setupContactList() {

if let phoneNumber = selectedCompany.phoneNumber {

let dictionaryForContact = NSDictionary()
dictionaryForContact.setValue("Phone", forKey: "Title")
dictionaryForContact.setValue(phoneNumber, forKey: "Detail")
arrayForContactList.addObject(dictionaryForContact)
}
// And the rest...

self.tableView.reloadData()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6
}


as for displaying the data in the tableview:

let contactArray = ["Phone", "Website", "Email", "Twitter", "Facebook", "Instagram"]

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("CellID") as! ContactCell

cell.contactLabel.text = arrayForContactList[indexPath.row].objectForKey("Title") as? String
cell.contactButton.setTitle(arrayForContactList[indexPath.row].objectForKey("Detail") as? String, forState: .Normal)

return cell
}


What I want to do reduce the size of the tableview by deleting cell(s) if the data = nil. Then, on top of that I want to decrease the size of the collectionview cell that the tableview sits in if cells from the tableview have been removed.

Answer

Since you are using tableview inside Collectionview i don't know whether it will work for you.Any how give it a try.

  • Let's start with numberOfSectionsInTableView in tableview

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 6
    }
    
  • Modify your numberOfRowsInSection method with following code.

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      if section == 0 {
          return selectedCompany.phoneNumber! ? 1 : 0
      }else if section == 1 {
          return selectedCompany.website! ? 1 : 0
      }else if section == 2 {
          return selectedCompany.email! ? 1 : 0
      }else if section == 3 {
          return selectedCompany.twitter! ? 1 : 0
      }else if section == 4 {
          return selectedCompany.facebook! ? 1 : 0
      }else if section == 5 {
          return selectedCompany.instagram! ? 1 : 0
    }
    }
    
  • FYI -Let me explain what we are doing, We gonna make every contact detail as seprate section and every section will have 1 or 0 depending on the availablity of the data

  • Let's jump into cellForRowAtIndexPath

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCellWithIdentifier("CellID") as! ContactCell
       if indexPath.section == 0 {
           cell.contactLabel.text = contactArray[indexPath.row]
           cell.contactButton.setTitle(selectedCompany.phoneNumber!, forState: .Normal)
       }else if indexPath.section == 1 {
           cell.contactLabel.text = contactArray[indexPath.row]
           cell.contactButton.setTitle(selectedCompany.website!, forState: .Normal)
       }else if indexPath.section == 2 {
           cell.contactLabel.text = contactArray[indexPath.row]
           cell.contactButton.setTitle(selectedCompany.email!, forState: .Normal)
       }else if indexPath.section == 3 {
           cell.contactLabel.text = contactArray[indexPath.row]
           cell.contactButton.setTitle(selectedCompany.twitter!, forState: .Normal)
       }else if indexPath.section == 4 {
           cell.contactLabel.text = contactArray[indexPath.row]
           cell.contactButton.setTitle(selectedCompany.facebook!, forState: .Normal)
       }else if indexPath.section == 5 {
           cell.contactLabel.text = contactArray[indexPath.row]
           cell.contactButton.setTitle(selectedCompany.instagram!, forState: .Normal)
       }
       return cell
    }
    
  • That's it now depending on availability of rows in section we gonna add contact details.

  • For instance if we don't have Facebook details then Section 4 will have 0 number of rows if it so while loading section 4 cellForRowAtIndexPath will not be called.

EDIT: Try using return selectedCompany.website!.isEmpty ? 1 : 0 OR some if condition to find the string is empty or not.like this

let str:String!
if str.characters.count != 0 {
    return 1
}else{
    return 0
}

in our case

if selectedCompany.phoneNumber!.characters.count != 0 {
    return 1
}else{
    return 0
}