I'm working on a personal project which is a simple quote generator.
Users can save favourite their quotes to a list, and in the list view I would like the UILabel to size responsively to the content (some quotes are short, and some are long - which can look a bit odd).
Any help would be greatly appreciated.
UILabel *favouriteLabel;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
favouriteLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, -10, 400, 140)];
}else {
favouriteLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, -20, 180, 130)];
}
favouriteLabel.backgroundColor = [UIColor clearColor];
favouriteLabel.text = [self.arrayFromFile objectAtIndex:indexPath.row];
NSLog(#"Row alloc %d", indexPath.row);
NSLog(#"Load label %#", favouriteLabel.text);
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
favouriteLabel.font = [UIFont fontWithName: #"Eras Bold ITC" size: 18];
}else if([GUIHelper isPhone5]){
favouriteLabel.font = [UIFont fontWithName: #"Dosis-Medium" size: 15];
}
else
favouriteLabel.font = [UIFont fontWithName: #"Dosis-Medium" size: 15];
favouriteLabel.textColor = [UIColor colorWithRed:68.0/255.0 green:68.0/255.0 blue:68.0/255.0 alpha:1];
favouriteLabel.textAlignment = UITextAlignmentCenter;
favouriteLabel.numberOfLines = 0;
favouriteLabel.center = CGPointMake(cell.center.x - 40, cell.center.y + 20);
favouriteLabel.tag = 900;
[cell.contentView addSubview:favouriteLabel];
UIImage *lineImg = [UIImage imageNamed:#"quote-divider#2x.png"];
CGRect lineImageFrame;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
lineImageFrame = CGRectMake(0, 120, lineImg.size.width, lineImg.size.height);
}else{
lineImageFrame = CGRectMake(0, 62, lineImg.size.width * 0.9, 0.8*lineImg.size.height);
}
UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:lineImageFrame];
lineImageView.image = lineImg;
lineImageView.center = CGPointMake(cell.center.x - 40, lineImageView.center.y + 35);
Before you add the label to the view, try [favoriteLabel sizeToFit].
Related
I want to scroll up and down in my page without using AutoLayout in Xcode. How do I make the scrolling work?
I want to show the second question label and text and add other labels and fields. But I can't scroll to see how they look after building the project
This is the viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *bg = [UIImage imageNamed:#"logo2.jpg"];
self.myImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
self.myImageView.image = bg;
self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
self.myImageView.center = self.view.center;
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.myImageView];
CGRect scrollViewRect = self.view.bounds;
self.myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect];
self.myScrollView.pagingEnabled = YES;
self.myScrollView.scrollEnabled = YES;
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width + 100, self.myScrollView.frame.size.height + 100);
[self.view addSubview:self.myScrollView];
float width = CGRectGetWidth(self.myScrollView.frame);
float height = CGRectGetHeight(self.myScrollView.frame);
float newPosition = self.myScrollView.contentOffset.y+height;
CGRect toVisible = CGRectMake(newPosition, 0, width, height);
[self.myScrollView scrollRectToVisible:toVisible animated:YES];
//FirstNameLabel
CGRect labelFirst = CGRectMake(10, 100, 100, 30);
self.firstnamelabel = [[UILabel alloc] initWithFrame:labelFirst];
self.firstnamelabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.firstnamelabel.text = #"First Name : ";
//FirstNameField
CGRect firsttextField = CGRectMake(100, 100, 200, 30);
self.firstnametext = [[UITextField alloc] initWithFrame:firsttextField];
self.firstnametext.borderStyle = UITextBorderStyleRoundedRect;
//LastNameLabel
CGRect labelLast = CGRectMake(10, 150, 100, 30);
self.lastnamelabel = [[UILabel alloc] initWithFrame:labelLast];
self.lastnamelabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.lastnamelabel.text = #"Last Name : ";
//LastNameTextField
CGRect lasttextField = CGRectMake(100, 150, 200, 30);
self.lastnametext = [[UITextField alloc] initWithFrame:lasttextField];
self.lastnametext.borderStyle = UITextBorderStyleRoundedRect;
//DateLabel
CGRect labeldate = CGRectMake(10, 190, 100, 30);
self.DateOfBirthLabel = [[UILabel alloc] initWithFrame:labeldate];
self.DateOfBirthLabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.DateOfBirthLabel.text = #"Date Of Birth: ";
//DatePicker
CGRect datepickersize=CGRectMake(10, 210, 300, 150);
self.datepicker=[[UIDatePicker alloc] initWithFrame:datepickersize];
NSDate *date= self.datepicker.date;
self.datepicker.datePickerMode=UIDatePickerModeDate;
//Email
CGRect labelemail = CGRectMake(30, 370, 100, 30);
self.emaillabel = [[UILabel alloc] initWithFrame:labelemail];
self.emaillabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.emaillabel.text = #"Email : ";
//EmailText
CGRect emailtextField = CGRectMake(100, 370, 200, 30);
self.emailtext = [[UITextField alloc] initWithFrame:emailtextField];
self.emailtext.borderStyle = UITextBorderStyleRoundedRect;
//PasswordLabel
CGRect labelpassword = CGRectMake(10, 410, 100, 30);
self.passwordlabel = [[UILabel alloc] initWithFrame:labelpassword];
self.passwordlabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.passwordlabel.text = #"Password : ";
//PasswordText
CGRect passwordtextField = CGRectMake(100, 410, 180, 30);
self.passwordtext = [[UITextField alloc] initWithFrame:passwordtextField];
self.passwordtext.borderStyle = UITextBorderStyleRoundedRect;
self.passwordtext.secureTextEntry = true;
//eyebutton
UIImage *eye = [UIImage imageNamed:#"eye.png"];
self.eyebutton = [UIButton buttonWithType:UIButtonTypeCustom];
self.eyebutton.frame = CGRectMake(290, 415, 20, 20);
[self.eyebutton setImage:eye forState:UIControlStateNormal];
[self.eyebutton addTarget:self action:#selector(touchDown) forControlEvents:UIControlEventTouchDown];
[self.eyebutton addTarget:self action:#selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside];
//FirstQuetionLabel
CGRect labelfq = CGRectMake(10, 450, 300, 20);
self.firstQuestlabel = [[UILabel alloc] initWithFrame:labelfq];
self.firstQuestlabel.font = [UIFont boldSystemFontOfSize:14.0f];
self.firstQuestlabel.text = #"Question 1: In which city you were born in?";
//FirstQuestionTextField
CGRect fqtextField = CGRectMake(10, 470, 240, 30);
self.firstQuestionText = [[UITextField alloc] initWithFrame:fqtextField];
self.firstQuestionText.borderStyle = UITextBorderStyleRoundedRect;
//SecondQuestionLabel
CGRect labelsq = CGRectMake(10, 500, 300, 30);
self.secondQuestLabel = [[UILabel alloc] initWithFrame:labelsq];
self.secondQuestLabel.font = [UIFont boldSystemFontOfSize:13.0f];
self.secondQuestLabel.text = #"Question 2: What is your mother's \rmiddle name?";
//SecondQuestionTextField
CGRect sqtextField = CGRectMake(10, 630, 240, 30);
self.secondQuestionText = [[UITextField alloc] initWithFrame:sqtextField];
self.secondQuestionText.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.eyebutton];
[self.view addSubview:self.firstnamelabel];
[self.view addSubview:self.firstnametext];
[self.view addSubview:self.lastnamelabel];
[self.view addSubview:self.lastnametext];
[self.view addSubview:self.DateOfBirthLabel];
[self.view addSubview:self.datepicker];
[self.view addSubview:self.emaillabel];
[self.view addSubview:self.emailtext];
[self.view addSubview:self.passwordlabel];
[self.view addSubview:self.passwordtext];
[self.view addSubview:self.firstQuestlabel];
[self.view addSubview:self.firstQuestionText];
[self.view addSubview:self.secondQuestLabel];
[self.view addSubview:self.secondQuestionText];
}
At first glance, scrolling may not be enabled because you have not set the contentSize property of the scroll view. To set allow scrolling, the contentSize needs to be set to be larger than the frame of the scroll view.
An example in objective-c would be:
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width + 20, self.myScrollView.frame.size.height + 20);
Updated for edited question
Any views you want to be scrollable within your scroll view need to be added as a subview on the scrollView i.e
[self.myScrollView addSubview: myLabel]; //do for all views that you want to be scrollable in the scrollView
Then you need find the sum height of all the views contained in your scroll view i.e
CGFloat sumHeight = myLabel1.frame.size.height + myView2.frame.size.height; //etc do for all views you want in your scrollView
then set it to be your contentSize ie
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width, sumHeight);
I have Manager UIViewController class. Draw Chart UIView is one of the components in Manager ViewController. It is shown in the attached picture.
In Manager.m, I set the size of DrawChart UIView.
- (IBAction)viewRecordButtonisPressed:(id)sender {
//set UIView size
CGRect frame = self.drawChart.frame;
frame.origin.x = 0;
frame.origin.y = (int)((float)self.view.bounds.size.height/6.0);
frame.size.width = self.view.bounds.size.width;
frame.size.height = (int)((float)self.view.bounds.size.height*(2.0/3.0));
self.drawChart.frame = frame;
self.drawChart.hidden = NO;
[self.drawChart setNeedsDisplay];
}
Then in DrawChart.m, I added three labels. Then Draw Chart's UIView size is changed. How to write on UIView without changing the UIView's size?
- (void)drawRect:(CGRect)rect {
if(![currentNameID isEqualToString:#""]){
[self extractInformation];
UILabel *idLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
[idLabel setTextColor:[UIColor whiteColor]];
[idLabel setBackgroundColor:[UIColor clearColor]];
[idLabel setFont:[UIFont fontWithName: #"Didot-Italic" size: 15.0f]];
NSString* idtext = #"ID: ";
idLabel.text = [idtext stringByAppendingString:currentNameID];
[self addSubview:idLabel];
UILabel *dobLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 20)];
[dobLabel setTextColor:[UIColor whiteColor]];
[dobLabel setBackgroundColor:[UIColor clearColor]];
[dobLabel setFont:[UIFont fontWithName: #"Didot-Italic" size: 15.0f]];
idtext = #"DOB: ";
dobLabel.text = [idtext stringByAppendingString:dob];
[self addSubview:dobLabel];
UILabel *genderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 300, 20)];
[genderLabel setTextColor:[UIColor whiteColor]];
[genderLabel setBackgroundColor:[UIColor clearColor]];
[genderLabel setFont:[UIFont fontWithName: #"Didot-Italic" size: 15.0f]];
idtext = #"Gender: ";
genderLabel.text = [idtext stringByAppendingString:gender];
[self addSubview:genderLabel];
}
}
Not sure what the problem is. The way I solved is I used CoreGraphic to write text. It doesn't change the UIView's size.
I replaced with
- (void)drawRect:(CGRect)rect {
if(![currentNameID isEqualToString:#""]){
[self extractInformation];
NSDictionary *textAttributes = #{NSFontAttributeName: [UIFont fontWithName: #"Didot-Italic" size: 15.0f], NSForegroundColorAttributeName:[UIColor whiteColor]};
// Create string drawing context
NSStringDrawingContext *drawingContext = [[NSStringDrawingContext alloc] init];
CGRect drawRect = CGRectMake(10, 10, 300, 20);
NSString* idtext = #"ID: ";
idtext = [idtext stringByAppendingString:currentNameID];
[idtext drawWithRect:drawRect
options:NSStringDrawingUsesLineFragmentOrigin
attributes:textAttributes
context:drawingContext];
CGRect drawRect1 = CGRectMake(10, 30, 300, 20);
idtext = #"DOB: ";
idtext = [idtext stringByAppendingString:dob];
[idtext drawWithRect:drawRect1
options:NSStringDrawingUsesLineFragmentOrigin
attributes:textAttributes
context:drawingContext];
CGRect drawRect2 = CGRectMake(10, 50, 300, 20);
idtext = #"Gender: ";
idtext = [idtext stringByAppendingString:gender];
[idtext drawWithRect:drawRect2
options:NSStringDrawingUsesLineFragmentOrigin
attributes:textAttributes
context:drawingContext];
}
}
I am working on one project where i want to use pagination in uitableview without using api. I am having a nsdictionary and i kept that dictionary in an nsarray ,now i want that in a list view type there are 10,20,30 numbers and which number i select that amount of rows will display in UI.
I have done this in uitableview
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *string1 = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string1];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string1];
UILabel *label1 = (UILabel*)[cell.contentView viewWithTag:401];
CGFloat widthLabelHeader = CGRectGetWidth(orderTable.frame)/7;
if(!label1)
{
label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(0, 0, widthLabelHeader, 45);
label1.layer.borderWidth = 0.5;
label1.font = [UIFont fontWithName:#"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label1];
}
label1.textAlignment = NSTextAlignmentCenter;
UILabel *label2 = (UILabel*)[cell.contentView viewWithTag:402];
if(!label2)
{
label2 = [[UILabel alloc]init];
label2.frame = CGRectMake(CGRectGetMaxX(label1.frame), 0, widthLabelHeader, 45);
label2.layer.borderWidth = 0.5;
label2.font = [UIFont fontWithName:#"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label2];
}
label2.textAlignment = NSTextAlignmentCenter;
UILabel *label3 = (UILabel*)[cell.contentView viewWithTag:403];
if(!label3)
{
label3 = [[UILabel alloc]init];
label3.frame = CGRectMake(CGRectGetMaxX(label2.frame), 0, widthLabelHeader, 45);
label3.layer.borderWidth = 0.5;
label3.font = [UIFont fontWithName:#"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label3];
}
label3.textAlignment = NSTextAlignmentCenter;
UILabel *label4 = (UILabel*)[cell.contentView viewWithTag:404];
if(!label4)
{
label4 = [[UILabel alloc]init];
label4.frame = CGRectMake(CGRectGetMaxX(label3.frame), 0, widthLabelHeader, 45);
label4.layer.borderWidth = 0.5;
label4.font = [UIFont fontWithName:#"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label4];
}
label4.textAlignment = NSTextAlignmentCenter;
UILabel *label5 = (UILabel*)[cell.contentView viewWithTag:405];
if(!label5)
{
label5 = [[UILabel alloc]init];
label5.frame = CGRectMake(CGRectGetMaxX(label4.frame), 0, widthLabelHeader, 45);
label5.layer.borderWidth = 0.5;
label5.font = [UIFont fontWithName:#"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label5];
}
label5.textAlignment = NSTextAlignmentCenter;
UILabel *label6 = (UILabel*)[cell.contentView viewWithTag:406];
if(!label6)
{
label6 = [[UILabel alloc]init];
label6.frame = CGRectMake(CGRectGetMaxX(label5.frame), 0, widthLabelHeader, 45);
label6.layer.borderWidth = 0.5;
label6.font = [UIFont fontWithName:#"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label6];
}
label6.textAlignment = NSTextAlignmentCenter;
UILabel *label7 = (UILabel*)[cell.contentView viewWithTag:407];
if(!label7)
{
label7 = [[UILabel alloc]init];
label7.frame = CGRectMake(CGRectGetMaxX(label6.frame), 0, widthLabelHeader, 45);
label7.layer.borderWidth = 0.5;
label7.font = [UIFont fontWithName:#"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label7];
}
label7.textAlignment = NSTextAlignmentCenter;
dict1 = [array1 objectAtIndex:indexPath.row];
if( dict1)
{
label1.text = [dict1 objectForKey:#"key1" ];
label1.backgroundColor = [UIColor lightGrayColor];
label2.text = [dict1 objectForKey:#"key2" ];
label2.backgroundColor = [UIColor lightGrayColor];
label3.text = [dict1 objectForKey:#"key3" ];
label3.backgroundColor = [UIColor lightGrayColor];
label4.text = [dict1 objectForKey:#"key4" ];
label4.backgroundColor = [UIColor lightGrayColor];
label5.text = [dict1 objectForKey:#"key5" ];
label5.backgroundColor = [UIColor lightGrayColor];
label6.text = [dict1 objectForKey:#"key6" ];
label6.backgroundColor = [UIColor lightGrayColor];
label7.text = [dict1 objectForKey:#"key7" ];
label7.backgroundColor = [UIColor lightGrayColor];
}}
return cell;
}
Please Help me how to do this?
use this method is method and call Api and append in same array which used in table datasource
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
BOOL endOfTable = (scrollView.contentOffset.y >= ((showListingArray.count * 60) - scrollView.frame.size.height)); // Here 40 is row height
nextPage ++;
NSString *nextPageStr = [NSString stringWithFormat:#"%d", nextPage];
int totalpages = [[PaginnationDic objectForKey:#"totalPages"]intValue];
if ( endOfTable && !scrollView.dragging && !scrollView.decelerating)
{
if (nextPage >= totalpages) {
// NSLog(#"No More Page to load");
return;
}
objPCDetailTableView.tableFooterView = footerView;
NSDictionary *parametrs = #{#"movieid":_movieID, #"page":nextPageStr, #"cityId":[Utility getCityID]};
[self getMoviesDetailFromApi:parametrs];
[(UIActivityIndicatorView *)[footerView viewWithTag:10] startAnimating];
}
}
You should set this deleget method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
with number you entered in text field and make this call [tableView reloadData]
or use this delegate methods
- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
to insert just new rows into you tableView.
I'm working on my first app which is a simple quote generator. I have run into a couple of problems and I'm kind of out of my depth here (the freelancer I hired has done a half-baked job, and I'm trying to fix it myself now that I'm out of cash). Any pointers would be very helpful.
There is a list view of favourited quotes, and the space provided per quote is fixed - which looks odd when the quotes vary in size (there's either lots of empty space or the quote cuts off). I think the snippet below is where this can be amended, as it's where I changed the text alignment.
Also I made some edits to the text file which the app pulls the quotes from, but they are only sometimes reflected when the app is running in the simulator... This seems really strange that some of the edits work and some don't.
Any suggestions highly appreciated as I've tried everything I can think of through trial and error.
UILabel *favouriteLabel;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
favouriteLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, -10, 400, 140)];
}else{
favouriteLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, -20, 160, 100)];
}
favouriteLabel.backgroundColor = [UIColor clearColor];
favouriteLabel.text = [self.arrayFromFile objectAtIndex:indexPath.row];
NSLog(#"Row alloc %d", indexPath.row);
NSLog(#"Load label %#", favouriteLabel.text);
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
favouriteLabel.font = [UIFont fontWithName: #"Eras Bold ITC" size: 18];
}else if([GUIHelper isPhone5]){
favouriteLabel.font = [UIFont fontWithName: #"Dosis-Medium" size: 14];
}
else
favouriteLabel.font = [UIFont fontWithName: #"Dosis-Medium" size: 14];
favouriteLabel.textColor = [UIColor blackColor];
favouriteLabel.textAlignment = UITextAlignmentCenter;
favouriteLabel.numberOfLines = 0;
favouriteLabel.center = CGPointMake(cell.center.x - 40, cell.center.y + 20);
favouriteLabel.tag = 900;
[cell.contentView addSubview:favouriteLabel];
UIImage *lineImg = [UIImage imageNamed:#"quote-divider#2x.png"];
CGRect lineImageFrame;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
lineImageFrame = CGRectMake(0, 110, lineImg.size.width, lineImg.size.height);
}else{
lineImageFrame = CGRectMake(0, 62, lineImg.size.width * 0.9, 0.8*lineImg.size.height);
}
UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:lineImageFrame];
lineImageView.image = lineImg;
lineImageView.center = CGPointMake(cell.center.x - 40, lineImageView.center.y + 35);
// set the tag myImageView.tag = 1010;
[cell.contentView addSubview:lineImageView];
I'm trying to have a UITableView with variable cell heights. The height should be based on the height of an image fetched from the server. I'm having some difficulties though. This is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
CGFloat height = (0.5*(self.view.frame.size.width-10))+100;
UILabel *heightLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-35, 30)];
heightLabel.text = [self.streams[indexPath.row] excerpt];
heightLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
heightLabel.numberOfLines = 3;
[heightLabel sizeToFit];
height = height + heightLabel.frame.size.height;
NSLog(#"Cell height is %f", height);
cell.backgroundColor = [UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, cell.frame.size.height-20)];
backgroundView.backgroundColor = [UIColor whiteColor];
backgroundView.tag = 1;
[cell addSubview:backgroundView];
NSURL *imageLink = [NSURL URLWithString:[self.streams[indexPath.row] image]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, 0.5*(self.view.frame.size.width-10))];
__block UIActivityIndicatorView *activityIndicator;
__weak UIImageView *weakImageView = imageView;
imageView.tag = 2;
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
[cell addSubview:imageView];
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, imageView.frame.origin.y + imageView.frame.size.height + 10, self.view.frame.size.width-35, 30)];
nameLabel.text = [self.streams[indexPath.row] name];
nameLabel.textColor = [UIColor blackColor];
nameLabel.textAlignment = NSTextAlignmentLeft;
nameLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:16];
nameLabel.numberOfLines = 2;
[nameLabel sizeToFit];
nameLabel.tag = 3;
[cell addSubview:nameLabel];
UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, nameLabel.frame.origin.y + nameLabel.frame.size.height + 5, self.view.frame.size.width-35, 30)];
descLabel.text = [NSString stringWithFormat:#"Posted by %#", [self.streams[indexPath.row] author]];
descLabel.textColor = [UIColor colorWithRed:193.0/255.0 green:193.0/255.0 blue:193.0/255.0 alpha:1.0];
descLabel.textAlignment = NSTextAlignmentLeft;
descLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:12];
descLabel.numberOfLines = 2;
[descLabel sizeToFit];
descLabel.tag = 5;
[cell addSubview:descLabel];
UILabel *websiteLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, descLabel.frame.origin.y + descLabel.frame.size.height + 5, self.view.frame.size.width-35, 30)];
websiteLabel.text = [self.streams[indexPath.row] excerpt];
websiteLabel.textColor = [UIColor colorWithRed:119.0/255.0 green:119.0/255.0 blue:119.0/255.0 alpha:1.0];
websiteLabel.textAlignment = NSTextAlignmentLeft;
websiteLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
websiteLabel.numberOfLines = 3;
[websiteLabel sizeToFit];
websiteLabel.tag = 4;
[cell addSubview:websiteLabel];
[imageView sd_setImageWithURL: imageLink
placeholderImage:[UIImage imageNamed:#"default.png"]
options:SDWebImageProgressiveDownload
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
if (!activityIndicator) {
[weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
activityIndicator.center = weakImageView.center;
[activityIndicator startAnimating];
}
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image) {
float newHeight;
#try {
[activityIndicator removeFromSuperview];
activityIndicator = nil;
float oldWidth = image.size.width;
float scaleFactor = imageView.frame.size.width / oldWidth;
newHeight = image.size.height * scaleFactor;
}
#catch (NSException *exception) {
newHeight = 0.5*(self.view.frame.size.width-10);
}
#finally {
imageView.image = image;
imageView.frame = CGRectMake(10, 10, self.view.frame.size.width-20, newHeight);
nameLabel.frame = CGRectMake(15, imageView.frame.origin.y + newHeight + 10, self.view.frame.size.width-35, 30);
[nameLabel sizeToFit];
descLabel.frame = CGRectMake(15, nameLabel.frame.origin.y + nameLabel.frame.size.height + 5, self.view.frame.size.width-35, 30);
[descLabel sizeToFit];
websiteLabel.frame = CGRectMake(15, descLabel.frame.origin.y + descLabel.frame.size.height + 5, self.view.frame.size.width-35, 30);
[websiteLabel sizeToFit];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self.tableView beginUpdates];
[cell setFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, newHeight+nameLabel.frame.size.height+descLabel.frame.size.height+websiteLabel.frame.size.height+50)];
[self.tableView endUpdates];
backgroundView.frame = CGRectMake(10, 10, self.view.frame.size.width-20, newHeight+nameLabel.frame.size.height+descLabel.frame.size.height+websiteLabel.frame.size.height+25);
}
}
}];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100.0f;
}
What am I doing wrong? Currently, cells are overlapping....
If your cells has variable height you should set AutomaticDimension
in viewDidLoad set this:
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 100
and you can eliminate heightForRowAtIndexPath or set it to automatic
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension; //works only about iOS 8
}