UITableViewCell misses loading some local images/text - ios

I have a custom Table View Cell that loads a thumbnail, text and text's background image. I am developing a chat app and the Cell is in the Send/Receive Message screen. This cell basically shows the sent/received. Below are more details regarding the project and problem.
I have two background images. One is for sender and the other is for receiver and these images are automatically resized based on the size of the text.
When I am sending/receiving small messages (1 line), the messages are displayed correctly.
However, when I try to send/receive multiple line messages, sometime the background images are missing and sometimes the text is missing (for some images) and when I scroll, those images/text appears some times.
I am using [UIImage imagedNamed:] to load the background images each time.
In my point of view, the issue is due to Memory as around 6-8 cells are visible all the times. Kindly help me in resolving the issue.
EDIT
Adding some code
-(UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [tblView dequeueReusableCellWithIdentifier:#"myCell"];
//Setting background image view of cell
[cell.bgImageView setImage:[[UIImage imageNamed:#"chat_box2.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:40]];
String message = ........;
CGSize textSize = CGSizeMake(250, 1000);
CGSize size = [message sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:textSize];
size.width += 9;
[cell.messageText setText:message];
[cell.messageText sizeToFit];
[cell.messageText setText:message];
//Setting frames of background Image View and message Text to our desired frame (**size** is calculated in the above lines)
[cell.bgImageView setFrame:CGRectMake(79,5, cell.bgImageView.frame.size.width, size.height+18)];
[cell.messageText setFrame:CGRectMake(98, 13, size.width, size.height)];
return cell;
}
Note: The size calculation is also done in -(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath so that the cell is resized accordingly.

Yup, The issue is due to cell reusability. When you deque Tableviewcells the contents become mixup as the system tries to Re-use the old tableviewCells. What you should do is to set all the values for your cell in cellForRowAtIndexPath delegate as:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//All Initialization code
.
.
.
//Set all the values of your Custom Table View Cell here
cell.image = yourImage;
cell.text = "your text";
cell.backGroundImage = yourBackGroundImage;
cell.TimeLabel.Text = "time value";
}
Hope this should help. Free feel to ask if you have further queries.

If the cell is being displayed and the image and text are not, then the problem is related to the frame of textView and frame of the imageView.
You can try to tick clip subviews at your views (especially imageViews) and check if that makes the trick.
Anyway I suggest you to use autolayout either then defining the frame of your views.

Finally, I am able to resolve the issue myself. Below is a detailed response highlighting the cause as well as the solution for the problem.
Cause of Problem
The items [bgImageView (UIImageView) and messageText(UILabel)] were IBOutlets defined inside the Custom Cell class and connected to Cell in the Storyboard.
Whenever, we try to change frame of such elements (defined inside storyboard), the cell is not updated which was the root cause of the problem.
Solution
In order to resolve the issue, I removed the elements from storyboard and defined them inside the -initWithCoder:. Please Note that this function is called for Cells of storyboard prototype cells (instead of -initWithStyle:reuseIdentifier).
initWithCoder: This method is to be defined inside the custom UITableViewCell Class (in my case, the name of the class is MyCell
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
//Settings sizes to zero as they will be changed in cellForRowAtIndexPath
_bgImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:_bgImageView];
_messageText = [[UILabel alloc] initWithFrame:CGRectZero];
_messageText.backgroundColor = [UIColor clearColor];
[_messageText sizeToFit];
[_messageText setFont:[UIFont systemFontOfSize:12]];
[_messageText setNumberOfLines:0];
[self.contentView addSubview:_messageText];
}
return self;
}
The heightForRowAtIndexPath* and *cellForRowAtIndexPath: are also given for reference.
heightForRowAtIndexPath:
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *message = .......;
CGSize size = [self getSize:message]; //look below for getSize:
size.height += 18 + 15; //to cater for padding (top & bottom).
return height;
}
cellForRowAtIndexPath:
-(UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [tblView dequeueReusableCellWithIdentifier:#"msgCell"];
NSString *message = ......;
CGSize size = [self getSize:message]; //look below for getSize:
cell.messageText.text = message;
[cell.messageText setFrame:CGRectMake(98, 13, size.width, size.height)];
[cell.bgImageView setFrame:CGRectMake(79,5, CELL_MESSAGE_WIDTH+32, size.height+18)]; //write #define CELL_MESSAGE_WIDTH 200 at the top of the file (below include statements)
[cell.bgImageView setImage:[[UIImage imageNamed:#"img.png"] stretchableImageWithLeftCapWidth:11 topCapHeight:23]];
return cell;
}
getMaxSize:
-(CGSize)getSize:(NSString*)str
{
CGSize maxSize = CGSizeMake(CELL_MESSAGE_WIDTH, 1000);
CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
return size;
}

Try to set the image, text and all the content inside of MyCell not in -tableView: cellForRowAtIndexPath: delegate method.
I can see you mark yourself as solved. If it works that's great. However there are couple of thing you should definitely improve and change. There are my suggestions.
I don't think you have to do anything in initWithCoder:. You can leave to storyboard to handle. Here is the code how I think you should do:
MyCell.h
// define enum for type of cell
typedef NS_ENUM(NSInteger, MyCellType) {
MyCellTypeSender,
MyCellTypeReceiver
};
#interface MyCell : UITableViewCell
#property (nonatomic, assign) MyCellType cellType;
#property (nonatomic, strong) NSString *message;
- (void)fitToSize:(CGSize)size;
#end
MyCell.m
#implementation MyCell
// As you can see image is implemented inside the cell in setter of cellType
-(void)setCellType:(MyCellType)cellType {
if (_cellType != cellType) {
_cellType = cellType;
UIImage *bgImage = [UIImage imageNamed:(cellType == MyCellTypeReceiver) ? #"receiverImg" : #"senderImg"];
self.bgImageView.image = [bgImage stretchableImageWithLeftCapWidth:11 topCapHeight:23];
}
}
-(void)setMessage:(NSString *)message {
if (![message isEqualToString:_message]) {
_message = message;
self.messageLabel.text = _message;
}
}
-(void)fitToSize:(CGSize)size {
self.messageLabel.frame = CGRectMake(98, 13, size.width, size.height);
self.bgImageView.frame = CGRectMake(79,5, size.width+32, size.height+18);
}
#end
In your file where you implement delegate and data source method for table use following code:
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *message = #"some message ....";
CGSize size = [self sizeForText:message];
return size.height;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:#"myCell"];
CGRect cellFrame = [tableView rectForRowAtIndexPath:indexPath]; // Do not calculate the size again. Just grab cell frame from current indexPath
[cell fitToSize:cellFrame.size]; // You set the content of the cell in MyCell by passing size of cell
cell.cellType = MyCellTypeRecever; // Or MyCellTypeSender whichever you decide
cell.message = #"some message ....";
return cell;
}
-(CGSize)sizeForText:(NSString*)str {
NSDictionary *attributes = #{NSFontAttributeName: [UIFont systemFontOfSize:18.f]};
CGSize maxSize = CGSizeMake(CELL_MESSAGE_WIDTH, 1000);
// This is the method you should use in order to calculate the container size
// Avoid using -sizeWithFont:constrainedToSize:lineBreakMode: as it is depracated in iOS7
CGRect rect = [str boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:NULL];
return rect.size;
}
Some lines are commented. As you can see most of the important code landed in MyCell subclass of UITableVieCell.
Also please note -sizeWithFont:constrainedToSize:lineBreakMode: is deprecated and use -boundingRectWithSize:options:attributes:context:

Related

How to set the same width between custom UITableViewCells and UITableView?

I created several cells with Interface Builder, and I'm using them to fill a UITableView. In other words, I have 3 classes for 3 different kinds of cell, and an other view which contains a UITableView.
- My UITableView containing different kinds of cells :
Here's my problem :
On the iPhone emulator, it looks great. But on the iPad emulator, the custom cells width is fixed. The UITableView width fits to the screen width, so it's good, but the UITableViewCells does not fit to the UITableView. I want to force the custom UITableViewCells to take the UITableView width.
Is there anything to do in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathmethod, where I instanciate my custom cells ?
Or do I have to write a thing like self.fitToParent; in the custom cells header file ?
EDIT (schema) :
EDIT 2 (cellForRowAtIndexPath method) :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifierType1 = #"cellType1";
static NSString *cellIdentifierType2 = #"cellType2";
NSString *currentObjectId = [[myTab objectAtIndex:indexPath.row] type];
// Cell type 1
if ([currentObjectId isEqualToString:type1])
{
CelluleType1 *celluleType1 = (CelluleType1 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifierType1];
if(celluleType1 == nil)
celluleType1 = [[CelluleType1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierType1];
celluleType1.lblAuteur.text = #"Type1";
return celluleType1;
}
// Cell type 2
else if ([currentObjectId isEqualToString:type2])
{
CelluleType2 *celluleType2 = (CelluleType2 *)[tableViewdequeueReusableCellWithIdentifier:cellIdentifierType2];
if(celluleType2 == nil)
celluleType2 = [[CelluleType2 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierType2];
celluleType2.lblAuteur.text = #"Type2";
return celluleType2;
}
else
return nil;
}
}
I think uitableviewcell's width is the same as the tableview's width.You can try to set cell's background color to test it. cell.backgroundColor = [UIColor redColor] ;
You should create a class which inherit from UITableViewCell and override it's method - (void)layoutSubviews , adjust your content's frame there.
I resolved my problem using the following code in each custom cell class. It's not very clean, but I can't spend one more day on this issue...
- (void)layoutSubviews
{
CGRect contentViewFrame = self.contentView.frame;
contentViewFrame.size.width = myTableView.bounds.size.width;
self.contentView.frame = contentViewFrame;
}
Thank you for your help KudoCC.
- (void)awakeFromNib {
[super awakeFromNib];
// anything you write in this section is taken with respect to default frame of width 320.
}
awakeFromNib is called when [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; is processed- anything you write in section is taken with respect to default frame of width 320.
You need to make another custom function and call it after cell gets initialized.
For eg:-
#implementation CheckinTableViewCell{
UILabel *NameLabel;
UILabel *rollLabel;
}
- (void)awakeFromNib {
[super awakeFromNib];
NameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
rollLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:NameLabel];
[self.contentView addSubview:rollLabel];
}
-(void) bindView{
NameLabel.frame = CGRectMake(10, 10, self.contentView.frame.size.width-20, 20);
rollLabel.frame = CGRectMake(10, 30, NameLabel.frame.size.width, 20);
}
and call this function in tableview cellForRowAtIndex:-
-(UITableViewCell*) tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
CheckinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell ==nil){
cell = [[CheckinTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.name = #"Harry";
cell.rollno = #"123456";
[cell bindView];
return cell;
}

dequeueReusableCellWithIdentifier with cells of different size

STEP 1 - this is working fine
I have UITableView that loads custom UITableViewCells
STEP 2 - this is kinda works
I change the UITableViewCell height so that all the data contained in the cell within a UITextView is visible
I manage to get the UItextView data and resize it by using the following code:
UITextView *dummy = [[UITextView alloc] init];
dummy.font = [UIFont systemFontOfSize:14];
dummy.text = cell.textView.text;
CGSize newSize = [dummy sizeThatFits:CGSizeMake(270.0f, 500.0f)];
//get the textView frame and change it's size
CGRect newFrame = cell.textView.frame;
newFrame.size = CGSizeMake(270.0f, fmaxf(newSize.height, 60));
cell.textView.frame = newFrame;
//resize the cell view I add +95 because my cell has borders and other stuff...
CGRect cellFrame = CGRectMake(0, 0, 320, newFrame.size.height+95);
cell.cellView.frame = cellFrame;
I then manage to set the UITableViewCell height using the delegate function heightForRowAtIndexPath:
Now when I run the code and scroll up and down the table cells the behavious isn't always as expected... i.e the cell size isn't always the right size, but if I scroll up and down it sometimes loads up the right size again.
I am thinking that perhaps the dequeueReusableCellWithIdentifier is the issue
cellIdentifier = #"tableCell";
ctTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
Since I am recycling cells of different size with the same identifier...
Can anybody give me some guidelines on how best to solve this issue?
Thanks!!!
I would start by adding UITextView dynamically inside cellForRowAtIndexPath method. assumptions(data array contains the content to be displayed inside cell)
// Defines
#define CELL_TEXTVIEW_WIDTH = 320
#define CELL_TEXTVIEW_HEIGHT = 9999
#define PADDING = 5
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"tableCell";
ctTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if(cell == nil){
cell = [[tableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *_data = [data objectAtIndex:indexPath.row];
CGSize _data_contentsize = [_data sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(CELL_TEXTVIEW_WIDTH, CELL_TEXTVIEW_HEIGHT) lineBreakMode:NSLineBreakModeWordWrap];
UITextView *dummy=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, _data_contentsize +(PADDING * 2))];
// add Font and text color for your textview here
[cell.contentView addSubview:dummy];
return cell;
}
After this calculate the height of the cell under heightForRowAtIndexPath method.
have you implemented following method ?
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:
you should set height for each row , if they are different from each other

Resize Contents of TableViewCell based on Data pulled from API

I'm trying to get the cell to resize based on the contents that are getting pulled in from an API (image+text).
I have it set right now that I leave space for an Image, a Headline (two lines), and a Description (two lines).
The problem: Sometimes no Image is available, sometimes the Headline is only one line, and sometimes there is no description or 1 line of description; so I need to resize the cell based on these dynamic contents.
WebListCell.m
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(1, 20, 320, 180);
self.headlineLabel.frame = CGRectMake(10, 210, 290, 40);
self.descriptionLabel.frame = CGRectMake(10, 250, 290, 30);
[self.headlineLabel setNumberOfLines:2];
[self.headlineLabel sizeToFit];
[self.descriptionLabel setNumberOfLines:2];
[self.descriptionLabel sizeToFit];
}
WebListViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Feed *feedLocal = [headlinesArray objectAtIndex:indexPath.row];
NSString *headlineText = [NSString stringWithFormat:#"%#", feedLocal.headline];
NSString *descriptionText = [NSString stringWithFormat:#"%#", feedLocal.description];
cell.headlineLabel.text = headlineText;
cell.descriptionLabel.text = descriptionText;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
WebListCell *cell = [tableView dequeueReusableCellWithIdentifier:#"WebListCell"];
Feed *feedLocal = [headlinesArray objectAtIndex:indexPath.row];
NSString *head = [NSString stringWithFormat:#"%#", feedLocal.headline];
cell.headlineLabel.text = head;
if (head.length > 100 ){
[cell.headlineLabel setNumberOfLines:1];
}
} // WARNING ALERT: "CONTROL REACHES END OF NON-VOID FUNCTION"
I have WebListCell.m that lays out the cell, and then WebListViewController.m that calls WebListCell.m for the layout. I'm pulling in all of the data correctly, but just need help resizing and can't figure it out, even though I've checked out other questions on StackOverflow... can anyone help with this?
Right now what I have doesn't work to resize the cell height.
Much appreciated, and I will post any additional code as needed!
heightForRowAtIndexPath expects you to return a float value.. i.e. the height for the cell. That's the exception being raised.
To figure out how much vertical space a string occupies, you can use this method:
CGSize size = [feedLocal.headline sizeWithFont:FONT
forWidth:WIDTH
lineBreakMode:NSLineBreakByWordWrapping];
return size.height + SOME_PADDING;
You'll need to experiment with what width and padding looks good. You have some other variables that you mention in the question, but generally, this is what you'll need to do.

UILabels not resizing in UITableView

I have a UITableView that uses prototype cells. The cells have a custom class called dataCell. The custom cells also have three UILabels:idLabel, contLabel, and expLabel. The cells properly resize based on the amount of text in expLabel; however, I cannot get the label itself to resize. Some labels resize when I scroll down; however, they also revert to showing only two lines and omitting text when I scroll back up. Here is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
dataCell *cell = (dataCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// the rest of your configure cell
// First Cell Label
[cell.idLabel setText:[idData objectAtIndex:indexPath.row]];
cell.idLabel.numberOfLines = 0;
// Second Cell Label
[cell.contLabel setText:[conData objectAtIndex:indexPath.row]];
cell.contLabel.numberOfLines = 0;
// Third Cell Label
[cell.expLabel setText:[expData objectAtIndex:indexPath.row]];
cell.expLabel.numberOfLines=0;
CGSize expectedLabelSize = [cell.expLabel.text sizeWithFont:cell.expLabel.font constrainedToSize:CGSizeMake(220, FLT_MAX) lineBreakMode:cell.expLabel.lineBreakMode];
cell.expLabel.frame=CGRectMake(cell.expLabel.frame.origin.x, cell.expLabel.frame.origin.y, expectedLabelSize.width, expectedLabelSize.height);
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
dataCell *cell = (dataCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
CGSize maximumLabelSize = CGSizeMake(220, FLT_MAX);
[cell.expLabel setText:[expData objectAtIndex:indexPath.row]];
CGSize expectedLabelSize = [cell.expLabel.text sizeWithFont:cell.expLabel.font constrainedToSize:maximumLabelSize lineBreakMode:cell.expLabel.lineBreakMode];
if (expectedLabelSize.height<43) {
expectedLabelSize.height=43;
}
return expectedLabelSize.height; }
Any help would be much appreciated
If you are using a storyboard and a UITableViewCell then you can just change the auto resizing mask, but if you are doing it programmatically then you will have to set the calculate the text width and height and reset the frame of the labels,
UILabel* label = [[UILabel alloc] init];
UIFont* font = label.font;
CGSize maxContentSizeForText = CGSizeMake(maxTextWidth, maxTextHeight);
CGSize stringTextSize = [string sizeWithFont:font constrainedToSize:maxContentSizeForText lineBreakMode:NSLineBreakByWordWrapping];
[label setFrame:CGRectMake(xPosition, yPosition, stringTextSize.width, stringTextSize.height);
[label setNumberOfLines:1000];
your label is probably a property from a xib file or storyboard, and the number of lines is just saying that you want the label to get really really big, since we can't say "infinite" i just generally use 1000 indicating 1000 lines of text maximum

Incorrect text wrapping in a UITableViewCell

I'm having problems with my custom UITableView. I was wondering as to how to properly make a group of text into the cell without seeing any ellipses "..." and without the text getting cut off at the end of the cell.
This is what my cell looks like, currently:
It is a part of a UISplitViewController. The problem with this is, before for some reason it would show the whole length of the text but it would get to the end of the cell and the rest of the string is cut off (this happens when I check "AutoLayout").
This is what my code looks like currently:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"BCell";
BracketTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[BracketTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell.description setLineBreakMode:NSLineBreakByWordWrapping];
cell.description.numberOfLines = 0;
cell.description.font = [UIFont fontWithName:#"Helvetica" size:14.0];
}
Bracket *bracket = [brackets objectAtIndex:indexPath.row];
[cell.description setText:bracket.name];
[cell.bracketId setText:[NSString stringWithFormat:#"%#", bracket.bracketId]];
return cell;
}
I am experimenting on height, but that doesn't seem to matter because I can set the height to whatever, but it still shows truncated text.
Thanks!
Typically my approach to supporting variable height cells is to define a class method that can calculate sizing for a given model object:
+ (CGFloat)heightForBracket:(Bracket*)bracket;
The beauty of making it a class method is that you can share constants (padding values, font sizes, indentation levels, etc) with your code that actually implements the layout without having to expose them to any other classes. If you want to change those constants in the future, you only have to make the change in one place in the cell subclass. An example subclass implementation:
#define kPaddingHorizontal 10.0
#define kPaddingVertical 10.0
#define kFontSizeName 17.0
+ (CGFloat)heightForBracket:(Bracket*)bracket {
// determine the dimensions of the name
UIFont *nameFont = [UIFont systemFontOfSize:kFontSizeName];
CGFloat nameSize = [bracket.name sizeWithFont:nameFont
constrainedToSize:CGSizeMake(300, CGFLOAT_MAX) // 300 is the width of your eventual label
lineBreakMode:NSLineBreakByWordWrapping];
// Apple recommends all cells be at least 44px tall, so we enforce a minimum here
return MAX(44, nameSize.height + 20 + kPaddingVertical*2); // 20 is space for the subtitle label
}
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
if (self) {
// bracket name
self.textLabel.numberOfLines = 0; // 0 makes this variable height
self.textLabel.font = [UIFont systemFontOfSize:kFontSizeName];
self.textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
self.textLabel.backgroundColor = [UIColor clearColor];
// if you wanted to hardcode a specific width, to a subview do it here as a constant and then share it with heightForBracket:
// bracket number
self.detailTextLabel.numberOfLines = 1;
self.detailTextLabel.font = [UIFont systemFontOfSize:14.0];
self.detailTextLabel.lineBreakMode = NSLineBreakByTruncatingTail;
self.detailTextLabel.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)setBracket:(Bracket*)bracket {
_bracket = bracket;
self.textLabel.text = bracket.name;
self.detailTextLabel.text = [NSString stringWithFormat:#"%#", bracket.bracketId];
}
You can then call heightForBracket: in tableView:heightForRowAtIndexPath::
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Bracket *bracket = [brackets objectAtIndex:indexPath.row];
return [BracketTableCell heightForBracket:bracket];
}
tableView:cellForRowAtIndexPath: becomes very easy, just set the appropriate bracket on the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"BCell";
BracketTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[BracketTableCell alloc] initWithReuseIdentifier:CellIdentifier];
}
Bracket *bracket = [brackets objectAtIndex:indexPath.row];
cell.bracket = bracket;
return cell;
}
A few notes:
this assumes the cell is not using Auto Layout
this explicitly hardcodes a width for the cell/label, which may or may not fit your use case
you should never name a property description because that is a method that already exists on the NSObject protocol
other enhancements would be caching the result of heightForBracket: to improve scrolling performance, especially if you start doing sizing logic for a ton of subviews
#gdubs you can use custom UITableViewCells
for reference you can use Customize Table View Cells for UITableView
I guess it would be easy for you to customize UILabels then. like if you want to add mutilple lines then set TitletLabel.numberOfLines=0; and if you want wordwrapping TitleLabel.lineBreakMode=NSLineBreakByWordWrapping;. There are other options in word wrapping as well.
The key to happiness with labels and Autolayout is to set the preferredMaxLayoutWidth property on the label. Without this labels don't wrap properly (or at all, in some cases, which is what you were seeing before, I think?).
Set the value to your maximum line width, and the labels should then behave correctly.
I think the problem has to do with the width of your label, if you are using auto layout expand your label's width to fill the parent cell and add trailing and leading to superview constraints, so that it resizes with it.

Resources