0

I want to display the information about an user into table view, some information is not mandatory so finally some cells may not be on the table view. I have the idea of using an array with cells, because I can have maximum 10 cells, and this will simplify the logic, if I will use standard Apple behavior I need to have to many checks and a complicated logic for table view data source and delegate. It's ok to initialise the cells, put them into an array and to display them after?

Pranav
  • 701
  • 4
  • 18
Adrian Zghibarta
  • 397
  • 2
  • 5
  • 17
  • initialise the cells, put them into an array , Means You make the loop and store the cells in array – srinivas n Aug 03 '16 at 12:35
  • I can imagine making that work if all the cells are static. That is, you don't rely on the table view's cache or its reuse logic. – Phillip Mills Aug 03 '16 at 12:36
  • I'd rather create a method that creates the cells from data, and only save the data in the array – Daniel Aug 03 '16 at 12:40
  • I suggest you do what Rob Napier below answered. You should take a look into having static cells in UITableView which makes it easier to show/hide cells. – Bryan P Aug 03 '16 at 12:53

1 Answers1

3

Sure. For a small number of cells like this, this is fine. You'll still need the data source of course, but it can just pull cells from an array if that works well for you.

You may also be interested in just using a table view configured with static cells in your Storyboard. You can then hide cells you don't want. See UITableView set to static cells. Is it possible to hide some of the cells programmatically?

Community
  • 1
  • 1
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Thanks for the answer, I was thinking that this approach can break the UITableViewCell life cycle in some ways. – Adrian Zghibarta Aug 03 '16 at 12:41
  • No; as long as you're not calling `dequeueReusableCell` you shouldn't have any trouble. Just create the cells explicitly. – Rob Napier Aug 03 '16 at 13:11
  • (Even that's a little strong, you can almost certainly use `dequeueReusableCell` to create them initially if that's convenient. Just don't call that in `cellforItemAtIndexPath`.) – Rob Napier Aug 03 '16 at 13:17