Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; }

like image 494
Olico Avatar asked Oct 06 '14 22:10

Olico


People also ask

Why we use dequeueReusableCellWithIdentifier in iOS?

dequeueReusableCellWithIdentifier: Returns a reusable table-view cell object after locating it by its identifier.

How does dequeueReusableCell work?

When you start to scroll down, the cells that disappear off the top of the screen are placed into the reuse queue. When the new cells appearing from the bottom need to be drawn they are dequeued from the reuse queue instead of initialising new instances, thereby keeping memory usage down.

Why use dequeueReusableCell Swift?

dequeueReusableCellWithIdentifier . If you've ever used a table view before, you probably know what this does. In short, it gets the cell from your Storyboard and allows you to use it when calling tableView(... cellForRowAt indexPath: IndexPath)... .

What is the purpose of reuse identifiers for table view cells?

The reuse identifier is associated with a UITableViewCell object that the table-view's delegate creates with the intent to reuse it as the basis (for performance reasons) for multiple rows of a table view. It is assigned to the cell object in init(frame:reuseIdentifier:) and cannot be changed thereafter.


2 Answers

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;
}
like image 102
Fruity Geek Avatar answered Oct 15 '22 14:10

Fruity Geek


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.

like image 39
Nerrolken Avatar answered Oct 15 '22 15:10

Nerrolken