Showing posts with label objectAtIndex. Show all posts
Showing posts with label objectAtIndex. Show all posts

Wednesday, December 22, 2010

Local Static Variable Lifetime Expectation in Objective-C

Something I learned today, worth remembering for performance reasons. Static variable defined inside a method is actually a global variable, which is visible only inside that method!

What this means is that you can increase your method performance by declaring variable only once and reuse it when needed next time.

Thursday, November 11, 2010

How to Define UITableViewCell reuseIdentifier for loadNibNamed

When you create UITableViewCells, it's important to use reuseIdentified. This lets iOS recycle your table cells, thus improving performance and minimizing memory use. Using it is pretty simple:

static NSString *idCell = @"MyCellId";
cell = (UITableViewCell *)[aTableView dequeueReusableCellWithIdentifier:idCell];
Try to reuse an already existing, but not currently used, cell created earlier with given identifier "MyCellId". If such isn't found, create a new one:
if (cell == nil)
{
    static NSString *nibcell = @"MyCell";
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibCell owner:self options:nil];
    cell = (UITableViewCell *)[nib objectAtIndex:0];
}
This code requires a separate stand-alone XIB, which contains nothing but your UITableViewCell. But how do you define the reuseIdenfier? That property is read-only and created when table cell is created.
reuseIdentifier
A string used to identify a cell that is reusable. (read-only)
@property(nonatomic, readonly, copy) NSString *reuseIdentifier
Open your XIB in Interface Builder, select UITableViewCell object, open "Table View Cell Attributes" (press command-1) and write your hardcoded magical string "MyCellId" into "Identifier" field. Use the same string in your code as reuseIdentifier.

Warning: you should NEVER access objects inside XIB (NIB) by magical index into array! Apple might change "something" in next Xcode release and thus break your code. You should use IBOutlets. You have been warned.

...yes, this is one of those sad "don't do as I do, but as I tell you to" cases...

Thursday, October 28, 2010

EGOTableViewPullRefresh for multiple UITableViews

Wanted to add a "Pull To Refresh" feature similar to Tweetie2, so i looked around to see if there was any reusable code (screenshot from Zattr).

Cocoanetics (ex-Dr. Touch) has a good article explaining"pull to refresh" internal workings, well worth investing the time and effort to read. Recommended! However he's solution was not reusable for me, since I don't have UITableViewController. Fortunately he referenced Enormego's solution using UITableViews, including a walk-through with changes.