iOS customize tableviewcell, the separator length is wrong - ios

I have customized a tableview cell programmatically.
header file:
#import <UIKit/UIKit.h>
#class LoadingCell;
typedef NS_ENUM(NSUInteger, LoadingCellStyle) {
LoadingCellStyleSelection,
LoadingCellStyleTextField,
LoadingCellStyleImagePicker,
};
#interface LoadingCell : UITableViewCell
#property (nonatomic, strong) UIImageView* leftImageView;
#property (nonatomic, strong) UILabel* titleLabel;
#property (nonatomic, strong) UILabel* detailLabel;
#property (nonatomic, assign) LoadingCellStyle style;
-(instancetype)initWithStyle:(LoadingCellStyle)style;
#end
implementation file:
#import "LoadingCell.h"
#define itemHeight 44
#implementation LoadingCell
-(instancetype)initWithStyle:(LoadingCellStyle)style
{
self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"loadingStats"];
self.style = style;
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.leftImageView];
if (self.style == LoadingCellStyleSelection) {
}
return self;
}
#pragma mark property setter
-(UILabel *)titleLabel
{
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.textColor = COLOR_TEXT_GRAY;
_titleLabel.font = [UIFont systemFontOfSize:13];
}
return _titleLabel;
}
-(UILabel *)detailLabel
{
if (!_detailLabel) {
_detailLabel = [[UILabel alloc] init];
_detailLabel.textColor = COLOR_TEXT_GRAY;
}
return _detailLabel;
}
-(UIImageView *)leftImageView
{
if (!_leftImageView) {
_leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"star"]];
}
return _leftImageView;
}
#pragma mark layouts
-(void)layoutSubviews
{
CGRect imageframe = CGRectMake(k_Margin, 0, itemHeight, itemHeight);
[self.leftImageView setFrame:imageframe];
[self.titleLabel setFrame:CGRectMake(CGRectGetMaxX(imageframe), 0, 100, itemHeight)];
[self.detailLabel setFrame:CGRectMake(CGRectGetMaxX(self.titleLabel.frame), 0, kScreen_Width-CGRectGetMaxX(self.titleLabel.frame)-50, itemHeight)];
}
#end
And the result is shown like
As you see, the cell separator is short on the right side.
self.separatorInset = UIEdgeInsetsMake(0, 0, 0, -100);
this method is able to work around but I think there is mistakes on my method to customizing the tableview cell.
Thank you

Solution: I missed
[super layoutSubviews];
inside my customized the layout methods.

Related

Why custom view awakeFromNib init set not work in Objc

I have a custom view about alertView.
But when I init my alertView, it seem not set my awakeFromNib value of label.
What's wrong about my code?
I can't figure out this issue.
Thanks.
AlertView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
#interface AlertView : UIView
#property (weak, nonatomic) IBOutlet UILabel *labTitle;
#property (weak, nonatomic) IBOutlet UILabel *labMessage;
#property (weak, nonatomic) IBOutlet UIButton *btnCancel;
#property (weak, nonatomic) IBOutlet UIButton *btnConfirm;
#end
AlertView.m
#import "AlertView.h"
#interface AlertView ()
#property (weak, nonatomic) IBOutlet UIView *headerView;
#end
#implementation AlertView
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(!self){
return nil;
}
self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
owner:self
options:nil] firstObject];
self.frame = frame;
return self;
}
- (void)awakeFromNib{
[super awakeFromNib];
// custom label text not show.
self.labTitle.text = #"123";
self.labMessage.text = #"456";
[self.btnCancel setTitle:#"789"; forState:UIControlStateNormal];
[self.btnConfirm setTitle:#"111"; forState:UIControlStateNormal];
self.btnCancel.layer.cornerRadius = 5.f;
self.btnConfirm.layer.cornerRadius = 5.f;
self.headerView.layer.cornerRadius = 10;
}
#end
Viewcontroller.m
#interface Viewcontroller ()
#property AlertView * alertView;
#end
#implementation Viewcontroller
- (void)viewDidLoad {
[self popupView];
}
- (void)popupView{
CGRect viewSize = CGRectMake(16, 100, [[UIScreen mainScreen] bounds].size.width - 16 * 2, self.height);
self.alertView = [[ResultAlertView alloc] initWithFrame:viewSize];
self.alertView.layer.cornerRadius = 10;
[self SetShadowForView:self.alertView];
[self.alertView.btnConfirm addTarget:self action:#selector(cancelStartUsingView:) forControlEvents:UIControlEventTouchUpInside];
[self.alertView.btnCancel addTarget:self action:#selector(cancelStartUsingView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.alertView];
}
- (IBAction)cancelStartUsingView:(UIButton *)sender{
//Btn also not work.
NSLog(#"123");
}
#end
you can use:
- (void)layoutSubviews {
[super layoutSubviews];
self.labTitle.text = #"123";
self.labMessage.text = #"456";
[self.btnCancel setTitle:#"789" forState:UIControlStateNormal];
[self.btnConfirm setTitle:#"111"forState:UIControlStateNormal];
self.btnCancel.layer.cornerRadius = 5.f;
self.btnConfirm.layer.cornerRadius = 5.f;
self.headerView.layer.cornerRadius = 10;
}
and call
- (void)viewDidLoad {
dispatch_async(dispatch_get_main_queue(), ^{
[self popupView];
});
}

[UIView registerClass:forCellReuseIdentifier:]: unrecognized selector sent to instance

I want to build a tableView cell and add subview to it.But something is wrong in there.The title is the mistake,and language is Objective-c,please help me,thanks!
Here are the subclass of UITableViewCell,it name is NameAndColorCell.
NameAndColorCell.h:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface NameAndColorCell : UITableViewCell
#property (copy,nonatomic) NSString *name;
#property (copy,nonatomic) NSString *color;
#end
NameAndColorCell.m:
#import "NameAndColorCell.h"
#interface NameAndColorCell()
#property (strong,nonatomic) UILabel *nameLabel;
#property (strong,nonatomic) UILabel *colorLabel;
#end
#implementation NameAndColorCell
- (id)initWithStyle : (UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
CGRect nameLabelRect = CGRectMake(0, 5, 70, 15);
UILabel *nameMaker = [[UILabel alloc]initWithFrame:nameLabelRect];
nameMaker.textAlignment = NSTextAlignmentRight;
nameMaker.text = #"Name";
nameMaker.font = [UIFont boldSystemFontOfSize:12];
[self.contentView addSubview:nameMaker];
CGRect colorLabelRect = CGRectMake(0, 26, 70, 15);
UILabel *colorMaker = [[UILabel alloc]initWithFrame:colorLabelRect];
colorMaker.textAlignment = NSTextAlignmentRight;
colorMaker.text = #"color";
colorMaker.font = [UIFont boldSystemFontOfSize:12];
[self.contentView addSubview:colorMaker];
CGRect nameValueRect = CGRectMake(80, 5, 200, 15);
_nameLabel = [[UILabel alloc]initWithFrame:nameValueRect];
[self.contentView addSubview:_nameLabel];
CGRect colorValueRect = CGRectMake(80, 25, 200, 15);
_colorLabel = [[UILabel alloc]initWithFrame:colorValueRect];
[self.contentView addSubview:_colorLabel];
}
return self;
}
- (void)setName:(NSString *)n{
if (![n isEqualToString:_name]) {
_name = [n copy];
self.nameLabel.text = _name;
}
}
- (void)setColor:(NSString *)c{
if (![c isEqualToString:_color]) {
_color = [c copy];
self.colorLabel.text = _color;
}
}
#end
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
<UITableViewDataSource,UITableViewDelegate>
#end
ViewController.m:
#import "ViewController.h"
#import "NameAndColorCell.h"
static NSString *CellTableIdentifier = #"CellTableIdentifier";
#interface ViewController ()
#property (copy,nonatomic) NSArray *computer;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.computer = #[#{#"Name" : #"MacBook Air", #"Color" : #"Silver"},
#{#"Name" : #"MacBook Pro", #"Color" : #"Silver"},
#{#"Name" : #"iMac", #"Color" : #"Silver"},
#{#"Name" : #"MacMini", #"Color" : #"Silver"},
#{#"Name" : #"Mac Pro", #"Color" : #"Black"}];
UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView registerClass:[NameAndColorCell class] forCellReuseIdentifier:CellTableIdentifier];
UIEdgeInsets contentInset = tableView.contentInset;
contentInset.top = 20;
[tableView setContentInset:contentInset];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.computer count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NameAndColorCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier forIndexPath:indexPath];
NSDictionary *rowData = self.computer[indexPath.row];
cell.name = rowData[#"Name"];
cell.color = rowData[#"Color"];
return cell;
}
#end
I set the value of tag as 1.
You might need to cast it like
UITableView *tableView = (UITableView *)[self.view viewWithTag:1];
`UITableView *tableView = (id)[self.view viewWithTag:1]`
returns the UIView not a UITableView. You need to cast it properly to UITableView. You should use
UITableView *tableView = (UITableView *)[self.view viewWithTag:1];
Instead of accessing TableView using its tag you can create IBOutlet for the same.
If you are using prototype cell in storyboard registerClass is not required.

UIButton doesn't resize to fit its titleLabel

I'm working with iOS8 Self-Sizing cell and I want the cell resize to fit its subview, messageBodyButton and profileImageView.
profileImageView's size is fixed. As you can see in the picture below, the messageBodyButton doesn't resize to fit its titleLabel.
Why did this happen? How to fix this bug using Auto Layout?
And when the titleLabel's content changed(say, the model's data changed), how should I update constraints so that the cell height can be calculated correctly?
Here is my code:
RXTChatCell.m:
#property (nonatomic, strong) UILabel *timeLabel;
#property (nonatomic, strong) UIImageView *profileImageView;
#property (nonatomic, strong) UIButton *messageBodyButton;
#property (nonatomic, assign) BOOL needUpdateConstraints;
#property (nonatomic, assign) BOOL didSetUpConstraints;
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_timeLabel = [[UILabel alloc] init];
_timeLabel.text = #"19:00";
[self.contentView addSubview:_timeLabel];
_profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"placeholder"]];
[self.contentView addSubview:_profileImageView];
_messageBodyButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_messageBodyButton.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[_messageBodyButton.titleLabel setTextAlignment:NSTextAlignmentRight];
_messageBodyButton.titleLabel.numberOfLines = 0;
_messageBodyButton.titleLabel.backgroundColor = UIColor.grayColor;
[self.contentView addSubview:_messageBodyButton];
_timeLabel.backgroundColor = UIColor.redColor;
_profileImageView.backgroundColor = UIColor.greenColor;
_messageBodyButton.backgroundColor = UIColor.blueColor;
self.contentView.backgroundColor = UIColor.orangeColor;
}
return self;
}
- (void)updateConstraints
{
if (!self.didSetUpConstraints) { // set up constraints
[_timeLabel makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView.topMargin);
make.centerX.equalTo(self.contentView);
}];
[_profileImageView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_timeLabel.bottom).offset(10);
make.right.equalTo(self.contentView).offset(-10);
make.width.and.height.equalTo(50);
make.bottom.lessThanOrEqualTo(self.contentView.bottomMargin);
}];
[_messageBodyButton makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_profileImageView);
make.right.equalTo(_profileImageView.left).offset(-10);
make.left.equalTo(self.contentView).offset(10);
make.bottom.lessThanOrEqualTo(self.contentView.bottomMargin);
}];
self.didSetUpConstraints = YES;
}
if (self.needUpdateConstraints){
// here, how should I update constraints?
}
[super updateConstraints];
}
- (void)setMessage:(RXTChatMessage *)message
{
EMTextMessageBody *body = (EMTextMessageBody *)message.messageBody.body;
[self.messageBodyButton setTitle:body.text forState:UIControlStateNormal];
self.needUpdateConstraints = YES;
[self setNeedsUpdateConstraints];
}
RXTChatViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.estimatedRowHeight = 44;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RXTChatMessage *message = self.messages[indexPath.row];
RXTChatCell *cell = [tableView dequeueReusableCellWithIdentifier:RXTChatCellId];
if (cell == nil) {
cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RXTChatCellId];
}
cell.message = message;
return cell;
}
Select Your Button -> Go to Editor -> Size to fit content.
Remove fixed height constraint for your button (only set leading,trailing,top bottom) constraint (Superview should be your cell view).
This should solve your problem
let me know if it solves your issue.

Custom UITableViewCell is wrong width on iPad

I've created a custom UITableViewCell, but when I run my app on iPad the cell's content is the same width as on iPhone. I want the content - the background UIView for example - to be the complete width.
customCell.h
#import <UIKit/UIKit.h>
#interface customCell : UITableViewCell
#property (nonatomic, strong) UILabel *title;
#property (nonatomic, strong) UILabel *description;
#property (nonatomic, strong) UIView *background;
#end
customCell.m
#import "customCell.h"
#implementation customCell
#synthesize title = _title;
#synthesize description = _description;
#synthesize background = _background;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// configure background
self.background = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 115.0f)];
// configure title
self.title = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, self.contentView.frame.size.width-10, 70)];
self.title.textAlignment = NSTextAlignmentNatural;
self.title.lineBreakMode = NSLineBreakByWordWrapping;
self.title.numberOfLines = 4;
self.title.preferredMaxLayoutWidth = 20;
self.title.adjustsFontSizeToFitWidth = NO;
self.title.textColor = [UIColor whiteColor];
self.title.font = [UIFont fontWithName:#"SourceSansPro-Regular" size:16];
[self.background addSubview:self.title];
// configure description
self.description = [[UILabel alloc] initWithFrame:CGRectMake(5, 80, self.contentView.frame.size.width-10, 20)];
self.description.textColor = [UIColor whiteColor];
self.description.textAlignment = NSTextAlignmentLeft;
self.description.font = [UIFont fontWithName:#"SourceSansPro-Bold" size:13];
[self.background addSubview:self.description];
[self addSubview:self.background];
[self sendSubviewToBack:self.background];
}
return self;
}
#end
What am I doing wrong here?
The cell's size is not yet set in the initWithStyle:reuseIdentifier: method. Either setup your subviews with proper autoresizingMask values or implement the cell's layoutSubviews method to update their sizes.
Also, since you add the title and description labels to the background view, the labels' sizes should be based on the size of the background view, not the size of the cell's contentView.
And lastly, never name a property description. It will conflict with the description method inherited from NSObject.

UITableView skips one indexPath between every one of custom UITableViewCell, disappears when scrolling

The problem in pictures:
Basically, my table view adds an empty cell between each other, and I don't know why. I already tried putting an NSLog statement in cellForRowAtIndexPath:, willDisplayCell:forRowAtIndexPath:, and didSelectRowAtIndexPath:, and the data source in each cell's indexPath is correct. It's just that the displayed cells are skipping in between, and I don't understand why.
My view controller and table view cell subclasses are also very simple. Here's the code:
PrivateMessagesViewController.m
static NSString * const kCellIdentifier = #"kCellIdentifier";
#interface PrivateMessagesViewController () <UITableViewDataSource, UITableViewDelegate>
#property (strong, nonatomic) NSMutableArray *messages;
#property (strong, nonatomic) SCPFInboxQuery *query;
#property (nonatomic) BOOL hasAlreadyFetchedBefore;
#property (strong, nonatomic) UITableView *tableView;
// This is a custom view that either shows a loading animation,
// a "No Results Found" label, or an error message with a Retry button
// at the center of the view of a view controller.
#property (strong, nonatomic) SCPCenterView *centerView;
#end
#implementation PrivateMessagesViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Private Messages";
self.messages = [NSMutableArray array];
self.query = [[SCPFInboxQuery alloc] init];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height - 113) style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.tableView registerClass:[SCPPrivateMessageListCell class] forCellReuseIdentifier:kCellIdentifier];
self.tableView.alpha = 0; // The table view starts out invisible.
[self.view addSubview:self.tableView];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!self.hasAlreadyFetchedBefore) {
[self.view addSubview:self.centerView];
[self.centerView showLoadingView];
__weak PrivateMessagesViewController *weakSelf = self;
[self.query runInBackgroundWithCompletion:^(NSArray *messages, NSError *error) {
PrivateMessagesViewController *innerSelf = weakSelf;
// If there is an error, handle it.
if (error) {
// ...
return;
}
// If there weren't any messages before and none were found,
// the user has no messages in the inbox.
if (!innerSelf.hasAlreadyFetchedBefore && messages.count == 0) {
[innerSelf.centerView showNoResultsLabel];
}
else {
if (!innerSelf.hasAlreadyFetchedBefore) {
[innerSelf.centerView fadeOutAndRemoveFromSuperview];
innerSelf.tableView.alpha = 1;
} else {
}
[innerSelf.messages addObjectsFromArray:messages];
[innerSelf.tableView reloadData];
}
innerSelf.hasAlreadyFetchedBefore = YES;
}];
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.messages.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SCPPrivateMessageListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
SCPFMessage *message = self.messages[indexPath.row];
cell.message = message;
return cell;
}
#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [SCPPrivateMessageListCell heightForMessage:self.messages[indexPath.row]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
SCPPrivateMessageListCell *theCell = (SCPPrivateMessageListCell *)cell;
NSLog(#"Message at %d: %#", indexPath.row, theCell.message.rawData);
}
#pragma mark - Getters
- (SCPCenterView *)centerView
{
if (!_centerView) {
_centerView = [[SCPCenterView alloc] initWithParent:self.view];
_centerView.noResultsText = #"You don't have any private messages yet.";
}
return _centerView;
}
#end
SCPPrivateMessageListCell.m
static const CGFloat kInnerMargin = 10;
static const CGFloat kSpaceBetweenImageAndRightLabel = 10;
static const CGFloat kImageViewSize = 60;
static const CGFloat kRightLabelX = kInnerMargin + kImageViewSize + kSpaceBetweenImageAndRightLabel;
static const CGFloat kMaxRightLabelWidth = 320 - (kRightLabelX + kInnerMargin);
#interface SCPPrivateMessageListCell ()
#property (strong, nonatomic) UIImageView *thumbnailImageView;
#property (strong, nonatomic) UILabel *dateLabel;
#property (strong, nonatomic) UILabel *senderLabel;
#property (strong, nonatomic) UILabel *subjectLabel;
#property (strong, nonatomic) UILabel *summaryLabel;
#end
#implementation SCPPrivateMessageListCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (self) {
_thumbnailImageView = [[UIImageView alloc] init];
_thumbnailImageView.contentMode = UIViewContentModeScaleAspectFill;
_thumbnailImageView.layer.cornerRadius = kImageViewSize / 2;
_thumbnailImageView.clipsToBounds = YES;
_dateLabel = [[UILabel alloc] init];
_dateLabel.font = [UIFont systemFontOfSize:10];
_dateLabel.textAlignment = NSTextAlignmentCenter;
_dateLabel.numberOfLines = 1;
_dateLabel.lineBreakMode = NSLineBreakByTruncatingTail;
_senderLabel = [SCPPrivateMessageListCell senderLabel];
_subjectLabel = [SCPPrivateMessageListCell subjectLabel];
_summaryLabel = [SCPPrivateMessageListCell summaryLabel];
[self.contentView addSubview:_thumbnailImageView];
[self.contentView addSubview:_dateLabel];
[self.contentView addSubview:_senderLabel];
[self.contentView addSubview:_subjectLabel];
[self.contentView addSubview:_summaryLabel];
}
return self;
}
- (void)setMessage:(SCPFMessage *)message
{
_message = message;
[self.thumbnailImageView setImageWithURL:message.thumbnailURL];
self.dateLabel.text = message.dateSent;
self.senderLabel.text = message.nameOfSender;
self.subjectLabel.text = message.subject;
self.summaryLabel.text = message.summary;
[self setNeedsLayout];
}
- (void)layoutSubviews
{
self.thumbnailImageView.frame = CGRectMake(kInnerMargin, kInnerMargin, kImageViewSize, kImageViewSize);
[self.dateLabel sizeToFitWidth:kImageViewSize];
self.dateLabel.frame = CGRectMake(kInnerMargin, kInnerMargin + kImageViewSize, kImageViewSize, self.dateLabel.frame.size.height);
[self.senderLabel sizeToFitWidth:kMaxRightLabelWidth];
self.senderLabel.frame = CGRectMake(kRightLabelX, kInnerMargin, kMaxRightLabelWidth, self.senderLabel.frame.size.height);
CGFloat subjectLabelY = self.senderLabel.frame.origin.y + self.senderLabel.frame.size.height;
[self.subjectLabel sizeToFitWidth:kMaxRightLabelWidth];
self.subjectLabel.frame = CGRectMake(kRightLabelX, subjectLabelY, kMaxRightLabelWidth, self.subjectLabel.frame.size.height);
CGFloat summaryLabelY = self.subjectLabel.frame.origin.y + self.subjectLabel.frame.size.height;
[self.summaryLabel sizeToFitWidth:kMaxRightLabelWidth];
self.summaryLabel.frame = CGRectMake(kRightLabelX, summaryLabelY, kMaxRightLabelWidth, self.summaryLabel.frame.size.height);
CGFloat cellHeight = [SCPPrivateMessageListCell heightForMessage:self.message];
self.contentView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, cellHeight);
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, cellHeight);
}
#pragma mark - Class methods
+ (UILabel *)senderLabel
{
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont boldSystemFontOfSize:17];
label.textColor = [UIColor colorFromHex:0x0076be];
label.numberOfLines = 1;
label.lineBreakMode = NSLineBreakByTruncatingTail;
return label;
}
+ (UILabel *)subjectLabel
{
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:14];
label.numberOfLines = 1;
label.lineBreakMode = NSLineBreakByTruncatingTail;
return label;
}
+ (UILabel *)summaryLabel
{
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:12];
label.textColor = [UIColor grayColor];
label.numberOfLines = 3;
label.lineBreakMode = NSLineBreakByTruncatingTail;
return label;
}
+ (CGFloat)heightForMessage:(SCPFMessage *)message
{
CGFloat height = kInnerMargin;
UILabel *senderLabel = [SCPPrivateMessageListCell senderLabel];
senderLabel.text = message.nameOfSender;
[senderLabel sizeToFitWidth:kMaxRightLabelWidth];
height += senderLabel.frame.size.height;
UILabel *subjectLabel = [SCPPrivateMessageListCell subjectLabel];
subjectLabel.text = message.subject;
[subjectLabel sizeToFitWidth:kMaxRightLabelWidth];
height += subjectLabel.frame.size.height;
UILabel *summaryLabel = [SCPPrivateMessageListCell summaryLabel];
summaryLabel.text = message.summary;
[summaryLabel sizeToFitWidth:kMaxRightLabelWidth];
height += summaryLabel.frame.size.height;
height += kInnerMargin;
return height;
}
#end
There. I'm not doing anything non-standard. The subviews of the cells also disappear when I scroll, but when I scroll them back in, the content becomes displayed, still with a skipping empty cell. Anyone have any idea why this is happening?
The problem was in my table view cell's layoutSubviews.
self.contentView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, cellHeight);
That should have been:
self.contentView.frame = CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, cellHeight);
I found out by setting a different color for self.backgroundColor and self.contentView.backgroundColor from inside the init method.

Resources