Overlapping Text in UITableViewHeaderFooterView - ios

Here i mention the code what i wrote , In UITableViewHeaderFooterView while scrolling and second time that method is calling text was overlapping (How many time scrolling that much time text are adding), Here i mention the Screen shots : http://prntscr.com/9sgq7x
_imgViewUser.imageURL = [NSURL URLWithString:modelData.user_image_big];
_lblUserName.text = modelData.posted_by;
_lblTime.text = modelData.posted_time;
if ([modelData.commentArray count])
{
_imgViewUser.imageURL = [NSURL URLWithString:[[modelData.commentArray objectAtIndex:0] valueForKey:#"cmt_user_small"]];
NSString *nameStr = #"";
if ([GETVALUE(kUSERID) isEqualToString:[[modelData.commentArray objectAtIndex:0] valueForKey:#"cmt_userid"]]) {
nameStr = #"You";
}else {
nameStr = [[modelData.commentArray objectAtIndex:0] valueForKey:#"full_name"];
}
_lblUserName.text = nameStr;
_lblTime.text = [[modelData.commentArray objectAtIndex:0] valueForKey:#"cmt_post_time"];
}
NSString *OriginalFrontendText = [[modelData.commentArray objectAtIndex:0] valueForKey:#"cmt_text"];
UIFont *font = [UIFont fontWithName:#"Arial" size:13.0];
CGFloat descriptionHeight = [OriginalFrontendText boundingRectWithSize:CGSizeMake(180, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName: font} context:nil].size.height;
[[self viewWithTag:tag]removeFromSuperview];
STTweetUserName *commentLbl;
if ([modelData.comments_count intValue] > 1) {
commentLbl = [[STTweetUserName alloc]initWithFrame:CGRectMake(0, 39, self.frame.size.width-10, descriptionHeight) withFont:9.0 attributeTextColor:[UIColor darkGrayColor] name:#"See More"];
commentLbl.text = [NSString stringWithFormat:#"%#See More",[[modelData.commentArray objectAtIndex:0] valueForKey:#"cmt_text"]];
}else
{
commentLbl = [[STTweetUserName alloc]initWithFrame:CGRectMake(0, 39, self.frame.size.width-10, descriptionHeight) withFont:9.0 attributeTextColor:[UIColor darkGrayColor] name:nil];
commentLbl.text = [[modelData.commentArray objectAtIndex:0] valueForKey:#"cmt_text"];
}
commentLbl.tag = tag;
if (commentLbl.tag == tag) {
[commentLbl removeFromSuperview];
}
[self addSubview:commentLbl];
commentLbl.detectionBlock = ^(STTweetHotUserName hotWord, NSString *string, NSString *protocol, NSRange range){
showAlert(string, nil, #"OK", nil);
//[self SeeMoreClicked];
};
self.frame = CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, 150);
i am getting overlopping while scrolling

Use following code to make your footer view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView * v = [[UIView alloc] init];
v.frame = CGRectMake(0, 0, tableView.frame.size.width, 44);
UILabel * label = [[UILabel alloc] init];
label.frame = CGRectMake(20, 0, tableView.frame.size.width/2, 44);
label.text = #"Some text";
[v addSubview:label];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(label.frame.size.width+label.frame.origin.x, 0, tableView.frame.size.width/2, 44);
[btn addTarget:self action:#selector(seeMore) forControlEvents:UIControlEventTouchUpInside];
btn.titleLabel.text = #"See more";
[v addSubview:btn];
return v;
}
Customise the above code however you want. This won't let your footer view overlap with text.
Get width of NSString text using
- (CGSize)getHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height+20.0f);
}
return size;
}

Related

UITextField will not become first responder when touched, none of the usual mistakes apply

I have a UITextField that will not become the first responder when tapped. I can assign it to become first responder, which works find. But if it resigns first responder status and I try and tap it or tab back to it to make it become the first responder again, nothing happens. It appears as if the touch is being trapped somewhere, and yet I can't find anything in my code that could be causing that to happen. I've checked the usual suspects:
If the textfield the top view
is the textfield within the bounds of it's superview
is the textfield userEnabled.
I've also rewritten the code in several different ways, to no avail.
Can anyone help me with this problem. The textField at issue is the field titled answerTextField in the method createOneDoubleViewAtOrigin.
The relevant code is below.
-(instancetype)initForProblem:(NSString *)problem{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
self = [super init];
if (self) {
[self parseBasicFractionProblem:problem];
if (_problemType == fractDoubleWithPic) {
NSLog(#"placed the fract views");
UIView *firstProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(26, 30) withNumerator:_numerator1 denominator:_denominator1 forViewNumber:0];
UIView *secondProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(342,30) withNumerator:_numerator2 denominator:_denominator2 forViewNumber:1];
[self addSubview:firstProblemView];
[self addSubview:secondProblemView];
[self bringSubviewToFront:firstProblemView];
[self bringSubviewToFront:secondProblemView];
}
else if (_problemType == fractDoubleNoPicAns||_problemType == fractDoubleNoPicExtendedAns ){
}
}
self.tag = 800;
self.backgroundColor = [UIColor redColor];
NSLog(#"made to end");
return self;
}
-(UIView *)createOneDoubleViewAtOrigin:(CGPoint)viewOrigin withNumerator:(NSInteger)numerator denominator:(NSInteger)denominator forViewNumber:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UIView *containerView = [[UIView alloc] initWithFrame: CGRectMake(viewOrigin.x,viewOrigin.y, 310, 263)];
containerView.backgroundColor = [UIColor colorWithRed:178.0/255.0 green:222.0/255.0 blue:80.0/255.0 alpha:1.0];
containerView.layer.cornerRadius = 5.0;
UILabel *numeratorView = [self createSubview:CGRectMake(66, 23, 59, 47) text:[NSString stringWithFormat:#"%ld",(long)numerator] inView:containerView];
UILabel *divisorView = [self createSubview:CGRectMake(66, 40, 59, 47) text:#"___" inView:containerView];
UILabel *denominatorView = [self createSubview:CGRectMake(72, 82, 47, 47) text:[NSString stringWithFormat:#"%ld",(long)denominator] inView:containerView];
UILabel *equals = [self createSubview:CGRectMake(125, 50, 47, 47) text:#"=" inView:containerView];
/*
FFractSupportedTextField *answerField = [self createAnswerField:CGRectMake(173,50,82,47)];
*/
UITextField *answerTextField = [[UITextField alloc] initWithFrame:CGRectMake(173,50,82,47)];
//Inside
answerTextField.font = [UIFont fontWithName:#"Helvetica" size:30.0];
answerTextField.textAlignment = NSTextAlignmentCenter;
answerTextField.placeholder = #"?";
//border
answerTextField.layer.borderWidth = 1;
answerTextField.layer.borderColor = [[UIColor blackColor] CGColor];
answerTextField.layer.cornerRadius = 5.0;
answerTextField.userInteractionEnabled = YES;
[answerTextField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
containerView.tag = 820 + 6*viewNumber;
numeratorView.tag = 821 + 6*viewNumber;
divisorView.tag = 822 + 6*viewNumber;
denominatorView.tag = 823 + 6*viewNumber;
equals.tag = 824 + 6*viewNumber;
answerTextField.tag = 801 + viewNumber;
UIView *pictureView = [self createFractPictureForNumerator:numerator denominator:denominator number:viewNumber];
pictureView.tag = 825 + 6*viewNumber;
if (viewNumber == 0){
_answerTextField1 = answerTextField;
[containerView addSubview:_answerTextField1];
[containerView bringSubviewToFront:_answerTextField1];
_pictureView1 = pictureView;
[containerView addSubview:_pictureView1];
[_answerTextField1 becomeFirstResponder];
} else if (viewNumber == 1) {
_answerTextField2 = answerTextField;
[containerView addSubview:_answerTextField2];
[containerView bringSubviewToFront:_answerTextField2];
_pictureView2 = pictureView;
[containerView addSubview:_pictureView2];
}
return containerView;
}
-(UILabel *)createSubview:(CGRect)frame text:(NSString *)text inView:(UIView *)containerView{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UILabel *labelView = [[UILabel alloc] initWithFrame:frame];
labelView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
labelView.textAlignment = NSTextAlignmentCenter;
labelView.text = text;
[containerView addSubview:labelView];
return labelView;
}
-(FFractSupportedTextField *)createAnswerField:(CGRect)frame{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
FFractSupportedTextField *fieldView = [[FFractSupportedTextField alloc] initWithFrame:frame];
//Inside
fieldView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
fieldView.textAlignment = NSTextAlignmentCenter;
fieldView.placeholder = #"?";
//border
fieldView.layer.borderWidth = 1;
fieldView.layer.borderColor = [[UIColor blackColor] CGColor];
fieldView.layer.cornerRadius = 5.0;
fieldView.userInteractionEnabled = YES;
return fieldView;
}
-(UIView *)createFractPictureForNumerator:(NSInteger)numerator denominator:(NSInteger)denominator number:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
NSLog(#"numerator:%ld denominator:%ld",(long)numerator,(long)denominator);
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(33, 165, 256, 78)];
containerView.backgroundColor = [UIColor whiteColor];
containerView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
containerView.layer.borderWidth = 1.0;
containerView.layer.cornerRadius = 3.0;
NSInteger smallViewCount = denominator;
if (denominator == 0) {
smallViewCount = 1;
}
float smallWidth = 245.0/smallViewCount;
for (int n = 0; n < smallViewCount; n++) {
NSLog(#"count %d",n);
UILabel *smallLabel = [[UILabel alloc] initWithFrame:CGRectMake(8 + n*smallWidth, 8, smallWidth - 5, 29)];
smallLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
smallLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
[smallLabel setAdjustsFontSizeToFitWidth:YES];
smallLabel.textAlignment = NSTextAlignmentCenter;
smallLabel.layer.cornerRadius = 3.0;
smallLabel.tag = 830+n + viewNumber*10;
[containerView addSubview:smallLabel];
}
UILabel *largeLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 41, 240, 29)];
largeLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
largeLabel.text = [NSString stringWithFormat:#"= %ld",(long)numerator];
largeLabel.textAlignment = NSTextAlignmentCenter;
largeLabel.layer.cornerRadius = 3.0;
[containerView addSubview:largeLabel];
NSLog(#"end of createFractPictFor..");
return containerView;
}

Stop iCarousel labels from scrolling

All my labels, done button and back button are scrolling within my icarousel. Is there a way to stop them from moving with the swipe gesture or are they inherently fixed? Or is it a case of taking them out of the function all together?
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
GalleryImage* gi = [self.galleryItem.galleryImages objectAtIndex:index];
UIImageView* imageView = nil;
UILabel* positionLabel = nil;
//UILabel* titleLabel = nil;
UILabel* label = nil;
UIButton* doneButton = nil;
UIView* labelBackground = nil;
UIActivityIndicatorView * spinnerView = nil;
UILabel * errorMessage = nil;
UILabel* titleLabel = nil;
//create new view if no view is available for recycling
if (view == nil) {
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, self.carousel.frame.size.height)] ;
// Image
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, self.carousel.frame.size.height)] ;
imageView.contentMode = UIViewContentModeScaleAspectFit;// UIViewContentModeCenter;
imageView.tag = 1;
// Spiner
spinnerView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinnerView setFrame:imageView.frame];
spinnerView.tag = 11;
//Error message
errorMessage = [[UILabel alloc] initWithFrame:spinnerView.frame];
[errorMessage setTextAlignment:NSTextAlignmentCenter];
[errorMessage setTextColor:[UIColor whiteColor]];
errorMessage.font = [UIFont fontWithName:#"Helvetica-Bold" size: 14.0];
errorMessage.tag = 111;
[imageView addSubview:errorMessage];
[imageView addSubview:spinnerView];
[view addSubview:imageView];
// Position
positionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, 30)] ;
positionLabel.backgroundColor = [UIColor clearColor];
positionLabel.textAlignment = NSTextAlignmentCenter;
positionLabel.lineBreakMode = NSLineBreakByWordWrapping;
positionLabel.numberOfLines=1;
positionLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size: 14.0];
positionLabel.textColor = [UIColor whiteColor];
positionLabel.tag = 2;
//if(UIInterfaceOrientationPortrait == self.interfaceOrientation)
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
// Title
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, self.carousel.frame.size.width - 20, 100)] ;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = NSTextAlignmentLeft;
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
titleLabel.numberOfLines = 4;
titleLabel.font = [UIFont fontWithName:#"Georgia" size: 18.0];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = [NSString stringWithFormat:#"%# \n", self.galleryItem.title];
[view addSubview:titleLabel];
/*
* Manage carousel speed and deceleration rate for portrait mode
*/
//self.carousel.decelerationRate = 0.175f;
//self.carousel.scrollSpeed = 0.45f;
}
else if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
/*
* Manage carousel speed and deceleration rate for landscape mode
*/
//self.carousel.decelerationRate = 0.25f;
//self.carousel.scrollSpeed = 0.55f;
}
labelBackground = [[UIView alloc] initWithFrame:CGRectMake(0, self.carousel.frame.size.height - 160, self.carousel.frame.size.width , 160)] ;
CAGradientLayer *labelBackgroundGradient = [CAGradientLayer layer];
labelBackgroundGradient.frame = labelBackground.bounds;
labelBackgroundGradient.colors = [NSArray arrayWithObjects: (id)[[UIColor clearColor]CGColor],
(id)[[UIColor blackColor]CGColor],
nil
];
[labelBackground.layer insertSublayer:labelBackgroundGradient atIndex:0];
labelBackground.opaque = false;
labelBackground.tag = 5;
// Description
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake( 0.0f,
0.0f,
labelBackground.bounds.size.width - 20.0f,
labelBackground.bounds.size.height)] ;
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentLeft;
label.numberOfLines = 0;
label.font = [label.font fontWithSize:12];
label.textColor = [UIColor whiteColor];
label.text = gi.descriptionGalleryImage;
[label autoAdjustGalleryCaptionSize];
[label setFrame:CGRectMake( label.bounds.origin.x + 10.0f,
label.bounds.origin.y + (labelBackground.bounds.size.height - (label.bounds.size.height + 10.0f)),
label.bounds.size.width,
label.bounds.size.height
)];
label.tag = 6;
[labelBackground addSubview:label];
[view addSubview:labelBackground];
} else {
imageView = (UIImageView *)[view viewWithTag:1];
spinnerView = (UIActivityIndicatorView*)[imageView viewWithTag:11];
errorMessage = (UILabel*) [imageView viewWithTag:111];
positionLabel = (UILabel *)[view viewWithTag:2];
doneButton = (UIButton *)[view viewWithTag:3];
//titleLabel = (UILabel *)[view viewWithTag:4];
labelBackground = (UIView *)[view viewWithTag:5];
label = (UILabel*)[view viewWithTag:6];
}
//Start image loading
ImageSize* imSize = [gi getLargestImage];
[spinnerView startAnimating];
errorMessage.text = #"";
[imageView sd_setImageWithURL:[NSURL URLWithString:imSize.source] placeholderImage:nil options:SDWebImageContinueInBackground progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//progress bar could be here
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//remove progressbar
[spinnerView stopAnimating];
//Display error message
if(error)
{
//errorMessage.text = error.localizedDescription;
errorMessage.text = #"Cannot load image";
}
//Assign image
if(image)
{
[imageView setImage:image];
}
}];
positionLabel.text = [NSString stringWithFormat:#"%d of %ld", (index + 1), (long)[self.galleryItem.galleryImages count]];
UIView* v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, 30)];
v.backgroundColor = [UIColor blackColor];
v.alpha = 0.6;
[view addSubview:v];
[view addSubview:positionLabel];
// Done button
doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 0 , 50, 30);
[doneButton setImage:[UIImage imageNamed:#"back_button_white"] forState:UIControlStateNormal];
doneButton.imageEdgeInsets = UIEdgeInsetsMake(0,5,0,5);
[doneButton setTintColor:[UIColor whiteColor]];
[doneButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:doneButton];
/*
* The description. Why are we setting the frame for the label multiple times?
* We need to do this so it can autoadjust its size and position according
* to the text contained within it.
*/
[label setText:gi.descriptionGalleryImage];
[label autoAdjustGalleryCaptionSize];
[label setFrame:CGRectMake( label.bounds.origin.x + 10.0f,
label.bounds.origin.y + (labelBackground.bounds.size.height - (label.bounds.size.height + 10.0f)),
label.bounds.size.width,
label.bounds.size.height
)];
labelBackground.hidden = self.hideLandscapeDetails;
label.hidden = self.hideLandscapeDetails;
doneButton.hidden = self.hideLandscapeDetails;
positionLabel.hidden = self.hideLandscapeDetails;
v.hidden =self.hideLandscapeDetails;
titleLabel.hidden = self.hideLandscapeDetails;
self.tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showHideInfo:)];
[view addGestureRecognizer:self.tap1];
// scroll portrait view to same index
if (self.delegate) {
[self.delegate setReturnIndex:self.carousel.currentItemIndex];
}
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:#"Image Gallery" // Event category (required)
action:#"Gallery Image Viewed" // Event action (required)
label:#"Picture swipe" // Event label
value:[NSNumber numberWithLong:(index + 1)]] build]]; // Event value
return view;
}
The label should be above the iCarousel to do what you want, not inside the views of the carousel.

Variable UITableViewCell height not working

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
}

Dynamic UITableViewCell Height with variable number of subviews

I have a UITableViewCell that is populated dynamically with data retrieved from a remote source. It has a variable number of subviews based on the type of item being represented in the cell. I am dynamically calculating the size of the cell's contents by using a NSMutableAttributedString and the boundingRectWithSize: method. I take the returned values and calculate a combinedHeight variable that is stored in a NSMutableDictionary and associated with the indexPath of the cell. That all works fine, but when I attempt to use the values from the dictionary in the heightForRowAtIndexPath: method, it only uses the first set of visible cells, and after that everything is 0.
My question is how I can dynamically size the cell to contain all of the subviews. I am also adding a custom view to the cell's contentView that is smaller width than the cell so that I can achieve a unique style we are going for.
Here is my code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (areFeedItemsLoaded && feedItems.count > 0 && indexPath.row < feedItems.count)
{
CGFloat height = [[_cellHeights objectForKey:[self keyForIndexPath:indexPath]] floatValue];
return MAX(90, height);
}
return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (areFeedItemsLoaded)
{
if (feedItems.count == 0)
{
if (indexPath.row == 0)
{
cell = [EmptyTableViewCell getBlankCell:tableView];
}
else
{
EmptyTableViewCell *eCell = [EmptyTableViewCell getEmptyCell:tableView withHeaderText:#"No Recent Activity"];
[eCell setLinkText:#"Your team's activity on Hudl will appear here." withUrl:nil];
cell = eCell;
}
}
else if (indexPath.row < feedItems.count)
{
FeedItem *item = [feedItems objectAtIndex:indexPath.row];
cell = [self.tableView dequeueReusableCellWithIdentifier:FeedItemCellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[FeedItemTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FeedItemCellIdentifier];
}
cell.clipsToBounds = YES;
cell.backgroundColor = [UIColor clearColor];
CGFloat combinedHeight = 8;
UIView *customContentView = [cell viewWithTag:customContentTagId];
if (customContentView == nil)
{
customContentView = [[UIView alloc] init];
customContentView.tag = customContentTagId;
customContentView.backgroundColor = [UIColor colorWithR:100 g:100 b:100 a:1];
[cell.contentView addSubview:customContentView];
}
customContentView.frame = CGRectMake(50, 0, 270, 70);
UIImageView *userImage = (UIImageView *)[cell viewWithTag:userImageTagId];
if (userImage == nil)
{
userImage = [[UIImageView alloc] init];
userImage.tag = userImageTagId;
[customContentView addSubview:userImage];
}
userImage.frame = CGRectMake(8, 8, 45, 45);
userImage.layer.cornerRadius = CGRectGetWidth(userImage.frame) / 2;
userImage.layer.masksToBounds = YES;
NSURL *imageUrl = item.imageSourceUrl;
[userImage setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:#"default-user.png"]];
CGFloat minX = CGRectGetMaxX(userImage.frame) + 8;
UILabel *titleLabel = (UILabel *)[cell viewWithTag:titleTagId];
if (titleLabel == nil)
{
titleLabel = [[UILabel alloc] init];
titleLabel.numberOfLines = 0;
titleLabel.textColor = [UIColor colorWithR:204 g:204 b:204 a:1];
titleLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
titleLabel.tag = titleTagId;
[customContentView addSubview:titleLabel];
}
NSString *title = [NSString stringWithFormat:#"%# %#", item.creatorName, item.title];
NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:title];
[titleText addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithR:204 g:204 b:204 a:1] range:NSMakeRange(0, title.length - 1)];
[titleText addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica-Bold" size:14] range:NSMakeRange(0, item.creatorName.length)];
[titleText addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica" size:15] range:NSMakeRange(item.creatorName.length + 1, title.length - item.creatorName.length - 1)];
titleLabel.attributedText = titleText;
CGFloat width = CGRectGetWidth(customContentView.frame) - (CGRectGetMaxX(userImage.frame) + 8) - 8;
width = MIN(width, 200);
CGSize titleSize = [titleText boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
titleLabel.frame = CGRectMake(minX, CGRectGetMinY(userImage.frame), ceil(titleSize.width), ceil(titleSize.height));
combinedHeight += ceil(titleSize.height) + 8;
UIView *thumbnailsView = [cell viewWithTag:thumbnailTagId];
UILabel *descriptionLabel = (UILabel *)[cell viewWithTag:descriptionTagId];
if ([item.feedItemType intValue] == PlaylistFI || [item.feedItemType intValue] == PlaylistAndNotesFI)
{
if ([item.uris count] > 0)
{
if (thumbnailsView == nil)
{
thumbnailsView = [[UIView alloc] init];
thumbnailsView.tag = thumbnailTagId;
[customContentView addSubview:thumbnailsView];
}
else
{
for (UIView *view in thumbnailsView.subviews)
{
[view removeFromSuperview];
}
}
thumbnailsView.frame = CGRectMake(minX, CGRectGetMaxY(titleLabel.frame) + 8, width, 40);
combinedHeight += 48;
for (int i = 0; i < 3 && i < item.uris.count; i++)
{
NSURL *url = [item.uris objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc] init];
int x = i * 60 + i * 8;
imageView.frame = CGRectMake(x, 0, 60, 40);
[imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"soft-dark-grad.png"]];
[thumbnailsView addSubview:imageView];
}
}
if (descriptionLabel == nil)
{
descriptionLabel = [[UILabel alloc] init];
descriptionLabel.tag = descriptionTagId;
descriptionLabel.font = [UIFont systemFontOfSize:14];
descriptionLabel.textColor = [UIColor colorWithR:204 g:204 b:204 a:1];
descriptionLabel.numberOfLines = 0;
descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
[customContentView addSubview:descriptionLabel];
}
NSMutableAttributedString *descriptionText = [[NSMutableAttributedString alloc] initWithString:item.descriptionText];
[descriptionText addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica" size:14] range:NSMakeRange(0, item.descriptionText.length)];
descriptionLabel.attributedText = descriptionText;
CGSize descriptionSize = [descriptionText boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
descriptionLabel.frame = CGRectMake(minX, CGRectGetMaxY(thumbnailsView.frame) + 8, ceil(descriptionSize.width), ceil(descriptionSize.height));
combinedHeight += ceil(descriptionSize.height) + 8;
}
else
{
if (thumbnailsView != nil)
{
[thumbnailsView removeFromSuperview];
}
if (descriptionLabel != nil)
{
[descriptionLabel removeFromSuperview];
}
}
UILabel *messageLabel = (UILabel *)[cell viewWithTag:messageTagId];
if ([item.feedItemType intValue] == MessageFI)
{
if (messageLabel == nil)
{
messageLabel = [[UILabel alloc] init];
messageLabel.tag = messageTagId;
messageLabel.font = [UIFont systemFontOfSize:14];
messageLabel.textColor = [UIColor colorWithR:204 g:204 b:204 a:1];
messageLabel.numberOfLines = 0;
messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
[customContentView addSubview:messageLabel];
}
NSMutableAttributedString *messageText = [[NSMutableAttributedString alloc] initWithString:item.message];
[messageText addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica" size:14] range:NSMakeRange(0, item.message.length)];
CGSize messageSize = [messageText boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
messageLabel.frame = CGRectMake(minX, CGRectGetMaxY(titleLabel.frame) + 8, ceil(messageSize.width), ceil(messageSize.height));
messageLabel.attributedText = messageText;
combinedHeight += ceil(messageSize.height) + 8;
}
else
{
if (messageLabel != nil)
{
[messageLabel removeFromSuperview];
}
}
if ([item.feedItemType intValue] == PlaylistFI || [item.feedItemType intValue] == PlaylistAndNotesFI ||
[item.feedItemType intValue] == ArticleFI)
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[_cellHeights setObject:#(combinedHeight) forKey:[self keyForIndexPath:indexPath]];
CGRect frame = customContentView.frame;
frame.size.height = MAX(70, combinedHeight);
customContentView.frame = frame;
}
else if (!requestFailed)
{
cell = [LoadingTableViewCell getLoadingCell:tableView];
}
else
{
cell = [ErrorTableViewCell getErrorCell:tableView];
}
}
else
{
cell = (indexPath.row == 0 || indexPath.row < feedItems.count) ? [EmptyTableViewCell getBlankCell:tableView] : [LoadingTableViewCell getLoadingCell:tableView];
}
return cell;
}
- (NSIndexPath *)keyForIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath class] == [NSIndexPath class]) {
return indexPath;
}
return [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
}
The issue with your code is the order in which the height is calculated. If you set a breakpoint in both methods you will see thattableView:heightForRowAtIndexPath: is actually called BEFORE tableView:cellForRowAtIndexPath:. This means that doing the calculations for the height when setting up the cell will not work (as the height will return 0 before it has been set).
Here is my suggestions:
Create a UITableViewCell subclass and move all of the initialization and setup logic you have piled intableView:cellForRowAtIndexPath: in that subclass.
once a cell is setup with whatever data properties may change its height, set the frame height of the cell internally because you will use it in the next step.
Then in order to calculate the height independently, use a method in the cell itself that looks something like this:
+ (CGFloat)heightForCellWithWithData:// use whatever parameters are usually set on a cell that might affect it's height
{
static dispatch_once_t once;
static CustomCellClass *sizingCell; // use your custom cell class here
dispatch_once(&once, ^{
sizingCell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"sizingCell"];
});
// set cell data properties here
return sizingCell.frame.size.height;
}
Then to get the height you would just do:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (areFeedItemsLoaded && feedItems.count > 0 && indexPath.row < feedItems.count)
{
return [CustomCellClass heightForCellWithWithData:whateverData];
}
return 44;
}

UIScrollView set its contentOffset default?

I have a scroll view which consists of many of views. These views are placed in the view according to their dynamic size. There is also a delete button on each view which deletes the view from the scrollview, and replace the others accordingly.
But I have a problem, when I delete something scrollview does not stay where I deleted. One row up or down.
This is my deletion code:
-(void) redrawTheList:(id)sender :(NSString*) except{
int tempo = counterRow;
NSMutableDictionary *doctorTemp = [[NSMutableDictionary alloc]initWithDictionary:doctorAddition copyItems:YES];
for(int i = 0; i<=tempo;i++)
{
NSString *dictTempKey = [NSString stringWithFormat:#"row%d",i];
NSMutableArray * tempArray = [doctorTemp objectForKey:dictTempKey];
for(UIView * subview in tempArray)
{
for(UIView * subview2 in [subview subviews])
{
if([subview2 isKindOfClass:[UILabel class]])
{
if([((UILabel*)subview2).text isEqualToString:except])
{
counterRow = i;
tempile = i;
yAxis=5.0+(i*40.0);
}
}
}
}
}
for(int j=tempile;j<=tempo;j++)
{
NSString *dictTempKey2 = [NSString stringWithFormat:#"row%d",j];
NSMutableArray * tempArray = [doctorAddition objectForKey:dictTempKey2];
for(UIView * subview in tempArray)
{
[subview removeFromSuperview];
}
[doctorAddition removeObjectForKey:dictTempKey2];
}
for(int k=tempile;k<=tempo;k++)
{
NSString *dictTempKey3 = [NSString stringWithFormat:#"row%d",k];
NSMutableArray * tempArray2 = [doctorTemp objectForKey:dictTempKey3];
for(UIView * subview in tempArray2)
{
for(UIView * subview2 in [subview subviews])
{
if([subview2 isKindOfClass:[UILabel class]])
{
if(![((UILabel*)subview2).text isEqualToString:except])
{
[self createDoctorBox:((UILabel*)subview2).text];
}
}
}
}
}
}
CreateDoctorBox method;
-(UIView*)createDoctorBox : (NSString*)name {
[addedList addObject:name];
NSString *myString = name;
CGSize stringSize = [myString sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:13]];
UIView *doktorKutu = [[UIView alloc]init];
[doctorList addSubview:doktorKutu];
UIView *doktorKutuBas = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 18, 36)];
[doktorKutuBas setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"doktor_isim_kutu_bas"]]];
[doktorKutu addSubview:doktorKutuBas];
UILabel * doktorKutuGovde = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, stringSize.width+3, 36)];
[doktorKutuGovde setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[doktorKutuGovde setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"doktor_isim_kutu_1px"]]];
[doktorKutuGovde setUserInteractionEnabled:YES];
[doktorKutuGovde setText:myString];
[doktorKutu addSubview:doktorKutuGovde];
UIView * doktorKutuKic = [[UIView alloc]initWithFrame:CGRectMake(stringSize.width+13, 0, 18, 36)];
[doktorKutuKic setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"doktor_isim_kutu_kic"]]];
[doktorKutu addSubview:doktorKutuKic];
UIImageView *cancelImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"cikar"]];
cancelImage.frame = CGRectMake(-5,9, 18, 18);
[doktorKutuKic addSubview:cancelImage];
UIButton *cancel = [[UIButton alloc]initWithFrame:CGRectMake(-5,0, 20, 36)];
[cancel setUserInteractionEnabled:YES];
[cancel addTarget:self action:#selector(removeNameFromTheList:) forControlEvents:UIControlEventTouchUpInside];
[doktorKutuKic addSubview:cancel];
UITapGestureRecognizer *singlePress =[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSinglePress:)];
[doktorKutuGovde addGestureRecognizer:singlePress];
[doktorKutu bringSubviewToFront:cancel];
[doctorCommented addObject:doktorKutu];
[doktorKutuKic bringSubviewToFront:cancel];
[doktorKutu bringSubviewToFront:cancel];
dictRowKey = [NSString stringWithFormat:#"row%d",counterRow];
NSMutableArray *row = [[NSMutableArray alloc]init];
doktorKutu.frame = CGRectMake(5, yAxis, stringSize.width+30, 36);
if([doctorAddition objectForKey:dictRowKey]!=nil)
{
row = (NSMutableArray*)[doctorAddition objectForKey:dictRowKey];
if(row.count>0)
{
int totalWidth = 5;
for(int i=0;i<row.count;i++)
{
totalWidth = totalWidth + ((UIView*)[row objectAtIndex:i]).frame.size.width+5;
}
if(totalWidth+stringSize.width<520)
{
doktorKutu.frame = CGRectMake(totalWidth, yAxis, stringSize.width+30, 36);
[row addObject:doktorKutu];
[doctorAddition removeObjectForKey:dictRowKey];
[doctorAddition setObject:row forKey:dictRowKey];
}
else
{
doktorKutu.frame = CGRectMake(5, yAxis+40, stringSize.width+30, 36);
yAxis= yAxis + 40.0;
counterRow++;
dictRowKey = [NSString stringWithFormat:#"row%d",counterRow];
row = [[NSMutableArray alloc]init];
[row addObject:doktorKutu];
[doctorAddition setObject:row forKey:dictRowKey];
}
}
}
else
{
[row addObject:doktorKutu];
[doctorAddition setObject:row forKey:dictRowKey];
}
[doctorList setContentSize:CGSizeMake(10, 36+(counterRow*41))];
return doktorKutu;
}
its because when your function " createDoctorBox " call, there may be you have changed a scrollview properties. Please check there.
in your code [doctorList setContentSize:CGSizeMake(10, 36+(counterRow*41))]; its reset the contentOffset of the UIScrollView. if you want to stay on position of delete button, then below that line, set the delete button point as contentOffset of UIScrollView.
Example code:
[doctorList setContentSize:CGSizeMake(10, 36+(counterRow*41))];
doctorList.contentOffset:CGMakePoint(0,deleteBtn.frame.origin.y);
if its help then please vote for me.

Resources