How to create multiple table views in a view controller?

Hi,

I am new to developing..My aim is to create the home page imitating facebook or instragram.I am not sure which view will be suitable as a parent view.

Please let me know how can I proceed.Thanks in advance.

Either a table view or collection view would probably work fine for that. I would probably start with a collection view (UICollectionView on iOS - you didn't say which platform you're talking about) since it's more flexible.

Hi Junkpiie,

Yeah,ur right..In IOS platform.Is it possible to retrive multiple array values in the UICollection view?because for creating profile of the users and adding comments.As per my understanding, I have to place a UIView inside each collection view cell, Please let me now whether I am correct or not.Thanks in advance.

Is it possible to retrive multiple array values in the UICollection view?because for creating profile of the users and adding comments.


You can divide your content in sections with collection view. Then return appropriate number of sections and rows for section.


I have to place a UIView inside each collection view cell, Please let me now whether I am correct or not.Thanks in advance.


You can use interface builder for adding subviews in the cell.

Hi jsslai,

Thanks for your reply.Sorry I couldnt get your point.Can you please describe about, what you mean by content in sections?As per my understanding I think I should use multiple collection views one below the other?

The view in each cell of a table or collection view can contain other arbitrary views, so you could have nested table views / collection views if necessary. However, unless you need scrolling in the nested views, it may be simpler to just programmatically add a number of subviews (one for each comment), one above the other, to each cell.


I recommend building it without support for multiple comments per post at first. That will get you familiar with how table / collection views work. Then once you have a good grasp of that (most important concept, IMHO, is to understand and enforce the separation between your model and your view) you can add more fancy stuff to each cell such as the variable dynamic number of comments.

Hi junkpile,

Thanks for your reply.As per my understanding first I have to create the table view/collection view, in that customised cells I have to add the profiles,images,and comments of the users.How to have nested table views / collection views because I am currently working in this concept, so I have to do the parent view first.

Do you want to have a structure like this?


- Message 1

* Comment 1

* Comment 2

- Message 2

* Comment 1


If yes, you could have a following model structure and collection view data source.


struct Profile {
    let name: String
    let image: UIImage
}
struct Message {
    var comments: [Message]
    let sender: Profile
}
class ViewController: UIViewController {
    var messages: [Message] = []
}
extension ViewController: UICollectionViewDataSource {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return messages.count
    }
   
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return messages[section].comments.count
    }
}


Then your section header would be the original message and cells would be comments.

Hi jsslai,

Thanks for your reply.I am new to IOS developing. In respose to your answer, my understanding is that, making the collection view as a parent view and in each cell I have to place a UIview in that I can add many subviews like labels,buttons etc.Is it right?please help me out.

Please let me know why can't we use the table view.(just to get knowledge).

hello I an a chinese developer.let's learning togerther! give me five 🙂My english is very poor !,although i want to give you a answer !!!

UICollectionViewCell inherits from UIView so you can add your subviews directly in cell. You can add them from Interface Builder or from the code. I would suggest you search for collection view tutorials to get a better understanding.


You can also use UITableView if vertical list is all you need. With UICollectionView, you can use different layouts for example horizontal or circular.

Hi jsslai,

Thanks for your reply..I understood now.

Hi youcan,

Sure you can help me.

How to create multiple table views in a view controller?
 
 
Q