hide custom header in tableview in iphone - iphone

I am using custom header in category tableview. So, in accordance to that condition, I have to hide header and it's detail. So, I used following code for displaying header with condition.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *aView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
if(section==0)
{
if(![appDelegate.purchase_requisitions_preference isEqualToString:#"0"] && ![appDelegate.purchase_requisitions_preference isEqualToString:#"null"])
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 320, 30)];
btn.backgroundColor = [UIColor colorWithRed:39.0/255.0 green:131.0/255.0 blue:33.0/255.0 alpha:1.0f];
[btn setTag:section+1];
UILabel *label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(20, 0, 320, 30);
label1.backgroundColor = [UIColor clearColor];
label1.textColor = [UIColor whiteColor];
label1.font = [UIFont boldSystemFontOfSize:16];
label1.text = #"Purchase Order Requisitions";
[label1 setTag:section+1];
[btn addSubview:label1];
[aView addSubview:btn];
[btn addTarget:self action:#selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
}
else
{
return nil;
}
}
else if(section==1)
{
if(![appDelegate.hr_vacancies_preference isEqualToString:#"0"] && ![appDelegate.hr_vacancies_preference isEqualToString:#"null"])
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 320, 30)];
btn.backgroundColor = [UIColor colorWithRed:39.0/255.0 green:131.0/255.0 blue:33.0/255.0 alpha:1.0f];
[btn setTag:section+1];
UILabel *label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(20, 0, 320, 30);
label1.backgroundColor = [UIColor clearColor];
label1.textColor = [UIColor whiteColor];
label1.font = [UIFont boldSystemFontOfSize:16];
label1.text = #"Human Resource Vacancies";
[label1 setTag:section+1];
[btn addSubview:label1];
[aView addSubview:btn];
[btn addTarget:self action:#selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
}
else
{
return nil;
}
}
else if(section==2)
{
if(![appDelegate.hr_offers_preference isEqualToString:#"0"] && ![appDelegate.hr_offers_preference isEqualToString:#"null"])
{
NSLog(#"in offer");
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 320, 30)];
btn.backgroundColor = [UIColor colorWithRed:39.0/255.0 green:131.0/255.0 blue:33.0/255.0 alpha:1.0f];
[btn setTag:section+1];
UILabel *label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(20, 0, 320, 30);
label1.backgroundColor = [UIColor clearColor];
label1.textColor = [UIColor whiteColor];
label1.font = [UIFont boldSystemFontOfSize:16];
label1.text = #"Human Resource Job Offers";
[label1 setTag:section+1];
[btn addSubview:label1];
[aView addSubview:btn];
[btn addTarget:self action:#selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
}
else
{
return nil;
}
}
return aView;
}
If my condition is false, then I want to hide my header as well as it's detail. Its detail is getting hide but header is still displaying there.

return 0 in heightForHeaderInSection: method if your Data is nil otherwise return 30( just example) like bellow..
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if(section==0)
{
if(![appDelegate.purchase_requisitions_preference isEqualToString:#"0"] && ![appDelegate.purchase_requisitions_preference isEqualToString:#"null"])
{
return 30;// any height which you want
}
else{
return 0;
}
}
// and so on
}

you need to set Height of Header Section,
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if(section==0 | section==1 |section==2)
return 30;
else
return 0;
}

Related

Created Table Cell With UIView as subview Not Displaying in iOS7

I have created UIView on top of the UITable Cell but its not displaying in iOS7 which is working fine in iOS6. Kindly provide me the solution.
We are adding the More button # the end of the table.
Added code for the same:
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 40)];
footerView.backgroundColor = [UIColor grayColor];
UIButton *btnLoadMore = [UIButton buttonWithType:UIButtonTypeCustom];
btnLoadMore.frame = CGRectMake(-10, 0, 768, 30);
btnLoadMore.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
[btnLoadMore setTitle:#"Sample" forState:UIControlStateNormal];
[btnLoadMore setBackgroundColor:[UIColor redColor]];
[btnLoadMore setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLoadMore addTarget:self action:#selector(loadMore) forControlEvents:UIControlEventTouchUpInside];
btnLoadMore.userInteractionEnabled=YES;
[btnLoadMore.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:17.0]];
[footerView addSubview:btnLoadMore];
[footerView setHidden:YES];
[footerView setTag:999];
[cell addSubview: footerView];
for (id subView in [cell subviews]) {
[subView setHidden:NO];
}
UIView *lastRow = [cell viewWithTag:999];
[lastRow setHidden:YES];
if(indexPath.section == [arSearch count] && isLoadMore){
for (id subView in [cell subviews]) {
[subView setHidden:YES];
}
cell.backgroundView = nil;
[lastRow setHidden:NO];
Check your code. Why you have written following line :
[footerView setHidden:YES];
I think you are hiding view from cell.
Instead of
[cell addSubview: footerView];
use
[cell.contentView addSubview: footerView];
Also remove
[footerView setHidden:YES];
For loadmore, I always do below.
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
v.backgroundColor = [UIColor clearColor];
int mySiz = 0;
// keep counter how many times load more is pressed.. initial is 0 (this is like index)
mySiz = [startNumberLabel.text intValue]+1;
// i have 15 bcz my index size is 15.
if ([feeds count]>=(15*mySiz)) {
NSLog(#"showing button...");
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(10, 10, 296, 45)];
[button setBackgroundImage:[UIImage imageNamed:localize(#"loadmore")] forState:UIControlStateNormal];
[button addTarget:self action:#selector(loadMoreData:) forControlEvents:UIControlEventTouchUpInside];
[v addSubview:button];
mainTableView.tableFooterView = v;
} else {
mainTableView.tableFooterView = nil;
}
}
[mainTableView reloadData];

create button,text field and insert images programmatically in Xcode

I am facing trouble unexpected issue in iphone app. for whatever reason, I compile this, I get a SIGABRT error. it happens on the:
textFieldRounded=[[UITextField alloc]initWithframe: CGRectMake(20,50,280,31)];
textFieldRounded.borderStyle=UITextBorderStyleRoundedRect;
textFieldRounded.textColor=[UIColor blackColor];
textFieldRounded.font=[UIFont systemFontOfSize:17.0];
textFieldRounded.placeholder=#"enter your name";
textFieldRounded.backgroundColor=[UIColor whiteColor];
textFieldRounded.autocorrectionType=UITextAutocorrectionTypeNo;
textFieldRounded.backgroundColor=[UIColor clearColor];
textFieldRounded.keyboardType=UIKeyboardTypeDefault;
textFieldRounded.returnKeyType=UIReturnKeyDone;
textFieldRounded.clearButtonMode=UITextFieldViewModeWhileEditing;
[self.view addSubView:textFieldRounded];
textFieldRounded.delegate=self;
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Button" forstate:UIControlStateNormal];
button.frame = CGRectMake(20,108,97,37);
[self.view addSubview:button];
label= [[UILabel alloc] initWithFrame:CGRectMake(40,243,240,21)];
label.backgroundColor=[UIColor clearColor];
label.textAlignment,UITextAlignmentCenter;
label.text=#"Label";
[self.view addSubview:label];
CGRect myImageRect = CGRectMake(40.0f,190.0f,240.0f,128.0f);
image= [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"Default#2x.png"]];
image.backgroundColor=[UIColor clearColor];
image.opaque =YES;
image.alpha=0;
[self.view addSubview:image];
[image release];
}
you can Creat UITextField, UIButton or UILabel like this bellow code:-
UITextField
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
[textField release];
UIButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"UIButton" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
[button release];
UILabel
UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 240, 300, 30)];
[self.view addSubview:headingLabel];
// Optional
headingLabel.text = #"WELCOME";
headingLabel.textColor = [UIColor orangeColor];
headingLabel.textAlignment = UITextAlignmentCenter;
headingLabel.tag = 10;
headingLabel.backgroundColor = [UIColor clearColor];
headingLabel.font = [UIFont fontWithName:#"MarkerFelt-Thin" size:14.0];
headingLabel.hidden = NO;
headingLabel.highlighted = YES;
headingLabel.highlightedTextColor = [UIColor blueColor];
headingLabel.lineBreakMode = YES;
headingLabel.numberOfLines = 0;
UIImageView
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
myImage.image = [UIImage imageNamed:#"myImage.png"];
[self.view addSubview:myImage];
[mymyImage release];
Hope its helps you

UITableView tableFooterView not appearing

I have a UITableView to which I add a tableFooterView, for some reason the tableFooterView is not appearing?
How I add my tableFooterView
I add the tableFooterView in a connectionDidFinishLoading method after reloading the tableview data.
So what I do is
[controls reloadData];
UIView *myFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
if(self.item.canRunOnDemand)
{
UIButton *buttonRunWorkflow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonRunWorkflow addTarget:self action:#selector(runWorkflow:) forControlEvents:UIControlEventTouchDown];
[buttonRunWorkflow setTitle:#"Run Now" forState:UIControlStateNormal];
buttonRunWorkflow.frame = CGRectMake(15, 5, 290, 44);
buttonRunWorkflow.backgroundColor = [UIColor clearColor];
[buttonRunWorkflow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myFooterView addSubview:buttonRunWorkflow];
}
if(item.canRunAlways)
{
UILabel *canRunAlwaysLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 46, 100, 44)];
canRunAlwaysLabel.backgroundColor = [UIColor clearColor];
canRunAlwaysLabel.text = #"Run Always:";
UISwitch *canRunAlways = [[UISwitch alloc] initWithFrame:CGRectMake(115, 56, 100, 44)];
[canRunAlways addTarget:self action:#selector(canRunAlwaysChanged:) forControlEvents:UIControlEventValueChanged];
[myFooterView addSubview:canRunAlways];
[myFooterView addSubview:canRunAlwaysLabel];
[canRunAlwaysLabel release];
[canRunAlways release];
}
[myFooterView release];
[controls.tableFooterView addSubview:myFooterView];
Footer view height
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 100;
}
I have also tried this:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if(section == [[fields objectAtIndex:section] count] - 1)
{
return 100;
}
else {
return 0;
}
}
-(UIView*) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if(section == [[fields objectAtIndex:section] count] - 1)
{
UIView *myFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
if(self.item.canRunOnDemand)
{
UIButton *buttonRunWorkflow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonRunWorkflow addTarget:self action:#selector(runWorkflow:) forControlEvents:UIControlEventTouchDown];
[buttonRunWorkflow setTitle:#"Run Now" forState:UIControlStateNormal];
buttonRunWorkflow.frame = CGRectMake(15, 5, 290, 44);
buttonRunWorkflow.backgroundColor = [UIColor clearColor];
[buttonRunWorkflow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myFooterView addSubview:buttonRunWorkflow];
}
if(item.canRunAlways)
{
UILabel *canRunAlwaysLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 46, 100, 44)];
canRunAlwaysLabel.backgroundColor = [UIColor clearColor];
canRunAlwaysLabel.text = #"Run Always:";
UISwitch *canRunAlways = [[UISwitch alloc] initWithFrame:CGRectMake(115, 56, 100, 44)];
[canRunAlways addTarget:self action:#selector(canRunAlwaysChanged:) forControlEvents:UIControlEventValueChanged];
[myFooterView addSubview:canRunAlways];
[myFooterView addSubview:canRunAlwaysLabel];
[canRunAlwaysLabel release];
[canRunAlways release];
}
return myFooterView;
}
else {
return nil;
}
}
By looking at the header file of UITableView.h we see the declaration of property tableFooterView like this:
#property(nonatomic,retain) UIView *tableFooterView; // accessory view below content. default is nil. not to be confused with section footer
so the default property is nil. That's why you can't add another UIView to nil UIView.
You should do something like this:
controls.tableFooterView = myFooterView;
Are you trying to add footer view in table view?
If you are trying for that Implement your footer view in this method
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
This may helps you.Thanks!!

UIToolBar is not shown when adding the same toolbar as inputAccessaryView

I have got a uipickerview which will be populated when pressing a uibutton.
This uipickerview has a uitoolbar control in the top of the uipickerview.
That tool bar has got uibuttons in it.It is working fine until i do the below thing.
I want to have that uittoolbar, on top of the uikeyboard.So I have added , that uitoolbar control as InputAccessoryView.
When i did this, the uitoolbar comes with uikeyboard.that is fine.
but when i tap on the button, which has to show the uipickerview with that toolbar and also needs to resign the toolbar.
So when tap on the button, i have resigned the uikeyboard.
the uipickerview is showing but the toolbar is not visible.
I have been trying to fix this for a long time, but really facing a tough time.
Please let me know.
- (void)viewDidLoad
{
[super viewDidLoad];
// Create an UIButton
self.btnClick = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.btnClick addTarget: self action:#selector(showPicker)
forControlEvents:UIControlEventTouchUpInside];
// Set frame width, height
self.btnClick.frame = CGRectMake(10, 100, 30, 30);
self.btnClick.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:self.btnClick];
self.toolBar = [[[UIToolbar alloc] init]autorelease];
[self.toolBar sizeToFit];
self.toolBar.frame = CGRectMake(0,198, 320, 35);
self.toolBar.barStyle = UIBarStyleBlackTranslucent;
UIImage *image = [UIImage imageNamed:#"orangebuttonsmall.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.bounds = CGRectMake( 0, 0, image.size.width, 25 );
[aButton setImage:image forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(pickerNumPadClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *numButton = [[[UIBarButtonItem alloc] initWithCustomView:aButton]autorelease];
UIImage *aCenterimage = [UIImage imageNamed:#"button_normal.png"];
UIButton *myCenterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[myCenterBtn setTitle:#"Poet Names" forState:UIControlStateNormal];
[myCenterBtn setBackgroundImage:aCenterimage forState:UIControlStateNormal];
myCenterBtn.frame = CGRectMake(100, 0,200,25);
myCenterBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myCenterBtn.titleLabel.font = [UIFont systemFontOfSize:10];
myCenterBtn.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
myCenterBtn.titleLabel.shadowOffset = CGSizeMake(0, -1);
myCenterBtn.bounds = CGRectInset(myCenterBtn.bounds, -3, 2);
myCenterBtn.titleLabel.font = [UIFont boldSystemFontOfSize:13];
[myCenterBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myCenterBtn addTarget:self action:#selector(aCenterBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithCustomView:myCenterBtn]autorelease];
UIImage *sendImage = [UIImage imageNamed:#"orangebuttonsmall.png"];
UIButton *Sendbutton = [UIButton buttonWithType:UIButtonTypeCustom];
Sendbutton.frame = CGRectMake(0, 0,50,25);
[Sendbutton setTitle:#"Send" forState:UIControlStateNormal];
[Sendbutton setBackgroundImage:sendImage forState:UIControlStateNormal];
Sendbutton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Sendbutton.titleLabel.font = [UIFont systemFontOfSize:10];
Sendbutton.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
Sendbutton.titleLabel.shadowOffset = CGSizeMake(0, -1);
Sendbutton.bounds = CGRectMake( 0, 0, 50, 25 );
Sendbutton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
[Sendbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[Sendbutton addTarget:self action:#selector(pickerDoneClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc ] initWithCustomView:Sendbutton]autorelease];
[self.toolBar setItems:[NSArray arrayWithObjects:numButton,flex, doneButton, nil]];
[self.view addSubview:self.toolBar];
self.txtView = [[UITextView alloc] init];
self.txtView.layer.cornerRadius = 15;;
self.txtView.clipsToBounds = YES;
self.txtView.frame = CGRectMake(45, 100, 274, 98);
self.txtView.layer.borderWidth = 1.0f;
self.txtView.delegate = self;
self.txtView.scrollEnabled = YES;
self.txtView.backgroundColor = [UIColor whiteColor];
self.txtView.contentInset = UIEdgeInsetsZero;
self.txtView.returnKeyType = UIReturnKeyDone; // type of the return key
self.txtView.layer.borderColor = [[UIColor blueColor] CGColor];
self.txtView.font = [UIFont fontWithName:#"Helvetica" size:17.0f];
[self.txtView setInputAccessoryView:self.toolBar];
[self.view addSubview:self.txtView];
self.itemsArray = [[[NSArray alloc]initWithObjects:#"William Langland",#"Philip Larkin", #"Dorianne Laux", #"David Lehman", #"Denise Levertov", nil]autorelease];
}
-(void)showPicker
{
[self.txtView resignFirstResponder];
self.pickerView = [[[UIPickerView alloc]init]autorelease];
self.pickerView.showsSelectionIndicator = YES;
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
self.pickerView.tag = 0;
self.pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
[self.pickerView sizeToFit];
self.pickerView.frame = CGRectMake(0, 293 , 320, 15);
self.toolBar.frame = CGRectMake(0,253, 320, 35);
[self.view addSubview:self.toolBar];
[self.view addSubview:self.pickerView];
[self.pickerView selectRow:1 inComponent:0 animated:YES];
}
Try this link
http://blog.swierczynski.net/2010/12/how-to-create-uipickerview-with-toolbar-above-it-in-ios/
It shows how to put a inputAccessoryView on uipicker

Can not set frame size of table view & view controller in iphone using xcode 4

I can not set frame size of view controller & table view. Here is image of my view hierachy
Content view is added as subview in scroll view. My content view contains labels , buttons & text view which are added programatically.
But according to my code listed below , I can not set the table view frame & view controller frame. My table view is frame is not set correctly & thats why I have to scroll up/down to see my table cell contents. So I have 2 scrolls i.e. 1 for scroll view & 1 for table view.
Here is my code
#define TABLE_CELL_HEIGHT 200.0f
#define ADD_VIEW_HEIGHT 30.0f
#define FONT_SIZE 24.0f
#define CELL_CONTENT_WIDTH 720.0f
#define CELL_CONTENT_MARGIN 50.0f
#define SectionHeight 50.0f
#define LEFT_MARGIN 25
#define RIGHT_MARGIN 25
#define TOP_MARGIN 35
#define BOTTOM_MARGIN 50
#define BOTTOM_FOOTER_MARGIN 32
#define DOC_WIDTH 768
#define DOC_HEIGHT 910
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(#"In viewDidLoad of PostDetailsViewController");
self.navigationItem.title = [post Title];
[self createUserInterface];
[self createCommentUserInterface];
self.contentView.backgroundColor = SET_BACKGROUND_IMAGE;
self.view.backgroundColor = SET_BACKGROUND_IMAGE;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self displayCommentsForPhotoID:[post ID]];
[self.commentsTableView reloadData];
CGRect frame = self.commentsTableView.frame;
frame.size.height = TABLE_CELL_HEIGHT*[commentsArray count];
self.commentsTableView.frame = frame;
CGRect viewFrame = self.contentView.frame;
viewFrame.size.height = viewFrame.size.height + (TABLE_CELL_HEIGHT*[commentsArray count]) + ADD_VIEW_HEIGHT;
self.contentView.frame = viewFrame;
[self.scrollView addSubview:self.contentView];
self.scrollView.contentSize = self.contentView.bounds.size;
}
- (void)createUserInterface {
lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 20.0f, 280.0f, 30.f)];
[lblTitle setBackgroundColor:[UIColor clearColor]];
lblTitle.text = #"Is This Fashion Post";
[self.contentView addSubview:lblTitle];
scrollViewForImage = [[UIScrollView alloc]initWithFrame:CGRectMake(20.0f, 60.0f, 280.0f, 200.0f)];
scrollViewForImage.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
scrollViewForImage.maximumZoomScale = 4.0;
scrollViewForImage.minimumZoomScale = 0.75;
scrollViewForImage.clipsToBounds = YES;
scrollViewForImage.delegate = self;
scrollViewForImage.showsHorizontalScrollIndicator = NO;
scrollViewForImage.showsVerticalScrollIndicator = NO;
//scrollViewForImage.contentMode = UIViewContentModeCenter;
scrollViewForImage.bouncesZoom = NO;
scrollViewForImage.bounces = NO;
//scrollViewForImage.autoresizesSubviews = YES;
[self.contentView addSubview:scrollViewForImage];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 280.0f, 200.0f)];
[self.scrollViewForImage addSubview:imageView];
lblBy = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 270.0f, 30.0f, 20.0f)];
[lblBy setBackgroundColor:[UIColor clearColor]];
lblBy.text = #"By:";
[self.contentView addSubview:lblBy];
lblUploaderName = [[UILabel alloc]initWithFrame:CGRectMake(60.0f, 270.0f, 200.0f, 20.0f)];
[lblUploaderName setBackgroundColor:[UIColor clearColor]];
[lblUploaderName setTextColor:[UIColor blueColor]];
[lblUploaderName setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showUserProfile)];
[lblUploaderName addGestureRecognizer:tapRecognizer];
[self.contentView addSubview:lblUploaderName];
lblOn = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 300.0f, 20.0f, 20.0f)];
[lblOn setBackgroundColor:[UIColor clearColor]];
lblOn.text = #"on";
[self.contentView addSubview:lblOn];
[lblOn release];
lblDateTime = [[UILabel alloc]initWithFrame:CGRectMake(45.0f, 300.0f, 255.0f, 20.0f)];
[lblDateTime setBackgroundColor:[UIColor clearColor]];
//lblDateTime.text = #"9/11/11 11:09 AM";
[self.contentView addSubview:lblDateTime];
//txtViewComments = [[UITextView alloc]initWithFrame:CGRectMake(12.0f, 330.0f, 280.0f, 70.0f)];
txtViewComments = [[UITextView alloc]initWithFrame:CGRectMake(12.0f, 325.0f, 280.0f, 70.0f)];
//[txtViewComments setBackgroundColor:[UIColor greenColor]];
txtViewComments.backgroundColor=[UIColor clearColor];
[txtViewComments setEditable:NO];
//txtViewComments.text = #"Toronto Film Festival, image from InStyle.com This is a comment on Is This Fashion";
[txtViewComments setFont:[UIFont fontWithName:#"Helvetica" size:17.0f]];
[self.contentView addSubview:txtViewComments];
[txtViewComments setText:[post Comment]];
CGRect frame = txtViewComments.frame;
frame.size.height = txtViewComments.contentSize.height;
txtViewComments.frame = frame;
//newHeight = frame.size.height + 330.0f + 20.0f;
newHeight = frame.size.height + 330.0f + 5.0f;
NSLog(#"newHeight => %f",newHeight);
//btnYes = [[UIButton alloc]initWithFrame:CGRectMake(20.0f, 410.0f, 60.0f, 50.0f)];
btnYes = [[UIButton alloc]initWithFrame:CGRectMake(20.0f, newHeight, 60.0f, 50.0f)];
[btnYes setBackgroundImage:[UIImage imageNamed:#"yes_icon.png"] forState:UIControlStateNormal];
[btnYes setBackgroundImage:[UIImage imageNamed:#"yes_icon.png"] forState:UIControlEventTouchUpInside];
[btnYes addTarget:self action:#selector(btnYesPressed)forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:btnYes];
//***lblYesPercentage = [[UILabel alloc]initWithFrame:CGRectMake(80.0f, 420.0f, 50.0f, 40.0f)];
//lblYesPercentage = [[UILabel alloc]initWithFrame:CGRectMake(80.0f, 420.0f, 70.0f, 40.0f)];
lblYesPercentage = [[UILabel alloc]initWithFrame:CGRectMake(80.0f, newHeight, 70.0f, 40.0f)];
[lblYesPercentage setBackgroundColor:[UIColor clearColor]];
[lblYesPercentage setFont:[UIFont fontWithName:#"Helvetica" size:24.0f]];
//[lblYesPercentage setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"lblYesPercentage.png"]]];
[self.contentView addSubview:lblYesPercentage];
//**btnNo = [[UIButton alloc]initWithFrame:CGRectMake(150.0f, 410.0f, 50.0f, 50.0f)];
//btnNo = [[UIButton alloc]initWithFrame:CGRectMake(150.0f, 410.0f, 60.0f, 50.0f)];
btnNo = [[UIButton alloc]initWithFrame:CGRectMake(150.0f, newHeight, 60.0f, 50.0f)];
[btnNo setBackgroundImage:[UIImage imageNamed:#"no_icon.png"] forState:UIControlStateNormal];
[btnNo setBackgroundImage:[UIImage imageNamed:#"no_icon.png"] forState:UIControlEventTouchUpInside];
[btnNo addTarget:self action:#selector(btnNoPressed) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:btnNo];
//**lblNoPercentage = [[UILabel alloc]initWithFrame:CGRectMake(210.0f, 420.0f, 50.0f, 40.0f)];
//lblNoPercentage = [[UILabel alloc]initWithFrame:CGRectMake(220.0f, 420.0f, 70.0f, 40.0f)];
lblNoPercentage = [[UILabel alloc]initWithFrame:CGRectMake(220.0f, newHeight, 70.0f, 40.0f)];
[lblNoPercentage setBackgroundColor:[UIColor clearColor]];
[lblNoPercentage setFont:[UIFont fontWithName:#"Helvetica" size:24.0f]];
//[lblNoPercentage setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"lblNoPercentage.png"]]];
[self.contentView addSubview:lblNoPercentage];
//newHeight = newHeight + 70;
newHeight = newHeight + 65;
//lblTags = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 480.0f, 50.0f, 20.0f)];
lblTags = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, newHeight, 50.0f, 20.0f)];
[lblTags setBackgroundColor:[UIColor clearColor]];
lblTags.text = #"Tags:";
[self.contentView addSubview:lblTags];
//newHeight = newHeight - 10;
newHeight = newHeight - 10;
//txtVwTagsValue = [[UITextView alloc]initWithFrame:CGRectMake(65.0f, 470.0f, 220.0f, 90.f)];
txtVwTagsValue = [[UITextView alloc]initWithFrame:CGRectMake(65.0f, newHeight, 220.0f, 90.f)];
//[txtVwTagsValue setBackgroundColor:[UIColor greenColor]];
[txtVwTagsValue setBackgroundColor:[UIColor clearColor]];
[txtVwTagsValue setEditable:NO];
[txtVwTagsValue setFont:[UIFont fontWithName:#"Helvetica" size:17.0f]];
[self.contentView addSubview:txtVwTagsValue];
[txtVwTagsValue setText:[post Tags]];
CGRect frame1 = txtVwTagsValue.frame;
frame1.size.height = txtVwTagsValue.contentSize.height;
txtVwTagsValue.frame = frame1;
[lblTitle setText:[post Title]];
//[btnUploaderName setTitle:[post Username] forState:UIControlStateNormal];
[lblUploaderName setText:[post Username]];
[lblDateTime setText:[post DateAdded]];
//[lblDateTime setText:[self returnDate:[post DateAdded]]];
//[txtViewComments setText:[post Comment]];
[lblYesPercentage setText:[post VoteYes]];
[lblNoPercentage setText:[post VoteNo]];
//[txtVwTagsValue setText:[post Tags]];
// Format the filename
NSString *formattedFilename = [[post Filename] stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
// display image
NSString *url= [NSString stringWithFormat:#"%#%#",PHOTOURL,formattedFilename];
NSLog(#"url %#",url);
if (url !=nil) {
// Here we use the new provided setImageWithURL: method to load the web image
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImageWithURL:[NSURL URLWithString:url]
placeholderImage:[UIImage imageNamed:#"Placeholder.png"]];
}
}
-(void)createCommentUserInterface {
//UILabel *lblComments = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 570.0f, 150.0f, 30.0f)];
//UILabel *lblComments = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, newHeight + 70.0f, 150.0f, 30.0f)];
UILabel *lblComments = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, newHeight + 45.0f, 150.0f, 30.0f)];
[lblComments setBackgroundColor:[UIColor clearColor]];
[lblComments setText:#"Comments"];
[self.contentView addSubview:lblComments];
UIButton *btnAddComment = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnAddComment addTarget:self
action:#selector(btnAddCommentPressed)
forControlEvents:UIControlEventTouchDown];
[btnAddComment setBackgroundImage:[UIImage imageNamed:#"btn_addcomment.png"] forState:UIControlStateNormal];
//btnAddComment.frame = CGRectMake(20.0f, 610.0f, 140.0f, 40.0f);
//btnAddComment.frame = CGRectMake(20.0f, newHeight + 110.0f, 140.0f, 40.0f);
btnAddComment.frame = CGRectMake(20.0f, newHeight + 80.0f, 140.0f, 40.0f);
[self.contentView addSubview:btnAddComment];
//commentsTableView = [[UITableView alloc]initWithFrame:CGRectMake(20.0f, 660.0f, 280.0f, 600.0f) style:UITableViewStylePlain];
//commentsTableView = [[UITableView alloc]initWithFrame:CGRectMake(20.0f, newHeight + 160.0f, 280.0f, 600.0f) style:UITableViewStylePlain];
commentsTableView = [[UITableView alloc]initWithFrame:CGRectMake(20.0f, newHeight + 130.0f, 280.0f, 600.0f) style:UITableViewStylePlain];
commentsTableView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.4];
commentsTableView.delegate = self;
commentsTableView.dataSource = self;
[self.contentView addSubview:commentsTableView];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.commentsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIColor *separatorColor = [UIColor colorWithRed:1.0/255.0 green:146.0/255.0 blue:173.0/255.0 alpha:1.0];
[tableView setSeparatorColor:separatorColor];
//[tableView setSeparatorColor:[UIColor darkGrayColor]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.backgroundColor = [UIColor whiteColor];
userComment = [commentsArray objectAtIndex:[indexPath row]];
UILabel *lblSubmittedBy = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 5.0f, 100.0f, 30.0f)];
[lblSubmittedBy setBackgroundColor:[UIColor clearColor]];
[lblSubmittedBy setText:#"Submitted by"];
[cell addSubview:lblSubmittedBy];
UIButton *btnUserName = [UIButton buttonWithType:UIButtonTypeCustom];
btnUserName.frame = CGRectMake(120.0f, 5.0f, 150.0f, 30.0f);
//[btnUserName setTitle:#"ITF" forState:UIControlStateNormal];
[btnUserName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[btnUserName setBackgroundColor:[UIColor clearColor]];
[btnUserName setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btnUserName addTarget:self action:#selector(showUserProfileForUserComment:) forControlEvents:UIControlEventTouchUpInside];
[btnUserName setUserInteractionEnabled:YES];
[cell addSubview:btnUserName];
UILabel *lblOnDate = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 40.0f, 20.0f, 30.0f)];
[lblOnDate setBackgroundColor:[UIColor clearColor]];
[lblOnDate setText:#"on"];
[cell addSubview:lblOnDate];
UILabel *lblDate = [[UILabel alloc]initWithFrame:CGRectMake(35.0f, 40.0f, 200.0f, 30.0f)];
[lblDate setBackgroundColor:[UIColor clearColor]];
[cell addSubview:lblDate];
UITextView *txtVwComments = [[UITextView alloc]initWithFrame:CGRectMake(5.0f, 75.0f, 265.0f, 70.0f)];
//UITextView *txtVwComments = [[UITextView alloc]initWithFrame:CGRectMake(5.0f, 75.0f, 265.0f, 0.0f)];
[txtVwComments setBackgroundColor:[UIColor clearColor]];
//txtVwComments.backgroundColor = cell.backgroundColor;
[txtVwComments setFont:[UIFont fontWithName:#"Helvetica" size:17.0f]];
[txtVwComments setEditable:NO];
[cell addSubview:txtVwComments];
[txtVwComments setText:[userComment Comment]];
CGRect frame = txtVwComments.frame;
frame.size.height = txtVwComments.contentSize.height;
CGFloat height = frame.size.height;
NSLog(#"Value of height => %f",height);
txtVwComments.frame = frame;
CGFloat ht1 = height + 20.0f + 75.0f;
//UIButton *btnUpVote = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, 160.0f, 16.0f, 16.0f)];
UIButton *btnUpVote = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, ht1 + 10.0f, 16.0f, 16.0f)];
[btnUpVote setBackgroundColor:[UIColor clearColor]];
[btnUpVote setBackgroundImage:[UIImage imageNamed:#"thumb_up.png"] forState:UIControlStateNormal];
[btnUpVote addTarget:self
action:#selector(btnUpVotePressed:)
forControlEvents:UIControlEventTouchDown];
[btnUpVote setTag:[userComment ID]];
[cell addSubview:btnUpVote];
NSLog(#"Value of ht1 => %f",ht1);
lblUpVoteCount = [[UILabel alloc]initWithFrame:CGRectMake(36.0f, ht1 , 30.0f, 30.0f)];
[lblUpVoteCount setBackgroundColor:[UIColor clearColor]];
[lblUpVoteCount setTag:[userComment ID]];
[cell addSubview:lblUpVoteCount];
//NSLog(#"Value of ht2 => %f",ht2);
//UIButton *btnDownVote = [[UIButton alloc]initWithFrame:CGRectMake(90.0f, 160.0f, 16.0f, 16.0f)];
UIButton *btnDownVote = [[UIButton alloc]initWithFrame:CGRectMake(90.0f, ht1 + 10.0f , 16.0f, 16.0f)];
[btnDownVote setBackgroundColor:[UIColor clearColor]];
[btnDownVote setBackgroundImage:[UIImage imageNamed:#"thumb_down.png"] forState:UIControlStateNormal];
[btnDownVote addTarget:self
action:#selector(btnDownVotePressed:)
forControlEvents:UIControlEventTouchDown];
[btnDownVote setTag:[userComment ID]];
[cell addSubview:btnDownVote];
//lblDownVoteCount = [[UILabel alloc]initWithFrame:CGRectMake(116.0f, 150.0f, 40.0f, 30.0f)];
lblDownVoteCount = [[UILabel alloc]initWithFrame:CGRectMake(116.0f, ht1 , 40.0f, 30.0f)];
[lblDownVoteCount setTag:[userComment ID]];
[lblDownVoteCount setBackgroundColor:[UIColor clearColor]];
[cell addSubview:lblDownVoteCount];
UIButton *btnReply = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//btnReply = [[UIButton alloc]initWithFrame:CGRectMake(180.0f, 150.0f, 60.0f, 40.0f)];
btnReply.frame = CGRectMake(180.0f, ht1, 60.0f, 30.0f);
[btnReply setBackgroundColor:[UIColor clearColor]];
[btnReply setBackgroundImage:[UIImage imageNamed:#"btn_reply.png"] forState:UIControlStateNormal];
[btnReply addTarget:self action:#selector(btnReplyPressed:) forControlEvents:UIControlEventTouchDown];
[btnReply setTag:[userComment ID]];
[cell addSubview:btnReply];
float y = ht1 + 40.0f;
for (int i=0; i < [userComment.replyArray count]; i++) {
if ([userComment.replyArray objectAtIndex:i] != nil) {
UITextView *txtVwReply = [[UITextView alloc]initWithFrame:CGRectMake(5.0f, y , 265.0,0.0f )]; //70.0f)];
//UITextView *txtVwReply = [[UITextView alloc]initWithFrame:CGRectMake(40.0f, y , 220.0,0.0f)];
[txtVwReply setUserInteractionEnabled:NO];
[txtVwReply setBackgroundColor:[UIColor clearColor]];
//[txtVwReply setFont:[UIFont fontWithName:#"Helvetica" size:14.0f]];
[txtVwReply setFont:[UIFont fontWithName:#"Helvetica" size:17.0f]];
[cell addSubview:txtVwReply];
commentReply = [userComment.replyArray objectAtIndex:i];
strReply = [NSString stringWithFormat:#"Submitted by %# \non %#\n%#",[commentReply ReplyUsername],[commentReply ReplyDateAdded],[commentReply ReplyComment]];
//NSLog(#"strReply => %#",strReply);
[txtVwReply setText:strReply];
CGRect frame = txtVwReply.frame;
frame.size.height = txtVwReply.contentSize.height;
CGFloat height = frame.size.height;
NSLog(#"Value of txtVwReply height => %f",height);
txtVwReply.frame = frame;
y = y + height;
//y = y + 80;
[txtVwReply release];
}
}
[btnUserName setTitle:[userComment Username] forState:UIControlStateNormal];
[lblDate setText:[userComment DateAdded]];
[lblUpVoteCount setText:[NSString stringWithFormat:#"%i",[userComment UpVotes]]];
[lblDownVoteCount setText:[NSString stringWithFormat:#"%i",[userComment DownVotes]]];
[lblSubmittedBy release];
//[btnUserName release];
[lblOnDate release];
[lblDate release];
[txtVwComments release];
[btnUpVote release];
[lblUpVoteCount release];
[btnDownVote release];
[lblDownVoteCount release];
return cell;
}
#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([commentsArray count]>0) {
userComment = [commentsArray objectAtIndex:indexPath.row];
NSString *strComment =userComment.Comment;
//NSLog(#"strComment : - %#",strComment);
NSString *strCommentReply;
CGFloat replyHeight = 135.0f;
if([userComment.replyArray count]>0) {
for (int i=0; i < [userComment.replyArray count]; i++) {
commentReply = [userComment.replyArray objectAtIndex:i];
//NSString *
strCommentReply = commentReply.ReplyComment;
//NSLog(#"strCommentReply => %#",strCommentReply);
// Append all the reply & then use to determine hight
[strCommentReply stringByAppendingFormat:#"\n %#",strCommentReply];
replyHeight = replyHeight + 120.0f;
}
}
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize sizeComment = [strComment sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(sizeComment.height + replyHeight + 40.0f, 44.0f);
NSLog(#"heightForRowAtIndexPath >> height %f",height);
return height;
}
NSLog(#"heightForRowAtIndexPath >> TABLE_CELL_HEIGHT - %f",TABLE_CELL_HEIGHT);
return TABLE_CELL_HEIGHT;
}
So I need to calculate the frame of the table view & accordingly set the frame of table view & content view. How can I do this?
This is how application look like now
Note that your content view is not a subview of your scroll view. If you look at the clip of the interface builder hierarchy, you can see it's on the same level as the other view. I don't know if this is causing your problem, though.