UITableView repeating cells over and over - ios

So I have this UITableView and it is repeating cells over and over and I don't get why, I have already tried googling this and looking at other similar stack overflow questions but none of the solutions in them seem to fix the problem I am having.
My code is the following:
NSArray in the ViewDidLoad method:
tableViewContents = #[
#"Infosecurity Netherlands:4th November:Jaarbeurs Utrecht-Utrecht,Netherlands:www.infosecurity.nl/",
#"MiCS Monaco International Clubbing Show:4th - 6th November:Grimaldi Forum-Monaco, Monaco:www.mics.mc/",
#"New York Audio Show 2015:6th - 8th November:Hilton Westchester-Rye Brook, NY:www.chestergroup.org/newyorkaudioshow/2015",
#"Tecno Multimedia InfoComm 2015:10th - 12th November:Corferias-Bogota, Columbia:www.tecnomultimedia.com/",
#"Productronica 2015:10th - 13th November:New Munich Trade Show Center-Munich, Germany:productronica.com/en/home",
#"CCW featuring SATCON co-located InfoComm Connections:11th November:Jacob Javits Center, New York, NY:www.ccwexpo.com/",
#"IAAPA Attractions Expo 2015:16th - 20th November:Orange County Convention Center-Orlando, FL:www.iaapa.org/expos/iaapa-attractions-expo/",
#"Streaming Media West 2015:17th November:Hyatt Regency Huntington Beach-Huntington Beach, CA:www.streamingmedia.com/Conferences/",
#"SATIS:17th - 19th November:Porte de Versailles-Paris, France:www.satis-expo.com/",
#"ISC East Expo 2015:18th November:Jacob K. Javits Convention Center-New York, NY:www.isceast.com/",
#"WFX Worship Facilities Conference and Expo:18th November:Music City Center-Nashville, TN:wfxweb.com/",
#"Inter BEE 2015:18th - 20th November:Makuhari Messe-Chiba, Japan:www.inter-bee.com/en/",
#"AV Executive Conference:18th - 20th November:TBA-Amelia Island, FL:www.infocomm.org/cps/rde/xchg/infocomm/hs.xsl/39221.htm",
#"JTSE Performance & Entertainment Technical Show & Convention 2015:24th November:Dock Pullman, Porte de la Chapelle-Paris, France:www.jtse.fr/en/index.php",
#"Government Video Expo 2015:1st - 3rd December:Walter E. Washington Convention Center-Washington, DC:www.gvexpo.com/",
#"IIDEX Canada:2nd December:Metro Toronto Convention Centre North-Toronto, ON, Canada:www.iidexcanada.com/",
#"IABM Annual International Business Conference 2015:3rd December:TBA:www.theiabm.org/",
#"SMSS The Social Media Strategies Summit Dallas:8th December:The Magnolia Hotel-Dallas, TX:www.socialmediastrategiessummit.com/dallas-2015/",
#"CineAsia:8th - 10th December:Hong Kong Convention & Exhibition Centre-Hong Kong, China:www.vnufilmgroup.com/cineasia",
#"CeBIT Bilisim Eurasia:17th - 19th December:Istanbul Expo Center-Istanbul, Turkey:www.cebitbilisim.com/en/index.php"
];
Table View Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//Amount of sections
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Amount of rows in each section
return [tableViewContents count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *contentsString = tableViewContents[indexPath.row];
NSArray *contentsArray = [contentsString componentsSeparatedByString:#":"];
//Cell Identifier
static NSString *simpleIdentifier = #"simpleIdentifier";
UILabel *eventNameLabel;
UILabel *eventDateLabel;
UILabel *eventLocationLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier];
eventNameLabel = [[UILabel alloc]init];
eventDateLabel = [[UILabel alloc]init];
eventLocationLabel = [[UILabel alloc]init];
eventNameLabel.frame = CGRectMake(10, 10, 300, 20);
eventNameLabel.font = [UIFont boldSystemFontOfSize:14];
eventNameLabel.textColor = [UIColor colorWithRed:231.0f/255.0f
green:123.0f/255.0f
blue:50.0f/255.0f
alpha:1.0f];
eventDateLabel.frame = CGRectMake(10, 40, 300, 20);
eventDateLabel.font = [UIFont systemFontOfSize:12];
eventDateLabel.textColor = [UIColor grayColor];
eventLocationLabel.frame = CGRectMake(10, 70, 300, 20);
eventLocationLabel.font = [UIFont systemFontOfSize:12];
eventLocationLabel.textColor = [UIColor grayColor];
[cell addSubview:eventNameLabel];
[cell addSubview:eventDateLabel];
[cell addSubview:eventLocationLabel];
}
eventNameLabel.text = contentsArray[0];
eventDateLabel.text = contentsArray[1];
eventLocationLabel.text = contentsArray[2];
//Return Cell
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
Thank you in advance for your help.

eventNameLabel and the other two labels are nil for reused cells so you never update the labels as desired.
You need an else statement added to your if that sets the three label variables to the labels in the existing cell.
Of course the better approach is to use a custom cell class with three properties for the three labels. Don't put all the logic in the view controller. It belongs in the cell class.

Obvious answer to your requirement is Custom Cell. Instead of doing all coding in cellforrowaatIndexpath method, customize the cell as per your requirement and use it for the table. It would be much easier to handle a Custom Cell. Refer these links for how to customize tableViewCell. http://www.appcoda.com/customize-table-view-cells-for-uitableview/ or http://www.appcoda.com/ios-programming-customize-uitableview-storyboard/ for customize cell.

Your code can't work as written. Think about it. Your cellForRowAtIndexPath method has 3 local variables eventNameLabel, eventDateLabel, and eventLocationLabel. If you create a new cell then those variables get pointed to your labels.
If you recycle an existing cell, though, those variables will be nil.
You need another way to get to the labels. How about assigning tags to them when you create the cells, and then in the code that deals with a recycled cell find the labels using those tags.
EDIT:
Or, as rmaddy suggests in his answer that beat mine by 1 minute, use a custom cell class where the cell has IBOutlet properties linked to the labels.

You can try putting 3 labels in your interface and this will let you access the labels in your cell. put it in
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-
NSArray *subviews = [[cell contentView] subviews];
UILabel *eventNameLabel = [subviews objectAtIndex:0];
UILabel *eventDateLabel = [subviews objectAtIndex:1];
UILabel *eventLocationLabel = [subviews objectAtIndex:2];
with this you don't have to add subViews either. another way is u can make a UITableViewCell class which you can link to the cell and then control drag labels to the cell class file as you would in a view controller.

Related

willDisplayCell won't change background color consistent with logic

I've tried just about everything I can think of, googled lots, searched SO, and still can't find the solution. Here's the issue:
I have a custom UITableViewCell with several labels in it. The text varies dynamically, and the background color (of the cell, not the labels) should vary as well. The text changes in the labels work fine. However, the BG color won't change no matter what I do. The only upside is that I don't feel that lonely. This is apparently a mystery even to a few high-rep people. I'm hoping someone here has found a solution (or can point out my mistake).
Here's what I have done:
Put the logic inside cellForRowAtIndexPath. Disappointment.
Googled and SO'd some more, then:
Put the logic inside a willDisplayCell call. Nothing.
Googled more, then:
Put the logic back into cellForRowAtIndexPath. Nada.
Put the logic inside the willDisplayCell call again. No go.
Found a post on SO that suggested putting another view in the custom cell to cover the original background and set it to change with logic. It didn't.
Tried putting the logic back into cellForRowAtIndexPath.
Tried using a switch statement in the logic.
Tried using if, else if, else in the logic.
Several other things I can't remember. Still doesn't work.
Yes, the UItableViewdelegate is set.
Here's the current code, which also doesn't work:
EDIT: Made a slight change in the top if statement to reflect the suggestion by #Adrian below. Unfortunately, it didn't cure the problem.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id<NSFetchedResultsSectionInfo> sectionInfo = [[detailFRC sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
NSLog(#"There are %lu objects in the frc",(unsigned long)[sectionInfo numberOfObjects]);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell * cell = [tableView dequeueReusableCellWithIdentifier:#"myCustomCell"];
if (!cell)
{
[tableView registerNib:[UINib nibWithNibName:#"CustomCell" bundle:nil] forCellReuseIdentifier:#"myCustomCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"myCustomCell"];
}
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(CustomCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
thisActivity = [detailFRC objectAtIndexPath:indexPath];
if (self.activityOrCategory == 0)
{
if (thisActivity.name == self.detailFocusItem)
{
[cell.myBGView setBackgroundColor:Rgb2UIColor(255, 215, 215)]; // Light red
cell.backgroundView = cell.myBGView;
}
else if (thisActivity.name == self.detailBenchmarkItem)
{
[cell.myBGView setBackgroundColor:Rgb2UIColor(215, 220, 255)]; // Light blue
}
else
{
cell.myBGView.backgroundColor = [UIColor whiteColor];
}
}
else if (self.activityOrCategory == 1)
{
if (thisActivity.category == self.detailFocusItem)
{
cell.myBGView.backgroundColor = Rgb2UIColor(255, 235, 200);
}
else if (thisActivity.category == self.detailBenchmarkItem)
{
cell.myBGView.backgroundColor = Rgb2UIColor(200, 255, 200);
}
else
{
cell.myBGView.backgroundColor = [UIColor whiteColor];
}
}
NSLog(#"cell.backgroundColor is %#",cell.backgroundColor);
NSLog(#"This row says %#",thisActivity.name);
cell.activityLabel.text = thisActivity.name;
cell.categoryLabel.text = thisActivity.category;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat: #"MM/dd/yyyy hh:mm:ss"];
cell.fromDateLabel.text = [dateFormat stringFromDate:thisActivity.startTime];
cell.toDateLabel.text = [dateFormat stringFromDate:thisActivity.stopTime];
}
Many thanks for taking the time to look! All help appreciated, even if it's my stupid mistake.
I was under the impression that a UITableViewCell has an actual property of backgroundView, I can see you have something called BGView, but I can't see anywhere from your example where the cell property gets set.
So all I can say is that for my examples where I have done this, I always create a UIView and set it's colour, then assign it to the backgroundView property.
I hope this might be of some help and that I didn't miss you doing that in your example earlier!
// Effectively draws the cell with a red background
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
backView.backgroundColor = [UIColor redColor];
cell.backgroundView = backView;
Thanks
Adrian_H
I never use Rgb2UIColor before, I don't know how it works.
Try the below code, it works on my app:-
inside willDisplayCell
CGFloat nRed=255/255.f;
CGFloat nGreen= 215/255.f;
CGFloat nBlue = 215/255.f;
UIColor * myColor = [UIColor colorWithRed:nRed green:nGreen blue:nBlue alpha:1]; //Light Red
cell.textLabel.backgroundColor = myColor;
cell.detailTextLabel.backgroundColor = myColor;
cell.backgroundColor =myColor;
[cell.backgroundView setBackgroundColor:myColor];
I hope those who looked at and especially those who took the time to answer this question can have a sense of humor about this:
It turns out that both of the answers graciously contributed above will work (with minor mods). I feel like a bit of a dummy, but the problem I was encountering wasn't with the setting of the background color, it was with the logic itself. Namely, I was using this code in my if statement:
if (thisActivity.name == self.detailFocusItem)
when, since both the objects in question are NSStrings, it should have been:
if ([thisActivity.name isEqualToString:self.detailFocusItem])
Thus, control was simply skipping the color-setting code. :/
I really do apologize for taking your time.
OTOH, had it not been for having been forced to take a new look at it as I implemented your suggestions, I don't know how long it might have taken me to see the forest instead of the trees.
You both get upvotes, and I hope you'll be gentle and chalk it up to the inexperience of a nooby enthusiast.
Many thanks for helping!

Border of each UIView

I have to create something similar to above mentioned image. I have a view and i need border after eat view like {breakfast, brunch,etc..}
for giving border I was have following code.
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, 43.0f, menuHeadView.frame.size.width, 1.0f);
bottomBorder.backgroundColor = [UIColor colorWithWhite:0.8f
alpha:1.0f].CGColor;
[menuHeadView.layer addSublayer:bottomBorder];
it works fine for single view but if I have an array for those views and I want to have border after each view using indexPath it doesn't work since it doesn't allow
[array objectAtIndex:i].layer;
help please! Thanks in advance.
If you have array of views you probably should cast:
Change :
[array objectAtIndex:i].layer;
To:
((UIView*)[array objectAtIndex:i]).layer;
EDIT:
Based on #Mischa comment:
Casting is telling the array what object is in it. If you have different kind of objects in array (you really shouldn't!) or to be super safe you can change casting to assign:
id myUnknownObject = [array objectAtIndex:i];
if([myUnknownObject isKindOfClass:[UIView class]]) {
((UIView*)myUnknownObject).layer...
} else {
NSLog(#"%#",[myUnknownObject class]);
}
But base on your comment you contain NSStrings in your array so you can't add layer to this objects. Please check your console, it's probably NSString.
Of course since you mentioned indexPath, and if you're using TableView with rows. You should use the good stuff from reusability and it method:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSDictionary *cellDic = [self.heartbeats objectAtIndex:indexPath.row];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//here goes reusability part like styling and stuff, this will calling only once for every reusable cell. So for example for cells height 44 this will called only 13 times for iPhone screen.
//HERE you should manipulate with layer!
}
//here goes your custom text of every cell - like "Breakfast", "Brunch" and so on. This will calls for every row which is about to show up.
//HERE you should manipulate with text
return cell;
}

my chat system looks a little wierd, cant get dynamic height for cell

Ive got a chat system in my app, and im attempting to make dynamic cells to have dynamic height according to how much text is in the cell, pretty common thing people try to do, however i cant get to get mine working properly.
Also the messages align to the right, the sender is supposed to be on the left and the reciever should be on the right... heres what i have done with the storyboard.
created a TableView with 2 dynamic prototypes, inside a UIViewControllerhere is the viewController for that... each cell has a label, one left one right, the whole right and left thing work... heres my issue. Its only pulling to the right for all, so basically my if isnt happening and my else is overruling. Heres a SS.
So i have two issues... Text wont have multiple lines... along with wont do dynamic height, also... if someone can point me i the right dirrection for getting sender and reciever to show on different sides.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *myWords = [[getMessage objectAtIndex:indexPath.row] componentsSeparatedByString:#":oyr4:"];
if (myWords[1] == [MyClass str]){
static NSString *sender = #"sender";
UITableViewCell* cellSender = [_tableView dequeueReusableCellWithIdentifier:sender];
messageContentTo = (UILabel *)[cellSender viewWithTag:83];
self->messageContentTo.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
self->messageContentTo.lineBreakMode = NSLineBreakByWordWrapping;
[self->messageContentTo sizeToFit];
messageContentTo.text = myWords[4];
return cellSender;
} else {
static NSString *reciever = #"reciever";
UITableViewCell* cellReciever = [_tableView dequeueReusableCellWithIdentifier:reciever];
messageContentFrom = (UILabel *)[cellReciever viewWithTag:84];
messageContentFrom.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
messageContentFrom.lineBreakMode = NSLineBreakByWordWrapping;
messageContentFrom.font = [UIFont systemFontOfSize:22];
messageContentFrom.numberOfLines = 0;
messageContentFrom.text = myWords[4];
return cellReciever;
}
}
#pragma mark - UITableViewDelegate methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize size = [[getMessage objectAtIndex:indexPath.row]
sizeWithFont:[UIFont systemFontOfSize:22]
constrainedToSize:CGSizeMake(1000, CGFLOAT_MAX)];
return size.height + 15;
}
The left-right problem might be due to this:
if (myWords[1] == [MyClass str])
If myWords[1] is a string, you need to use isEqualToString: not "==" to compare it.
if ([myWords[1] isEqualToString:[MyClass str]])
As far as the label height not adjusting properly, it's hard to tell what's going on without knowing how your labels are set up. I usually do it by making constraints between the label and the top and bottom of the cell in IB. That way, when you change the height of the cell, the label will follow (and of course, set numberOfLines to 0). Also, in your sizeWithFont:constrainedToSize: method, the width you pass into CGSizeMake() should be the width of the label, not 1000.

UITable does not scroll smoothly and cells show wrong info in IOS

I have an UITable with just 6 custom cells. Each CustomCell have a horizontal scroll view which have custom Views into it. Each CustomView has a ImageView and Text on it.
So to put it all together it may look like this
UITable --> CustomCell ---> Horizontal ScrollView --> CustomView --> ImageView and Text
Here is the code for Cell in UITable
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MySecondIdentifier = #"MySecondIdentifier";
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:MySecondIdentifier];
if(cell2 == nil){
cell2 = [(CustomCell* )[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MySecondIdentifier target:self row:indexPath.row parent:self];
}
[cell2 setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell2 setValueToCellViewItem:tempTitleString setOfImage:dictCatData];
return cell2;
}
where DictCatData = NSMutableArray of data nodes
and tempTitleString = Title string for the cell (using it for some other purpose)
Here is how I set a CustomCell values
- (void) setValueToCellViewItem:(NSString *)pTitle setOfImage:(NSMutableArray *)catData{
[the_pScrolView setContentSize:CGSizeMake([catData count] * 107 , 107)];
int counter = 0;
for(NSDictionary *tempDict in catData){
NSString *url = [[NSString alloc]init];
url = [tempDict objectForKey:#"main_img_url"];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
UIImageView *mSdWebImage = [[UIImageView alloc] initWithFrame:CGRectMake(counter * 107, 0, 107, 107)];
[mSdWebImage setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil];
[mSdWebImage setBackgroundColor:[UIColor grayColor]];
[the_pScrolView addSubview:mSdWebImage];
///Setting the title properties
UILabel *the_pLable = [[UILabel alloc] initWithFrame:CGRectMake((counter * 107) + 15, 85, 97, 22)];
the_pLable.textColor = [UIColor whiteColor];
the_pLable.font = [UIFont fontWithName:#"Helvetica" size:10.0];
the_pLable.numberOfLines = 1;
the_pLable.backgroundColor = [UIColor clearColor];
the_pLable.text = [tempDict objectForKey:#"title"];
[the_pScrolView addSubview:the_pLable];
counter++;
}
I am using SDWebImage for async downloading and caching as I think thats the best we have on net.
The ScrollView can contain images ranging from 0 to 30+ images
When I open this page on my iPhone, the images are getting downloaded an cached properly I guess, as I am able to see them with no difficulties
My Problem are
When I try to scroll up and down the table, the scrolling is not smooth. So how can I make it more smoother without effecting the background image downloading and caching
When I scroll the table up and down several times, the custom cells are redrawn I guess so the CustomCells with no images (i.e. no customViews in scrollView) show the images from other custom cells below/top.
Sometimes the app crashes, I guess this is issue of memory management.
How big are the images being downloaded? I had similar issues (without the extra horizontal scrolling) and I was able to fix it by using actual thumbnails of the images instead of the actual images (in the TableView).
You may want to try downloading, caching, and creating thumbnails of the images in a separate object and then letting the TableViewCell load those thumbnails of the images instead of the actual images.
This sped up scrolling perfectly for me.
To fix the reused cells showing wrong images, simply remove the images in cellForRowAtIndexPath before calling your code to display new images. That way if the images are delayed, at least the old ones have been removed or hidden.

Creating an Instagram-like photo feed

I'm trying to make a photo feed for an app I'm making, similar to Instagram's:
Instagram photo feed
I've created a preliminary version using a UITableView for the feed, but it's becoming a hassle dealing with the dynamic nature of each cell. The number of likes and comments along with the comment text itself will determine how tall the cell is. I used UITextViews to draw the comment and like text, and approximate the cell height and positioning using the UITextViews' text with sizeWithFont. This solution seems very imprecise and has a lot of downsides.
I was thinking about using a UIWebView for the entire feed as an alternate solution. It would make positioning the like and comment text extremely simple, along with the ability to have variable font in the text as seen in Instagram. I haven't really used UIWebViews extensively, so I'm not sure how easy or hard it would be to create the whole feed this way.
Should I continue using my UITableView solution or look into redoing it all using a UIWebView?
In my app, I've got a UITableViewCell that dynamically adjusts its height based on the amount of text in the cell. I think it might help answer your question.
Here's the trick: There is no need to "approximate text sizes" --- check out the documentation for the method [NSString sizeWithFont: constrainedToSize: lineBreakMode:]
maximumSize = CGSizeMake(self.contentView.bounds.size.width - TEXT_LEFT_MARGIN * 2.0,
MAXIMUM_TEXT_HEIGHT);
textStringSize = [textLabel.text sizeWithFont:textLabel.font
constrainedToSize:maximumSize
lineBreakMode:textLabel.lineBreakMode];
So let's take a simple example where you want 10 pixels of whitespace above the label, and 15 pixels of whitespace below the label. You'd then generate a CGRect with the measurements above, with a height of 25 pixels added to the textStringSize.height value generated above.
Using this method means that your UITableViewCells will scale up and down nicely, regardless of the size of the comments in the different labels.
try this..
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleIdendifer=#"item";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleIdendifer];
if(cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdendifer];
}
[[cell textLabel] setText:self.str];
[[cell textLabel] setNumberOfLines:0];
[[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
[[cell textLabel] setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//add your table view cell content here
NSString *string = self.str;
NSDictionary *attributes = #{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody]};
CGRect frame = [string boundingRectWithSize:CGSizeMake(CGRectGetWidth(tableView.bounds), CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributes context:nil];
return ceilf(CGRectGetHeight(frame)+TableViewCellPadding);
}

Resources