1

In my UITableView, I recently changed the structure of the cell from formerly just putting UILabels in the contentView of the cell, to adding two UIViews (CellFront and CellBack, on top of one another) into the contentView (so I can achieve a sliding effect by sliding the top one off and revealing the lower one) and adding the UILabels to the top UIView.

However, now, for whatever reason, the cells never get init'd and as a result my UITableView is full of blank cells.

My cell gets created as follows (ArticleCell is a subclass of UITableViewCell):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = nil;

    ArticleInfo *articleInfo = [self.fetchedResultsController objectAtIndexPath:indexPath];

    // Checks if user simply added a body of text (not from a source or URL)
    if ([articleInfo.isUserAddedText isEqualToNumber:@(YES)]) {
        CellIdentifier = @"BasicArticleCell";
    }
    else {
        CellIdentifier = @"FullArticleCell";
    }

    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[ArticleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // If the user simply added a body of text, only articlePreview and progress has to be set
    cell.preview = articleInfo.preview;

    // If it's from a URL or a source, set title and URL as well
    if ([articleInfo.isUserAddedText isEqualToNumber:@(NO)]) {
        cell.title = articleInfo.title;
        cell.URL = articleInfo.url;
    }

    return cell;
}

But I set a breakpoint on the initWithStyle method above within the if statement and it never gets called:

enter image description here

What would cause this? I'm deleting the app and building it from scratch every time, so data is definitely being added to the UITableView, but all the cells are blank. And I can tell a bunch of cells are being added as I have disclosure indicators on all of them, and the table view just gets filled with empty cells with the indicators only.

enter image description here

What am I doing wrong?

user212541
  • 1,878
  • 1
  • 21
  • 30
  • Given the info on `dequeueReusableCellWithIdentifier:forIndexPath:` by @vikingosegundo, it sounds as though your `tableView:cellForRowAtIndexPath:` is working as expected. This would seem to suggest that it is something you have changed in your `ArticleCell` class that is causing the blank cells. Providing your implementation of this class might reveal the problem. – Barjavel Apr 14 '13 at 21:02
  • Here are the files relevant to the `ArticleCell` class: http://cl.ly/3t0g0a0a2I3s – user212541 Apr 15 '13 at 19:23

1 Answers1

2

try

ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

instead of

ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

the first one is the old standard way. It will not create a cell for you. While with the second a cell will be created form the storyboard. So if you use storyboards you should use indeed the method you are using now, but it will never go info the if branch, as the cell will never be nil.


when instantiating form storyboard, initWithStyle:reuseIdentifier: is never called. Either set everything up in -initWithCoder: or -layoutSubviews

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • Still never gets called. And I do want a cell created from the storyboard. This all worked fine until I added the two UIView subviews to the UITableViewCell. – user212541 Apr 14 '13 at 18:53
  • so how do you add those? you know, that your class is not working -> you should tell us more about your class ("tell about" aka "show the code") – vikingosegundo Apr 14 '13 at 22:33
  • 1
    Please add the most important bits to the question's text. – vikingosegundo Apr 15 '13 at 19:57
  • I can't open zip here (iOS) you should use a gist or another pasting service – vikingosegundo Apr 15 '13 at 19:58
  • Free apps like Documents by Readdle allow you to open zips fine. I'll work on a gist, though. – user212541 Apr 16 '13 at 17:57
  • why dont you put those few lines in the question? – vikingosegundo Apr 16 '13 at 17:57
  • people dont like to navigate away just to get the full picture of your problem. and sure is u'd suggest to install 3rd party software just to look at your code, prepare to get some down votes. – vikingosegundo Apr 16 '13 at 17:58
  • anyway: i had a look at your code and my edit has the answer: initWithStyle:reuseIdentifier: will not be called when instantiating from nib/storyboard – vikingosegundo Apr 16 '13 at 18:00
  • I was simply telling you that iOS can indeed open zip files, but I provided the gist like you asked. Didn't mean any offense. – user212541 Apr 16 '13 at 18:01
  • I'm not sure that's correct. I initialized from a storyboard the whole time with that method, I only changed UIView. http://stackoverflow.com/questions/9334156/how-to-initialize-a-custom-prototype-style-table-cell-in-ios-5-storyboards – user212541 Apr 16 '13 at 18:03
  • so what are you trying to show me with the link? there cells are not instantiated from the storyboard. `dequeueReusableCellWithIdentifier:` works differently than `dequeueReusableCellWithIdentifier:forIndexPath:` – vikingosegundo Apr 16 '13 at 18:07
  • Where are they instantiated then? – user212541 Apr 16 '13 at 18:09
  • did u have a look on my answer? specially the edit from 7 hours ago? – vikingosegundo Apr 16 '13 at 18:11
  • I didn't, apologies. I'm curious why I need to use those, when in this project: http://cl.ly/1K2n1T1j361K I needn't. – user212541 Apr 16 '13 at 18:14
  • ok, I have to confess I had an error in it. possibly answering from a 4" screen is not the best idea. fixed. – vikingosegundo Apr 16 '13 at 18:14
  • listen: i cannot teach you the differences between manual/nib/storyboard ios5/ storyboard ios6 instantiation in comments. and I dont want to. everything is discussed and explained zillions of times. work yourself through apple documentation, seach google and read on stack overflow. – vikingosegundo Apr 16 '13 at 18:18
  • Okay, thank you, my last question would just be whether or not this is how to do it properly: http://stackoverflow.com/questions/3943801/objective-c-how-do-i-use-initwithcoder-method and how what is my value for `forKey:`? – user212541 Apr 16 '13 at 18:20
  • so you think, as I dont want to explain different kinds of instantiation, I will start to give a full-scale programming course instead? start with reading documentation NOW. start here: https://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/ – vikingosegundo Apr 16 '13 at 18:26