iOS - What are best practices for view controller setup for a posts + comments data model? - ios

Forgive me if this question has been asked elsewhere, I was having trouble finding similar questions.
So I have an app where I am displaying a list of posts, and each post has a list of comments, very similar to the way Instagram's home page is laid out.
Current implementation
The way I've implemented it, I'm using a single UITableViewController and I have an array of posts that back my data source. Each post object has it's own array of comment objects. In numberOfRowsInSection I iterate through the posts array and add up all the posts and comments to get the total number of rows. I have two UITableViewCell templates, one for posts and one for comments, and in cellForRowAtIndexPath and estimatedHeightForRowAtIndexPath (and really any function that does anything significant) I have to do a bunch of logic just to get the right model or view for the given index path.
Issues
This setup seems to work okay, but is really difficult to debug and add features to. Especially now that I am trying to implement Pusher updates from the server, I am running into a lot of issues with how complicated the model to view relationship is and how long it takes to update the model before the view can be updated.
Alternative Implementations I've considered
I started reading a little about UICollectonViews and was thinking about using the supplementary views to show comments and having only the posts be regular cells, but I'm not sure if that's actually how collection views work or if it would make this any easier. I've also considered using only one cell template for the post + comments that dynamically adds comment views to itself as necessary, but I'm not even sure that's possible.
Question
Is there an easier way to implement a posts+comments layout that is better suited to the way the data is laid out? This seems like a pretty common problem that's been solved by a lot of people so I'm hoping someone can point me to some best practices.
Edit: An example of what I mean. The part outlined in red is the comment section for the post, outlined in blue. Instagram seems to be making each post a section though, with the poster's name as a floating header, each post being it's own section seems like it would be easier to implement. I had initially tried to implement my app that way but ran into issues with making the section header much larger than default, maybe I should just change my design to better suit sections?

I guess your main problem is that you are using different cells for "sub entities". So each time you have to reload multiple cells when updating one entity.
It doesn't really make a difference whether you chose the table or collection view. (Depending on the layout, but I guess Instagram is a table view too)
To make your view handling better / easier you should create a cell that handles all the properties of your entity. And for showing all comments you should probably switch to a new view controller. Or you could use one section for one post. So you could just reload one section. The post could be the section header and the comments the cells in the section.
So you could access your cells easily. tableData[section].commments[row]. And then you could just insert a new row into the section when a comment is added.
I hope I understood your problem.

Related

How to create a dynamic drop down within a table view?

I need to create a tableview with at least two sections, transportation and hotel. Tapping on either one creates a drop down view showing info on 1 to n examples of the topic selected. You don't know in advance what data is going to be there so it has to be dynamic to present any number of results with some having lengthy descriptions below each example and some not. I've been racking my brain and I can't figure out how to do this. The dynamic nature of this makes it extra difficult. Here is a mock up of what it should look like: click here
Check out this Git-Repo. I am using it inside my app and its working quite well. You can also watch the tutorial for it: https://www.youtube.com/watch?v=22zu-OTS-3M&t=1s
Hope that helps!

How do I make a 'card stack' kind of UICollectionView?

I'd really like to do something similar to Tinder's "pile of cards" interface, but with a UICollectionView.
I would assume that if you need the other cells to dynamically move given the cell you are interacting with, one would be in Edit Mode on a UICollectionView, but I'm not sure.
Please don't reply with specific Cocoapods that do this and "why don't I just use those?"; it goes beyond just that. I have all sorts of content, and this is just one of the layouts / presentation types I need. Sometimes it's a grid. A lot of the controller actions are the same; just the display and interaction is different.
That said, please DO refer me to any code that may already do this, that I could use as a starting point.
Otherwise, I would be grateful to know where I would get started. I'm thinking maybe one of those projects that re-creates Springboard with a collection view??
EDIT: I was looking for something like a card stack, but not like Tinder in that you can interact with the top card before swiping or letting it return. In the end what I was trying to accomplish was similar to the old iPod-style 'Cover Flow' view.
I'll try to answer my own post. I made a few incorrect assumptions. In the end it was a lot less complex than I thought.
To get a collection view whose layout isn't just 'static within a scrollview' but changes dynamically with the contentOffset property, you need to subclass UICollectionViewLayout and make sure first of all you return YES for - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
from there, most of your heavy lifting will be done in the - (void)prepareLayout method.
I basically used the following tutorial as a starting point and kind of followed what they were trying to do (or just downloaded the final project and saw how they did it, and mimicked the same effect), but obviously had a different prepareLayout method:
Ray Wenderlich

IOS table view inside of a table view

I have a table view with expanding cells for each of them. I want to make every expanding cell be a new table inside of the bigger one. Is that possible? I've done the research, someone said that duplicate UITableView cannot put in one file.
For example
dish order1
=================
dish order2
rice
coke
===============
dish order3
which every "dish order #" is a cell of a table, and when you expand one cell, there will be a smaller table appears. I've done all the expanding stuff. But don't know how to put a small table into a bigger one. Thanks
Well if i understand your question correctly, what you search is called nested tableView, here's some examples:
SDNestedTable
ExtensiveCell
CollapseClick
JKExpandTableView
Good luck.
It would likely be impossible to put one table view inside of another table view, and if you did manage to do it, it would be awful. Ask yourself, have you EVER seen this in a commercial app? If not, as a novice iOS developer, you are out of your depth trying to invent new UI paradigms.
Master the existing UI tools first. Don't try to use the UI elements in totally novel ways until you have a strong feel for the standard way to use them. Expect this to take 6 months or more.
Others have suggested a sectioned table view where initially only the first element of a section is visible, but when you tap on it, it expands to add other rows. This works quite well.
Another approach would be to use a master/detail UI, where tapping on one table view cell pushes a new view controller the uses a new table view to display detail information about the previously selected item. There are some use cases where you might have several levels of increasing detail.
I really do not think you want two instance of UITableView, instead what you want is a sectioned UITableView which expands when it's header is tapped. Apple has a good sample code for this here:
UITableView expand/collapse
i have demo for you , just got from github..
https://github.com/mitsBhadeshiya/ExpandingCell
i hope this is helpfull for you...
Have you considered using one UITableView with multiple sections? You can accomplish what you are depicting above with only one tableView.
Since this was a problem for many people in the past, iOS 9 has a thing called Stack View, it does exactly what you are saying.
Check out this article.

Parse PFTableViewController vs regular UITableView with PFObject as data source

I am new to iOS development, and I have been doing a fair amount of research. Unfortunately, as the title of my post suggests, neither possibility seems exactly suited to my needs.
I need a lot of customization with respect to the look and feel of each individual table view cell, and I need for my table to have header sections which are clickable. I would actually like for the section headers to have the drill down quality and not the cells beneath each section header.
To my knowledge, the problem with PFTableViewController is that it doesn't easily allow for multiple sections (even less so if I need for these section headers to be clickable and of a different look and feel from the regular cells.) Furthermore, customizing the PFTableViewCells doesn't seem terribly simple either.
However, if I use the regular UITableView even with Parse as my backend, won't I run into some difficulties with respect to loading, pagination, etc...?
I have a pretty good idea of how I would implement this app with regular UITableViews so I guess here is my question: is it worth it for me to try and figure out all of this PFTableViewController stuff?
I should also mention the fact that my table view controller will not take up the entire view but only half of the screen.
Thanks in advance for any and all advice
It seems that a normal UITableView will suit your needs. If you want to customize the look and feel of individual cells, you can just subclass a UITableViewCell and modify it.
In terms of your needs with the headers, it's hard to say without knowing more about your app, but you want to follow standard user interface conventions. Users don't expect headers to be clickable. Instead, they attempt to traverse information hierarchies by selecting table view cells, which then bring them to new views with more information. Based on your, needs I think that a standard table view controller with some customization is what you want.

How can storyboard handle two segues for a table cell?

I'd like a master/detail kind of UI, but with the option of going to a different detail page depending on the data in the table cell. Can storyboards do this? If so, is there a tutorial or somewhere that explains? As an alternative, if I mix table cell types in the same table, can I define different detail pages that way?
Also, just an observation, does anyone find the Apple tutorials get very complicated very quickly? I'm having trouble trying to get through even with the first iOS app tutorial. So many control clicks and command drags and whatever and to non-intuitive targets. Like, who would expect a drag from a graphical element to a specific line in a source file? It doesn't make any sense.
I think what you're looking for is didSelectRowAtIndexPath. In the .m file for the view controller you have your UITableView on, whenever the user clicks a row, it will trigger the code in didSelectRowAtIndexPath. Typically, you have an if block that checks which row was clicked, and does different things (like display different detail views).
As far as detailed tutorials, I find YouTube to be the easiest to learn from, especially for simple or common tasks. If you search for "Master-Detail Xcode" on there, I'm sure you'll find tons of results ( http://www.youtube.com/watch?v=S_J_r8AKw0k I think was one of the ones I found useful).

Resources