Google Analytics

Readability

BuySellAds.com

Our DNA is written in Objective-C
Jump

Cells with Switch

When I revisited the settings screen on MyAppSales to add a switch it turned my stomach how I would have added it over a year ago versus to the way I’m doing it now.

The Old Way

Consider the following snipped just to get a similar nausea so that you can appreciate what I am going to show you afterwards. That’s from a random switch in cellForRowAtIndexPath.

NSString *CellIdentifier = @"ServerSectionSwitch";
 
SwitchCell *cell = (SwitchCell *)[tableView dequeueReusableCellWithIdentifier:
		CellIdentifier];
if (cell == nil)
{
	cell = [[[SwitchCell alloc] initWithFrame:CGRectZero reuseIdentifier:
		CellIdentifier] autorelease];
}
 
cell.titleLabel.text = @"Enable on WLAN";
ASiSTAppDelegate *appDelegate = (ASiSTAppDelegate *)[[UIApplication sharedApplication]
		delegate];
 
cell.switchCtl.on = appDelegate.serverIsRunning;
[cell.switchCtl addTarget:appDelegate action:@selector(startStopServer:)
			forControlEvents:UIControlEventValueChanged];
return cell;

So obviously I had created a custom tableview cell, so let’s glance at that as well.

SwitchCell.h

#import 
 
 
@interface SwitchCell : UITableViewCell {
	UISwitch *switchCtl;
	UILabel *titleLabel;
 
}
 
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UISwitch *switchCtl;
 
@end

SwitchCell.m

#import 
 
 
@interface SwitchCell : UITableViewCell {
	UISwitch *switchCtl;
	UILabel *titleLabel;
 
}
 
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UISwitch *switchCtl;
 
@end
 
#import "SwitchCell.h"
 
#define MAIN_FONT_SIZE 16.0
#define LEFT_COLUMN_OFFSET 10.0
 
@implementation SwitchCell
 
@synthesize switchCtl, titleLabel;
 
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = TABLEVIEWCELL_PLAIN_INIT) {
        // Initialization code
		self.selectionStyle = UITableViewCellSelectionStyleNone;
 
		// layoutSubViews will decide the final frame
		titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
		titleLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
		titleLabel.font = [UIFont boldSystemFontOfSize:MAIN_FONT_SIZE];
		[self.contentView addSubview:titleLabel];
 
		switchCtl = [[UISwitch alloc] initWithFrame:CGRectZero];
		[self.contentView addSubview:switchCtl];
    }
    return self;
}
 
 
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
 
    [super setSelected:selected animated:animated];
 
    // Configure the view for the selected state
}
 
- (void)layoutSubviews
{
	[super layoutSubviews];
    CGRect contentRect = [self.contentView bounds];
 
	NSString *text = titleLabel.text;
	CGSize sizeNecessary = [text sizeWithFont:[UIFont systemFontOfSize:
		MAIN_FONT_SIZE]];
 
    CGRect frame = CGRectMake(contentRect.origin.x + LEFT_COLUMN_OFFSET ,
		contentRect.origin.y,  sizeNecessary.width+20.0,  contentRect.size.height);
	titleLabel.frame = frame;
 
	frame = CGRectMake(contentRect.origin.x + contentRect.size.width - 91.0 -
		 LEFT_COLUMN_OFFSET,contentRect.origin.y+
				(contentRect.size.height-27.0)/2.0,
					   94.0, 27.0);
	switchCtl.frame = frame;
}
 
- (void)dealloc
{
	[titleLabel release];
	[switchCtl release];
    [super dealloc];
}
 
@end

Ok, you feel my sickness over this code? Let me explain, these are the reasons why this is crappy coding.

0 COmments »

  1. Dr Touch,
    This is off topic i know,but is it possible to add the tab view shown at the top to be added as a detail view(ie a view which is navigated into from some other view)?I tried but found that although the corresponding views for each tab appear,no further navigation is possible in any of them!

Leave a Comment

You must be logged in to post a comment.