TTTAttributedLabel Delegate didSelectLinkWithURL not getting called - ios

I'm having problem setting up the TTTAttributedLabel within my custom cell. I have already checked many post and still didn't figured how to solve this. (I can see the URL styling at this custom label , but if i click on it nothing happen).
CustomCell.h:
#interface MyCustomCell : UITableViewCell<TTTAttributedLabelDelegate>
#property (nonatomic, strong, readonly) UITapGestureRecognizer *tapRecognizer;
#property (nonatomic, strong, readonly) UILabel *senderLabel;
#property (nonatomic, strong, readonly) UILabel *timeLabel;
#property (nonatomic, strong, readonly) UILabel *dateLabel;
#property (nonatomic, strong) TTTAttributedLabel *customTextLabel;
#end
CustomCell.m:
#implementation MyCustomCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
_dateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_dateLabel.textAlignment = NSTextAlignmentCenter;
_dateLabel.font = [UIFont boldSystemFontOfSize:12.0];
_dateLabel.textColor = [UIColor grayColor];
_dateLabel.text = #"2015-10-10";
_dateLabel.userInteractionEnabled = false;
_senderLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_senderLabel.textAlignment = NSTextAlignmentLeft;
_senderLabel.font = [UIFont boldSystemFontOfSize:14.0];
_senderLabel.textColor = [UIColor whiteColor];
_senderLabel.userInteractionEnabled = false;
_timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_timeLabel.userInteractionEnabled = false;
_timeLabel.textAlignment = NSTextAlignmentCenter;
_timeLabel.font = [UIFont boldSystemFontOfSize:11.0];
_timeLabel.textColor = [UIColor whiteColor];
_customTextLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
_customTextLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
_customTextLabel.delegate = self;
_customTextLabel.backgroundColor = [UIColor clearColor];
_customTextLabel.numberOfLines = 0;
_customTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
_customTextLabel.textColor = [UIColor blackColor];
_customTextLabel.font = [UIFont systemFontOfSize:18.0];
[self.textLabel addSubview:_customTextLabel];
_customTextLabel.userInteractionEnabled = true;
self.imageView.userInteractionEnabled = YES;
self.imageView.layer.cornerRadius = 5.0;
self.imageView.layer.masksToBounds = YES;
}
return self;
}
Thank you for help.

NSString *text = #"Hello this is https://www.google.com";
_customTextLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 100, 300, 20)];
_customTextLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
_customTextLabel.delegate = self;
_customTextLabel.backgroundColor = [UIColor clearColor];
_customTextLabel.numberOfLines = 0;
_customTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
_customTextLabel.textColor = [UIColor blackColor];
_customTextLabel.font = [UIFont systemFontOfSize:18.0];
_customTextLabel.text = text;
[self.view addSubview:_customTextLabel];
And set delegate method of TTTAttributedLabel it works fine please check:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
}

Related

dynamic cell height for custom uitableviewcell not working

I created a custom tableview cell and the code is given below. I am trying to add a new UIView named bottomTextContainer to the contentview of the cell.The bottomTextContainer holds the labels of published date and source of the article. my problem is bottomTextContainer view is not showing inside the cell it is overlapping with the next cell.
ArticleCell.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
#interface ArticleCell : UITableViewCell
#pragma mark- Properties
#property (strong, nonatomic) UILabel *source;
#property (strong, nonatomic) UILabel *publishedDate;
#pragma mark- Methods
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier;
#end
NS_ASSUME_NONNULL_END
ArticleCell.m
#import "ArticleCell.h"
#interface ArticleCell ()
#pragma mark- Private Properties
#property (strong, nonatomic) UIView *bottomTextContainer;
#end
#implementation ArticleCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self createSubViews];
}
return self;
}
- (void)createSubViews{
self.textLabel.numberOfLines = 0;
self.textLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:20];
self.textLabel.textColor = [UIColor blackColor];
self.textLabel.textAlignment = NSTextAlignmentLeft;
self.detailTextLabel.numberOfLines = 0;
self.detailTextLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:16];
self.detailTextLabel.textColor = [UIColor blackColor];
self.detailTextLabel.textAlignment = NSTextAlignmentLeft;
self.bottomTextContainer = [[UIView alloc] init];
[self.contentView addSubview:self.bottomTextContainer];
self.bottomTextContainer.translatesAutoresizingMaskIntoConstraints = false;
self.source = [[UILabel alloc] init];
[self.bottomTextContainer addSubview:self.source];
self.source.textColor = [UIColor blackColor];
self.source.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12];
self.source.textAlignment = NSTextAlignmentLeft;
self.source.translatesAutoresizingMaskIntoConstraints = false;
self.publishedDate = [[UILabel alloc] init];
[self.bottomTextContainer addSubview:self.publishedDate];
self.publishedDate.textColor = [UIColor blackColor];
self.publishedDate.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12];
self.publishedDate.textAlignment = NSTextAlignmentRight;
self.publishedDate.translatesAutoresizingMaskIntoConstraints = false;
}
- (void)layoutSubviews{
[super layoutSubviews];
UILayoutGuide *contentViewLayout = self.contentView.layoutMarginsGuide;
[self.bottomTextContainer.topAnchor constraintEqualToAnchor:self.detailTextLabel.bottomAnchor].active = YES;
[self.bottomTextContainer.leadingAnchor constraintEqualToAnchor:contentViewLayout.leadingAnchor].active = YES;
[self.bottomTextContainer.trailingAnchor constraintEqualToAnchor:contentViewLayout.trailingAnchor].active = YES;
[self.source.topAnchor constraintEqualToAnchor:self.bottomTextContainer.topAnchor constant:10].active = YES;
[self.source.bottomAnchor constraintEqualToAnchor:self.bottomTextContainer.bottomAnchor constant:10].active = YES;
[self.source.leadingAnchor constraintEqualToAnchor:self.bottomTextContainer.leadingAnchor].active = YES;
[self.source.widthAnchor constraintEqualToAnchor:self.bottomTextContainer.widthAnchor multiplier:0.5].active = YES;
[self.publishedDate.topAnchor constraintEqualToAnchor:self.bottomTextContainer.topAnchor constant:10].active = YES;
[self.publishedDate.bottomAnchor constraintEqualToAnchor:self.bottomTextContainer.bottomAnchor constant:10].active = YES;
[self.publishedDate.trailingAnchor constraintEqualToAnchor:self.bottomTextContainer.trailingAnchor].active = YES;
[self.publishedDate.leadingAnchor constraintEqualToAnchor:self.source.trailingAnchor].active = YES;
}
#end
This is the result I am getting. Show
Try this:
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 44;

UITextField - Next Button (2)

I have used all the suggestions on the forum and still can not go from one uitextfield to the other by hitting the next button. Am i missing something or have something out of place. Any help would be great. Thanks.
Here is all of my code:
//.h file
#import <UIKit/UIKit.h>
#interface IncomeTransactionViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *businessField;
#property (strong, nonatomic) IBOutlet UITextField *memoField;
#property (strong, nonatomic) IBOutlet UITextField *amountField;
#end
//.m file
#import "IncomeTransactionViewController.h"
#interface IncomeTransactionViewController ()
#end
#implementation IncomeTransactionViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//Business UITextField
self.businessField = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 280, 40)];
self.businessField.returnKeyType = UIReturnKeyNext;
self.businessField.placeholder = #"Business";
self.businessField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.businessField.adjustsFontSizeToFitWidth = TRUE;
self.businessField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.businessField];
//Memo UITextField
self.memoField = [[UITextField alloc] initWithFrame:CGRectMake(20, 350, 280, 40)];
self.memoField.returnKeyType = UIReturnKeyNext;
self.memoField.placeholder = #"Memo";
self.memoField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.memoField.adjustsFontSizeToFitWidth = TRUE;
self.memoField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.memoField];
//Amount UITextField
self.amountField = [[UITextField alloc] initWithFrame:CGRectMake(20, 400, 280, 40)];
self.amountField.returnKeyType = UIReturnKeyDone;
self.amountField.placeholder = #"Amount";
self.amountField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.amountField.adjustsFontSizeToFitWidth = TRUE;
self.amountField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.amountField];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.amountField) {
[textField resignFirstResponder];
} else if (textField == self.businessField) {
[self.businessField resignFirstResponder];
[self.memoField becomeFirstResponder];
} else if (textField == self.memoField) {
[self.memoField resignFirstResponder];
[self.amountField becomeFirstResponder];
}
return YES;
}
#end
You have to take the <UITextFieldDelegate> in your .h file
#interface IncomeTransactionViewController : UIViewController <UITextFieldDelegate>
and in your .m file you need to assign delegate like this
self.businessField = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 280, 40)];
self.businessField.delegate = self; // add this line
self.businessField.returnKeyType = UIReturnKeyNext;
self.businessField.placeholder = #"Business";
self.businessField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.businessField.adjustsFontSizeToFitWidth = TRUE;
self.businessField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.businessField];
//Memo UITextField
self.memoField = [[UITextField alloc] initWithFrame:CGRectMake(20, 350, 280, 40)];
self.memoField.delegate = self; // add this line
self.memoField.returnKeyType = UIReturnKeyNext;
self.memoField.placeholder = #"Memo";
self.memoField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.memoField.adjustsFontSizeToFitWidth = TRUE;
self.memoField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.memoField];
//Amount UITextField
self.amountField = [[UITextField alloc] initWithFrame:CGRectMake(20, 400, 280, 40)];
self.amountField.delegate = self; // add this line
self.amountField.returnKeyType = UIReturnKeyDone;
self.amountField.placeholder = #"Amount";
self.amountField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.amountField.adjustsFontSizeToFitWidth = TRUE;
self.amountField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.amountField];
This should do it. let me know if it still doesn't work

View has wrong frame when loaded from xib and shown first time

I created a view to show in MapView annotation callout.
If I didn't use xib and instantiate this view with initWithFrame, then there is no problem.
But if I use xib, size shown on map is bigger, than is should be.
Code in view:
#interface CalloutContentView : UIView
#property (nonatomic) Visit *visit;
+ (id)customView;
#end
#import "CalloutContentView.h"
#interface CalloutContentView ()
#property (weak, nonatomic) IBOutlet UILabel *title;
#property (weak, nonatomic) IBOutlet UILabel *subtitle;
#property (weak, nonatomic) IBOutlet UISegmentedControl *sc;
#end
#implementation CalloutContentView
+ (id)customView
{
CalloutContentView *customView = [[[NSBundle mainBundle] loadNibNamed:#"CalloutContentView" owner:self options:nil] lastObject];
if ([customView isKindOfClass:[CalloutContentView class]])
return customView;
else
return nil;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// _title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, 18)];
_title.font = [UIFont boldSystemFontOfSize:15.0];
// _title.textAlignment = NSTextAlignmentLeft;
_title.textColor = [UIColor colorWithWhite:0.199 alpha:1.000];
_title.numberOfLines = 1;
_title.backgroundColor = [UIColor yellowColor];
[self addSubview:_title];
// _subtitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 25.0, w, 14)];
_subtitle.font = [UIFont systemFontOfSize:12.0];
_subtitle.textAlignment = NSTextAlignmentLeft;
_subtitle.textColor = [UIColor colorWithWhite:0.239 alpha:1.000];
_subtitle.backgroundColor = [UIColor clearColor];
[self addSubview:_subtitle];
// _sc = [[UISegmentedControl alloc]initWithItems:#[#"One",#"Two"]];
_sc.frame = CGRectMake(0, self.frame.size.height - 30, w, 30);
[_sc addTarget:self action:#selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
[self addSubview:_sc];
}
return self;
}
# end
And in MapViewController:
- (void)popupMapCalloutView {
// Change this for creating your Callout View
CalloutContentView *customView = [CalloutContentView customView];
// CalloutContentView *customView = [[CalloutContentView alloc]initWithFrame: CGRectMake(0, 0, 10, 90)];
// if you provide a custom view for the callout content, the title and subtitle will not be displayed
_calloutView.contentView = customView;
VisitAnnotationView *bottomPin = _visitAnnotationViews[_idAnn];
bottomPin.calloutView = _calloutView;
[_calloutView presentCalloutFromRect:bottomPin.bounds
inView:bottomPin
constrainedToView:_mapView
permittedArrowDirections:SMCalloutArrowDirectionAny
animated:YES];
}
That's how it look like when xib is used:

Difficulty setting property in custom UITableViewCell

I have a custom UITableViewCell that contains several labels and an image view that can be one of three icons depending on a value in each item represented by the cell. All of my labels are updating properly, but the value of the image name is always nil. The image name is declared as a property of type NSString.
The custom cell is called FavoriteCell and all the values are setting properly except the NSString value "imgName"
FavoriteCell.h:
#interface FavoriteCell : UITableViewCell
#property (nonatomic, strong) UILabel *lblMainTitle;
#property (nonatomic, strong) UILabel *lblGaugeID;
#property (nonatomic, strong) UILabel *lblGaugeLastUpdate;
#property (nonatomic, strong) UILabel *lblGaugeHeight;
#property (nonatomic, strong) UILabel *lblGaugeCFS;
#property (nonatomic, strong) UIImage *bgImage;
#property (nonatomic, strong) UIImageView *goodToGoImage;
#property (nonatomic, strong) NSString *imgName;
#end
FavoriteCell.m:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
bgImage = [UIImage imageNamed:#"tableCellBG.png"];
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIColor *transparentBG = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
CGSize size = self.contentView.frame.size;
//self.backgroundView = [[UIImageView alloc] initWithImage:bgImage];
self.lblMainTitle = [[UILabel alloc] initWithFrame:CGRectMake(8.0, -10, size.width-20, size.height-40)];
[self.lblMainTitle setFont:[UIFont boldSystemFontOfSize:15]];
[self.lblMainTitle setTextAlignment:NSTextAlignmentLeft];
[self.lblMainTitle setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[self.lblMainTitle setBackgroundColor:transparentBG];
self.lblGaugeID = [[UILabel alloc] init];
self.lblGaugeLastUpdate = [[UILabel alloc] initWithFrame:CGRectMake(9, 14, size.width-40, size.height)];
[self.lblGaugeLastUpdate setFont:[UIFont systemFontOfSize:14]];
[self.lblGaugeLastUpdate setBackgroundColor:transparentBG];
self.lblGaugeHeight = [[UILabel alloc] initWithFrame:CGRectMake(8, 45, size.width-40, size.height)];
[self.lblGaugeHeight setFont:[UIFont boldSystemFontOfSize:21]];
[self.lblGaugeHeight setBackgroundColor:transparentBG];
self.lblGaugeCFS = [[UILabel alloc] initWithFrame:CGRectMake(120, 45, size.width-40, size.height)];
[self.lblGaugeCFS setFont:[UIFont boldSystemFontOfSize:21]];
[self.lblGaugeCFS setBackgroundColor:transparentBG];
NSLog(#"IMAGE NAME: %#\n", imgName); // imgName is nil
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 40, 40, 40)];
[imgView setImage:[UIImage imageNamed:imgName]];
[self.contentView addSubview:lblMainTitle];
[self.contentView addSubview:lblGaugeHeight];
[self.contentView addSubview:lblGaugeLastUpdate];
[self.contentView addSubview:lblGaugeCFS];
[self.contentView addSubview:imgView];
self.editingAccessoryType = UITableViewCellAccessoryDetailButton;
}
return self;
}
The values are being set in my table view controller's cellForRowAtIndexPath method shown here. For now, I'm just hard coding a value for testing. The hard coded value is not carrying over to the custom cell.
-(FavoriteCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
FavoriteCell *cell = (FavoriteCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil){
cell = [[FavoriteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
Favorite *fave = [allFavoriteObjects objectAtIndex:indexPath.row];
NSString *currFeet = [NSString stringWithFormat:#"%.02fft", [fave.currentFeet doubleValue]];
NSString *currFlow = [NSString stringWithFormat:#"%dcfs", [fave.currentCFS intValue]];
[cell setImgName:#"thumbsUp.png"];
[cell.lblMainTitle setText:[fave stationRealName]];
[cell.lblGaugeID setText:[fave stationIdentifier]];
[cell.lblGaugeCFS setText:currFlow];
[cell.lblGaugeHeight setText:currFeet];
return cell;
}
Have I missed something obvious? Thanks!
that is normal because you code is running when you don't have set the imgName, you set it then that your init code is run.
fox fix it remove the code UIImage and the NSString from you custom cell and set it into your imageView then when you make the cell into the -tableView:cellForRowAtIndexPath: by imageView.image = [UIImage imageNamed:#"thumbsUp.png"];
Well, upon first glance, the moment at which you are saying imgName logs as nil seems to be because you are not actually setting the imgName until after initWithStyle is called. Or is there something I'm missing about what you're expecting to happen?

Why are my cells appearing blank in my UITableView?

I have a little bit of an odd structure for my UITableView. Basically, I have ArticleCell objects (subclass of UITableViewCell) that make up the table, and each ArticleCell is comprised of a "front" view and a "back" view. The front view has all the labels and whatnot the user sees, while the back has two icons to the left and right. This two view idea is so the user can slide the top view to the right or left to quickly select an option (kind of like in Reeder).
I've implemented this slightly in the storyboard, but mostly in code. The only thing I did in the storyboard was have the layout of the UITableViewController done, and I named the identifier for the prototype cell (identifier: "ArticleCell").
Other than the storyboard, as I said, everything's done in code. I get the cell's information from Core Data, and construct the cell by setting the article to the cell's article property, which then sets that article to CellFront UIView's article property. Because there's two kinds of cell layouts depending on the type of article the cell holds, in CellFront it checks when kind of cell it is (a property called isUserAddedText) and creates the layout accordingly.
But as I said, nothing's showing up when the app loads, all the cells load and I can tap them to go to their content, but the cells themselves are blank.
The relevant code is as follows:
Cell Data Source:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ArticleInfo *articleInfo = [self.fetchedResultsController objectAtIndexPath:indexPath];
static NSString *CellIdentifier = #"ArticleCell";
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ArticleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.article = articleInfo;
return cell;
}
In ArticleCell.m, I overrode article's set method so when the data source method above calls it, it could set the view's article property as well.
- (void)setArticle:(ArticleInfo *)article {
_article = article;
self.cellFront.article = article;
}
I also created the CellFront and CellBack UIViews in the ArticleCell.m file:
- (void)awakeFromNib {
[super awakeFromNib];
self.cellBack = [[CellBack alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 80)];
[self.contentView addSubview:self.cellBack];
self.cellFront = [[CellFront alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 80)];
[self.contentView addSubview:self.cellFront];
}
CellFront.m then calls the following method inside its initWithFrame: method, which sets up the labels depending on the kind of article and adds them to the subview.
- (void)addControlsToView {
if ([self.article.isUserAddedText isEqualToNumber:#YES]) {
UILabel *preview = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 280, 70)];
preview.text = self.article.preview;
preview.numberOfLines = 4;
preview.font = [UIFont systemFontOfSize:16.0f];
preview.textColor = [UIColor blackColor];
preview.backgroundColor = [UIColor clearColor];
[self addSubview:preview];
}
else {
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 20)];
title.text = self.article.title;
title.font = [UIFont boldSystemFontOfSize:18.0f];
title.textColor = [UIColor blackColor];
title.backgroundColor = [UIColor clearColor];
[self addSubview:title];
UILabel *URL = [[UILabel alloc] initWithFrame:CGRectMake(20, 35, 280, 20)];
URL.text = self.article.url;
URL.font = [UIFont systemFontOfSize:16.0f];
URL.textColor = [UIColor blackColor];
URL.backgroundColor = [UIColor clearColor];
[self addSubview:URL];
UILabel *preview = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 280, 40)];
preview.text = self.article.preview;
preview.numberOfLines = 2;
preview.font = [UIFont systemFontOfSize:16.0f];
preview.textColor = [UIColor grayColor];
preview.backgroundColor = [UIColor clearColor];
[self addSubview:preview];
}
}
That's everything I thought would be relevant to include. Why exactly are all the cells showing up blank?
If you are not using storyboard or NIB's to define your views, awakeFromNib: will never be called.
Here is some documentation
You should try overriding initWithStyle:reuseIdentifier: instead of awakeFromNib:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self != nil) {
// DO STUFF
}
return self;
}
If you are trying to use a storyboard then you are dequeueing the cells incorrectly, replace:
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ArticleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
With:
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
This method will initialize a new cell from the storyboard if there is not one in the queue. The method you were using won't. This means that cell will equal nil sometimes, then you initialize a new cell with the initWithStyle:reuseIdentifier: method, which will not call the awakeFromNib because you are not decoding it from the NIB.
Here is a link to the documentation for this method.
Additional Information
You will also not see the data in the cell because you are setting the label text values in the same block of code that you using to initialize the views. Basically, you need to do:
...
preview.text = self.article.preview;
...
...this part of the addControlsToView method every time the cell is reused, and the initialization of the UILabel only once. I would move the above code into a setter for the article property on CellFront. For example, first declare some properties for the labels
#property (nonatomic, strong) UILabel *preview1Label;
#property (nonatomic, strong) UILabel *titleLabel;
#property (nonatomic, strong) UILabel *URLLabel;
#property (nonatomic, strong) UILabel *preview2Label;
Then something like this to initialize the controls
- (void)initControls {
self.preview1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 280, 70)];
self.preview1.text = self.article.preview;
self.preview1.numberOfLines = 4;
self.preview1.font = [UIFont systemFontOfSize:16.0f];
self.preview1.textColor = [UIColor blackColor];
self.preview1.backgroundColor = [UIColor clearColor];
[self addSubview:self.preview1];
self.title = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 20)];
self.title.text = self.article.title;
self.title.font = [UIFont boldSystemFontOfSize:18.0f];
self.title.textColor = [UIColor blackColor];
self.title.backgroundColor = [UIColor clearColor];
[self addSubview:self.title];
self.URL = [[UILabel alloc] initWithFrame:CGRectMake(20, 35, 280, 20)];
self.URL.text = self.article.url;
self.URL.font = [UIFont systemFontOfSize:16.0f];
self.URL.textColor = [UIColor blackColor];
self.URL.backgroundColor = [UIColor clearColor];
[self addSubview:self.URL];
self.preview2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 280, 40)];
self.preview2.text = self.article.preview;
self.preview2.numberOfLines = 2;
self.preview2.font = [UIFont systemFontOfSize:16.0f];
self.preview2.textColor = [UIColor grayColor];
self.preview2.backgroundColor = [UIColor clearColor];
[self addSubview:self.preview2];
}
Then something like this to set the controls, likely from the setter of the article property.
- (void)setControls {
if ([self.article.isUserAddedText isEqualToNumber:#YES]) {
self.preview1.hidden = NO;
self.preview1.text = self.article.preview;
self.title.hidden = YES;
self.title.text = nil;
self.URL.hidden = YES;
self.URL.text = nil;
self.preview2.hidden = YES;
self.preview2.text = nil;
}
else {
self.preview1.hidden = YES;
self.preview1.text = nil;
self.title.hidden = NO;
self.title.text = self.article.title;
self.URL.hidden = NO;
self.URL.text = self.article.url;
self.preview2.hidden = NO;
self.preview2.text = self.article.preview;
}
}

Resources