Olico
Olico

Reputation: 73

How to NOT use dequeueReusableCellWithIdentifier?

I have a small table view, maybe 10 items max, and I do not want to use the dequeueReusableCellWithIdentifier method. The reason is, each time I'm scrolling the table view, I have the first row in place of the last row, which take few second to update back to get update back with the last row.

I'm using a prototype cell and i try to remove the :

JsonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

but it return blank cells in table view.

So here is my code :

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

JsonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell=[[JsonTableViewCell alloc]initWithStyle:
          UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

I also try to keep the

JsonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

and configure the cell like:

if (cell == nil) {
    cell.title.text = titleStrip;
}

but in this case, I've got the prototype cell as it is in the storyboard. It's not blank, but not filled with data.

Anybody can tell me what I am doing wrong?

UPDATE **** 10.10.2014 *******

Here is maybe the all code would help :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return myObject.count;
}

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell";

    JsonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell=[[JsonTableViewCell alloc]initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; }

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); dispatch_async(queue, ^{

    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];

    NSMutableString *text; text = [NSMutableString stringWithFormat:@"%@", [tmpDict objectForKeyedSubscript:title]];

    NSMutableString *detail; detail = [NSMutableString stringWithFormat:@"%@ ", [tmpDict objectForKey:content]];

    NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [[UIImage alloc]initWithData:data];

    NSString *titleStrip = [[text description] stringByReplacingOccurrencesOfString:@""withString:@""];

    NSString *detailStrip = [[detail description] stringByReplacingOccurrencesOfString:@""withString:@""];

    dispatch_sync(dispatch_get_main_queue(), ^{

    cell.titreNews.text = titleStrip;

    cell.detailNews.textAlignment = NSTextAlignmentJustified;

    cell.detailNews.text = detailStrip;

    UIImage *image0 = [UIImage imageNamed:@"video.png"];

    if (img != nil) { cell.imageNews.image = img; } else { cell.imageNews.image = image0; }

    }); });

    [cell setAccessoryType:UITableViewCellAccessoryNone];

    return cell; }

Upvotes: 6

Views: 7420

Answers (2)

Nerrolken
Nerrolken

Reputation: 2005

First of all, welcome to Stack Overflow!

Second, I highly recommend against disabling dequeuing. Apple is famously strict when it comes to expected behaviors, and trying to re-work a basic UI element like a TableView to work in a different way could easily get your app rejected. Plus, dequeuing is a good thing, which helps keep memory usage down and streamlines things in general. It's far, far better to find a way to design your own code so it works with the dequeuing process as it is intended to work.

That being said, as I understand it all you need to do is omit the dequeueReusableCellWithIdentifier line, and it should work. cell will be nil each time, and will thus get re-initialized each time from scratch.

Upvotes: 3

Fruity Geek
Fruity Geek

Reputation: 7381

This block of code is never called because you instantiate a cell before it (cell is never nil).

if (cell == nil) {
    cell.title.text = titleStrip;
}

Don't call the dequeue methods if you aren't using them. Change your method to this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    //Don't even try to dequeue, just create new cell instance
    JsonTableViewCell *cell = [[JsonTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    cell.title.text = titleStrip;
    return cell;
}

Upvotes: 3

Related Questions