2

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.

luke
  • 2,743
  • 4
  • 19
  • 43

3 Answers3

1

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
}
Gokul G
  • 2,046
  • 15
  • 22
  • under numberOfRows in section I get 'String' does not conform to protocol BooleanType – luke Jul 29 '16 at 09:59
  • seems to be returning 1 section everytime. EDIT: Tell a lie it works. Forgot to add number of sections. You are a saviour! – luke Jul 29 '16 at 10:31
  • Welcome luke,if you need any help let me know.have a great day.happy coding. – Gokul G Jul 29 '16 at 10:34
  • I don't have that much experience on collection view dynamic height..[this](http://stackoverflow.com/questions/28161839/uicollectionview-dynamic-cell-height) may help.If i found something useful i'll let you know. – Gokul G Jul 29 '16 at 11:04
0
return 6

you need to return amount of cells, if you don't want that many count how many do you need

Lu_
  • 2,577
  • 16
  • 24
  • I know, but even when i return linkArray.count there is no difference in the amount of cells returned. When data is nil the cells are still displayed without any data – luke Jul 29 '16 at 08:47
  • so you are returning .count of wrong thing, make different array with values that you have – Lu_ Jul 29 '16 at 09:03
  • Which is where I'm having problems. How do I create an array that only has none nil values? – luke Jul 29 '16 at 09:14
0

Try by adding following method in your code:

    // when you get all the values call below code

    var arrayForContactList : NSMutableArray

    if let phoneNumber = selectedCompany.phoneNumber! {

        let dictionaryForContact : NSDictionary
        dictionaryForContact.setValue("Phone", forKey: "Title")
        dictionaryForContact.setValue(phoneNumber, forKey: "Detail")
        arrayForContactList.addObject(dictionaryForContact)
    }
    // Likewise do this for all your fields

    self.tableView.reload()


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayForContactList.count
}


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
}
Suhas Arvind Patil
  • 1,732
  • 1
  • 19
  • 31
  • I have modified my answer, give try ! – Suhas Arvind Patil Jul 29 '16 at 09:14
  • I'm getting all sorts of errors. Remember this is inside a collection view cell rather than a view controller which makes it a little more tricky, and complicated. – luke Jul 29 '16 at 09:17
  • so you have do this modification in the collection view array also, Since your table view is the subpart of the collection view. – Suhas Arvind Patil Jul 29 '16 at 09:20
  • I edited my code. Not sure what you mean by modification in the collection view array. The arrayForContactList always seems to return nil now. – luke Jul 29 '16 at 09:55
  • I meant to say that how you displaying the collection view? In the modified code you did not get anything inside the arrayForContactList? make sure you called this after getting response from server. – Suhas Arvind Patil Jul 29 '16 at 09:58
  • yeah It's empty. I tried to call setupContactList in viewdidload of the vc and pass the data through the cells and now get the nsexception error this class is not key value coding-compliant for the key Title.' – luke Jul 29 '16 at 10:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118618/discussion-between-suhas-patil-and-luke). – Suhas Arvind Patil Jul 29 '16 at 10:11