Sathi Reddy Sathi Reddy - 2 months ago 6
Objective-C Question

How to display two types of cells in UITableView without conflicts, but with the following requirement?

I have the following requirement to display data in a tableView as follows:

POST
POST
POST
CHANNEL CHANNEL CHANNEL ...(Each row has 6 channels)
POST
POST
POST
CHANNEL CHANNEL CHANNEL .....


For clarification, see the below image:

enter image description here

For example, if posts count = 20 and channels count = 2

then we have to display like this:

POST
POST
POST
CHANNEL CHANNEL
POST
POST
....


Is it possible to implement this way in Objective- C using Switch statement or any other way without conflicts?

Answer

Easiest way is to use CollectionView inside TableView. This means that you should have UITableviewCells to represent each posts and for channels you should have a UITableViewCell with CollectionView inside. This UITableViewCell cell will implement CollectionView Delegates.


For example, if posts count = 20 and channels count = 2

then you have to display like this:

POST - UITableViewCell
POST - UITableViewCell
POST - UITableViewCell
CHANNELS -> UITableViewCell ->
               [channel1 channel2] -> UICollectionViewCell 
POST - UITableViewCell
POST - UITableViewCell

UITableView[
     UITableViewCell -> Post UI
     UITableViewCell -> Post UI
     UITableViewCell -> Post UI
     UITableViewCell -> CollectionView[
                                         UICollectionViewCell ->Channel1 UI
                                         UICollectionViewCell ->Channel2 UI
                                        ]
]

You don't have to use switch case here. You need a property which is used to differentiate Posts and channels

 UITableViewCell *cell = nil;
    if (data.type ==  DATA_TYPE_POST){
        cell = [tableView dequeueReusableCellWithIdentifier:PostCellIdentifier];
        if(!cell){
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PostCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:ChannelCellIdentifier];
        if(!cell){
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChannelCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
    }