Online Book Reader

Home Category

iOS Recipes - Matt Drance [32]

By Root 217 0
event is triggered only when we have detected an end to touches on the control and established the extent of the control rotation. We are able to access the derived value of the currentNumber property value and use that to update the label accordingly. It’s worth noting that the value appears to change before the rotational momentum of the control has stopped. This is because it is triggered before the half-second animation that we use to imply the frictional slowdown of the wheel. We could experiment a little here and alter the effect by triggering an additional event at the end of the momentum animation and using that to update the label.

NumberSpinControl/NumberSpinControl/NumberSpinControlViewController.m

- (void)numberChanged

{

numLabel.text = [NSString stringWithFormat:@"%d", numbers.currentNumber];

}

Though complete in itself, the code we’ve worked through here really covers only the basic elements of a control; we could add many enhancements to make it more configurable. You could increase the number of external properties, color, and images, for example; allow for both vertical and horizontal presentation; or allow for different sequences, such as letters or symbols. The options are limited only by your imagination!


Footnotes

[1] A continuous recognizer actually requires more states. For the full list of states, refer to the iPad developer guide at Apple.com.


Copyright © 2011, The Pragmatic Bookshelf.

Chapter 2

Table and Scroll View Recipes

UIScrollView and its popular subclass UITableView are two powerful and versatile tools for iOS developers. They mask an incredible amount of complexity and save you the time and heartache of having to make a comparable solution yourself—exactly what any API should do.

As the iOS platform has matured, a number of patterns have emerged that have led to redundant work done in many (if not all) projects. The recipes in this section aim to identify and tackle those areas where things could be a little simpler. They’re designed to save you time and effort while staying out of the way of whatever you’re planning to do. Most of the traditional patterns laid out by UIKit are preserved in order to make these recipes easy for you to understand.

Recipe 15 Simplify Table Cell Production

Problem

UIKit provides an efficient reuse mechanism for table view cells, keeping overhead low and minimizing costly allocations that slow down scrolling. Although this mechanism works well to curb resource consumption, it tends to be verbose, repetitive, and, most of all, error prone. This common pattern begs for a solution that minimizes controller code and maximizes reuse across multiple views or even applications.

Solution

A basic UITableView layout, as seen in the iPod and Contacts applications, is simple enough to re-create without causing too many headaches: the cells all use the same boilerplate UITableViewCellStyle. Once we venture outside of this comfort zone, however, our code can get messy rather quickly. Consider a custom cell with two images and a text label. Our ‑tableView:cellForRowAtIndexPath: method may start off like this:

static NSString *CellID = @"CustomCell";

UITableViewCell *cell = [tableView

dequeueReusableCellWithIdentifier:CellID];

if (cell == nil) {

cell = [[[UITableViewCell alloc]

initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:CellID]

autorelease];

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

UIImageView *mainImageView = [[UIImageView alloc] initWithImage:rainbow];

UIImageView *otherImageView = [[UIImageView alloc] initWithImage:rainbow];

CGRect iconFrame = (CGRect) { { 12.0, 4.0 }, rainbow.size };

mainImageView.frame = iconFrame;

iconFrame.origin.x = CGRectGetMaxX(iconFrame) + 9.0;

altImageView.frame = iconFrame;

[cell.contentView addSubview:mainImageView];

[cell.contentView addSubview:otherImageView];

UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];

[cell.contentView addSubview:label];

[mainIcon release];

[otherIcon release];

[label release];

}

return cell;

Note we haven’t even

Return Main Page Previous Page Next Page

®Online Book Reader