twotoasters/TTSwitch

TTSwitch will display nothing as cell accessory

RobinQu opened this issue · 5 comments

Hi,

I am building a settings panel with InAppSettingsKit. I decided to customize a cell with a switch by using TTSwitch.

InAppSettingsKit uses simple UITableView and UITableViewCell, and place the switch control in cell's accessoryView. However, it display nothing if I choose to place a switch that is a subclass of TTSwitch.

Here is my subclass's header file:


#import <UIKit/UIKit.h>
#import <TTSwitch/TTSwitch.h>
#import <TTSwitch/TTSwitchSubclass.h>

@interface IASKSwitch : TTSwitch {
    NSString *_key;
}

@property (nonatomic, retain) NSString *key;

@end

Is there anything I missed for writing a subclass?

On OSX 10.8.4, XCode 5 DP2, iOS6&7

I am not familiar with InAppSettingsKit. What does the implementation of the subclass look like? Can you supply the code where you instantiate the switch?

Sure~

Basically it invoke initWithFrame in tableview's tableView:cellForRowAtIndexPath:

cell.accessoryView = [[[IASKSwitch alloc] initWithFrame:CGRectMake(0, 0, 79, 27)] autorelease];
        [((IASKSwitch*)cell.accessoryView) addTarget:self action:@selector(toggledValue:) forControlEvents:UIControlEventValueChanged];

Full source code : https://github.com/futuretap/InAppSettingsKit/blob/master/InAppSettingsKit/Controllers/IASKAppSettingsViewController.m#L466

IAKSwitch is a subclass of TTSwitch.

The subclass .m file


#import "IASKSwitch.h"


@implementation IASKSwitch

@synthesize key=_key;

- (void)dealloc {
    [_key release], _key = nil;

    [super dealloc];
}


@end

You need too setup the switch to use the images that you want. Here is an example

TTSwitch *switch = [[TTSwitch alloc] initWithFrame:(CGRect){ CGPointZero, { 76.0f, 27.0f } }];
switch.trackImage = [UIImage imageNamed:@"square-switch-track"];
switch.overlayImage = [UIImage imageNamed:@"square-switch-overlay"];
switch.thumbImage = [UIImage imageNamed:@"square-switch-thumb"];
switch.thumbHighlightImage = [UIImage imageNamed:@"square-switch-thumb-highlight"];

Problem solved! Thanks.

I thought TTSwitch should have some default UI.