Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UITableViewCell with Subtitle style

I used this guide: http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder to be able to create my own custom UITableViewCell with a nice background image in interface builder. But I would like the style of the cell to be UITableViewCellStyleSubtitle to still be able to use textLabel and detailTextLabel for my custom cell. Can I in some way set the style of the cell in interface builder?

Now only the textLabel is set when using:

cell.textLabel.text = @"Title";  
cell.detailTextLabel.text = @"Details";

Also I use a transparent image as background, but textLabel has white background. Can I change this in interface builder? Or do you think I should just add my own labels to the custom cell in interface builder and skip the standard textLabel and detailTextLabel?

Thanks for your help!

like image 889
Martin Avatar asked May 27 '10 18:05

Martin


2 Answers

Use Option B. Either fully use your own custom cell, or fully use Apple's styled cell. Trying to mix and match is not forward compatible.

If you want a convenient property for the detailTextLabel, subclass UITableViewCell and return the UILabel you create in the nib. Give it a different name than detailTextLabel.

like image 122
drawnonward Avatar answered Sep 20 '22 10:09

drawnonward


If you want to use detailTextLabel in a custom button, follow these steps:

1. Override UITableViewCell

// MYTableViewCell.h:
@interface MYTableViewCell : UITableViewCell
@end

2. Add a new IBOutlet to your subclass

// MYTableViewCell.m
@interface MYTableViewCell ()

@property (nonatomic, weak) IBOutlet UILabel* detailLabel;

@end

3. Override -detailTextLabel

@implementation MYTableViewCell

- (UILabel*)detailTextLabel
{
    return self.detailLabel;
}

@end

4. Set up interface builder

Set the custom class for your tableview cell to be MYTableViewCell and associate your UILabel with your new IBOutlet. Future calls to detailTextView on your tableview cell will now correctly return the proper text view. In this way, you can mix custom and OS provided tableview cells without worrying about the detailTextView.

like image 44
Sandy Chapman Avatar answered Sep 21 '22 10:09

Sandy Chapman