Skip to content

Using blocks for different modes of TableView

November 24, 2013

Design Scenario

We have a single Home screen displaying information (in a UITableView)based on user’s preferences.   Lets say there are three modes the home screen can be accessed with.  Now based on each mode,  different information is displayed in cells with the layout of the UITableViewCell itself being  different on each mode. The approaches I have taken to solve this problem are

Option  1:

Use different UIViewController for different mode. This is a simple solution and does not create any further problem in terms of performance. (if  the readers find any problem with this approach please comment it. I will update the post after evaluating it)

Option 2: (The  solution which makes you think it is easy to do)

Use UIViewController’s itself the datasource and delegate of  UITableView to display different information  based on different mode by using Different UITableViewCells (obviously different layout)

Now the problem here is if there are (1+n) different modes , then this approach is a very bad approach because each time you compare in delegate and data source methods what mode the controller is which will affect the usability at least by a negligible amount.

Option 3:

In UIViewController’s viewDidLoad method, determine the mode of the application  and create a separate model controller (derived from NSObject and complying to UITableViewDataSource and/or UITableViewDelegate based on your requirements) for each mode set them as appropriate dataSource and delegate of the UITableView in your view controller.

Verdict:

Option 1 and Option 3 are better solution than Option 2 interms of performance.

Block based solution:

With blocks introduced in iOS 4.0 (nearly three years ago), our approach to solving the above problems can be made much better.

Additional Solution with Blocks:

(Assumption that we have only one section in UITableView)

Block based Solution  for Problem 1:

typedef   NSInteger (^NumberOfRowsInSection)(NSInteger section);

typedef id (^CellCreationForDifferentModes)(UITableView *tableView , NSIndexPath * indexPath);

@interface SPDifferntTypesController ()

{

NumberOfRowsInSection  numberOfRows;

CellCreationForDifferentModes cellcreation;

}

- (void)viewDidLoad

{

if (self.type == SPType1) {

__block SPDifferntTypesController *blocksafeSelf = self;

numberOfRows = ^()

{

return 10;

};

cellcreation =  ^id(UITableView *tableView , NSIndexPath * indexPath)  {

//Create cell and return it

//Type 1 cell

};

}else

{

__block SPDifferntTypesController *blocksafeSelf = self;

numberOfRows = ^()

{

return 5;

};

cellcreation =  ^id(UITableView *tableView , NSIndexPath * indexPath)  {

//Create cell and return it

//Type 2 cell

};

}

}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return numberOfRows(section);

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

return  cellcreation(tableView,indexPath);

}
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: