Tim
Tim

Reputation: 1428

Tableview inside a CollectionViewCell

Does anyone have any experience using a tableView inside a collectionViewCell?

I have a tableview inside a collectionViewCell (each is a full screen view). Each collectionViewCell displays its own unique data. You can scroll horizontally between each collectionViewCell. The tableview has a custom tableviewCell with a couple of labels. I'm using a tableview to take advantage of the built in tableview reordering functionality.

The first collectionViewCell loads fine with data correctly showing in the tableview. However, when I scroll between collectionView cells the tableView is empty (showing only placeholder text in the labels) until the collectionViewCell stops scrolling and then the tableview suddenly appears with the correct data.

Any suggestions on how I can ensure the tableview is already populated with correct data before it's displayed? I need it to be already there as the user drags the next collectionViewCell across (just like the background image/title text is already there).

Here's a link to my project so you can see the code.

Any help appreciated!

Upvotes: 1

Views: 510

Answers (2)

Charan Giri
Charan Giri

Reputation: 1097

in collectionviewcontroller replace this method this. Here we can dynamically set values to cell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableViewTimeDisplayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if (cell == nil) {

        NSArray *arrayCellXib = [[NSBundle mainBundle] loadNibNamed:@"tableViewTimeDisplayCell"
                                                              owner:self
                                                            options:nil];

        cell = arrayCellXib[0];//[arrayCellXib objectAtIndex:0];
        [cell setNameOfTimeLabel:cellOrderArray[indexPath.row]];//[cellOrderArray objectAtIndex:1] here we can make it as dynamic index
        [cell setEventNumber:eventNumber];
        [cell startTimer];
    }

    return cell;
}

// i guess the issue is when we are scrolling horizontally Tableview is allocating again and also the collection view. In collection view i have removed code other than reload tableview and pasted in initwithframe. As the view is allocating every time this will happen.

Upvotes: 1

dcorbatta
dcorbatta

Reputation: 1889

I haven't experience doing that.

But I think that you can reduce the problem creating a reference to your cell. So your cell won't be released. You can use an array to do that.

EDIT:

Your problem is that you are using a timer to update your label in the TableViewCell

You can change this:

if (!timer) {
        timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(updateLabels:) userInfo:nil repeats:YES];
    }

for this:

[self updateLabels:nil];

and change your updateLabels: for this:

- (void)updateLabels:(NSTimer *)_timer
{
    if (!timer) {
        timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(updateLabels:) userInfo:nil repeats:YES];
    }

    unitOfTimeLabel.text = nameOfTimeLabel;

    if ([nameOfTimeLabel isEqualToString:@"Days"]) amountOfTimeLabel.text = [self daysRemaining];
    if ([nameOfTimeLabel isEqualToString:@"Hours"]) amountOfTimeLabel.text = [self hoursRemaining];
    if ([nameOfTimeLabel isEqualToString:@"Minutes"]) amountOfTimeLabel.text = [self minutesRemaining];
    if ([nameOfTimeLabel isEqualToString:@"Seconds"]) amountOfTimeLabel.text = [self secondsRemaining];
}

Upvotes: 1

Related Questions