Skip to content

Creating cells

Aleš Kocur edited this page Apr 17, 2015 · 3 revisions

Creating cells

Create UITableViewCell with xib

#import <TFBasicDescriptedCell.h>`

change subclass

@interface MyCustomCell : TFBasicDescriptedCell

Design your cell in xib.

Height

If you want height of cell to be calculated dynamically by autolayout, setup constraints from up to down. Also for support iOS 7, use wherever you can TFExplicitLabel instead of UILabel. It prevents the text size to be calculated wrong on widescreen devices.

If your cell have static height, implement *+ (NSNumber )height

+ (NSNumber *)height {
    return @44.0;
}

Configuration

For configuration with given data implement -(void)configureWithData:(id)data

- (void)configureWithData:(id)data {

    if ([data isKindOfClass:[NSString class]]) {
        self.titleLabel.text = data;
    } 
}

Action response

For example: if you have two buttons in your cell, define ENUM for each in the header file. Something like this:

typedef NS_ENUM(NSInteger, MyButtonCellActionType) {
    MyButtonCellActionTypeTriggerButton1,
    MyButtonCellActionTypeTriggerButton2
};

and create IBAction method inside .m file and connect it with buttons. In the method verify whether action can be triggered (a form of response is set). Then create TFRowAction and trigger it.

- (IBAction)buttonActions:(id)sender {

    if ([self.rowDescriptor canTriggerAction]) {
        
        if (sender == self.button1) {
            [self.rowDescriptor triggerAction:[TFRowAction actionWithSender:sender actionType:MyButtonCellActionTypeTriggerButton1]];
        } else if (sender == self.button2) {
            [self.rowDescriptor triggerAction:[TFRowAction actionWithSender:sender actionType:MyButtonCellActionTypeTriggerButton2]];
        }
    }
}

You can pass some object as sender like cell, button, etc.

Now you can set appropriate actions in you controller.

row = [TFRowDescriptor descriptorWithRowClass:[MyButtonCell class] data:nil tag:nil];
[row setActionBlock:^(TFRowAction *action) {
        
    if (action.actionType == MyButtonCellActionTypeTriggerButton1) {
        [[[UIAlertView alloc] initWithTitle:@"Action block" message:@"This message was triggered by block" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: nil] show];
     }
}];

More about implementing actions can be found in Table description article.

Clone this wiki locally