iOS Archives

These posts are syndicated from my iOS Dev Diary over at pinkstone.co.uk.

The format is slightly different with more code snippets and less explanations. I had originally designed it as a notebook in iOS Development – now you can follow along if you’re interested.

How to dequeue UITableViewCells

Sometimes Xcode does a bit of magic behind the scenes which I don’t really understand. Consider this standard table view method to display cells: – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@”Cell”]; // populate your cell return cell; } Where do we actually ever create a UITableViewCell? All we’re asking here […]

How to hide (and show) the UISearchBar in a UITableView

If you add a UISearchBar to your table view (see previous article) it’s just sitting there by default. Chances are you’d like to hide it when your table view first loads. You can do this by adding the following code to your viewWillAppear method: – (void)viewWillAppear:(BOOL)animated { // scroll the search bar off-screen CGRect newBounds […]

How to create a searchable UITableView

The ingredients for a search function in a UITableView are more involved than just displaying a simple search field. First we need the standard UITableView. Next we need a UISearchBar which can be added to the top of the table view. But displaying the actual search results is something called the UISearchDisplayController. This process isn’t […]

How to fix “this class is not key value coding-compliant” after accidentally adding an IBOutlet instead of an IBAction

It has happened to us all: you’re in a storyboard, you’re using the Assistant Editor, you want to control-drag from a button into your code and create an action. But sadly you forget to select “Action” from the drop down menu and instead create an outlet. No biggie you think, deleting the outlet code. You […]

How to convert an NSString into an integer

Imagine you had an NSString and wanted to save it as an integer value. You can use the intValue method for that: NSString *myString = @”47″; int myInt = [myString intValue]; To do the reverse, you can use the initWithFormat method: int myInt = 32; NSString *myOtherString = [[NSString alloc]initWithFormat:@”%i”, myInt];

How to loop through every element in an NSArray

We can use something called Fast Enumeration for this, using a modified for loop: // build an array NSArray *myArray = [[NSArray alloc]initWithObjects:@”One”, @”Two”, @”Three”, @”Four”, nil]; // loop through every element (dynamic typing) for (id tempObject in myArray) { NSLog(@”Single element: %@”, tempObject); } You don’t have to use dynamic typing if you know […]

How to generate a random number in iOS

We can use the C function rand() for this: int i = rand()%10+1; NSLog(@”Random Number: %i”, i); This generates an integer between 1 and 10. Alternatively you can use the arc4random function: int i = arc4random() % 10; NSLog(@”Random Number: %i”, i); This will generate a random number between 0 and 9. One thing of […]

How to read the contents of a text file into an NSString

Imagine we had a file called myfile.txt which is a standard text file. On each line we have a new item we’d like to read so that our app can do something with it. Here’s how we do that: // get a reference to our file NSString *myPath = [[NSBundle mainBundle]pathForResource:@”myfile” ofType:@”txt”]; // read the […]