NUCLiOS IGGridView QuickTip: Register a Cell

Stephen Zaharuk / Tuesday, November 18, 2014

So the IGGridView is all about creating custom cells to display whatever you'd like. But did you know that you don't have to constantly check to see if a cell is available to be dequeued or then create. Instead you can always expect your cell to exist when you dequeue. 

First lets look at the original way to create a cell:

IGGridViewImageCell* cell = [gridView dequeueReusableCellWithIdentifier:@"MyImageCell"];

if(!cell)

   cell = [[IGGridViewImageCell alloc]initWithReuseIdentifier:@"MyImageCell"];

cell.imageView.image = [UIImage imageNamed:@"someImage.png"];

return cell;

With the register method, we can eliminate the check to see if the cell exists and subsequent line that creates the cell. Here's how it works:

When we create a IGGridView, you can register a cell class like this: 

[_gridView registerClass:[IGGridViewImageCell class] forCellReuseIdentifier:@"MyImageCell"]

Now, in the datasource's create cell method, you can do this: 

IGGridViewImageCell* cell = [gridView dequeueReusableCellWithIdentifier:@"MyImageCell"];

cell.imageView.image = [UIImage imageNamed:@"someImage.png"];

return cell;

And thats it!

Hope this was helpful. 

-SteveZ