How do I add elements above a UITableView that is animated down - iphone

Basically, I am trying to add a View above a UITableView programatically. The code below, will animate the UITableView down 150 Pixels and I would like to add elements to the space that is created above. But anytime I try to add something, like the Label in the code, it disappears. It's a UITableViewController so I am thinking that might have something to do with it.
Help!
- (IBAction)chooseCity:(id)sender {
[UIView beginAnimations:nil context: NULL];
[UIView setAnimationDuration:0.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
CGRect frame = self.tableView.frame;
if (frame.origin.y > 0) {
frame.origin.y = 0;
} else {
frame.origin.y += 150;
}
[self.tableView setFrame:frame];
[UIView commitAnimations];
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -30, 200, 30)];
testLabel.text = #"Hello";
testLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:testLabel];
}

If you want to add UILabel at top of view, you can add it in tableview's tableHeaderView property,
Allocate UILabel and assign it to tableHeaderView, Even you can put this part in UIView animation, so it will animate according to your requirement.

you can use
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -30, 200, 30)];
testLabel.text = #"Hello";
testLabel.backgroundColor = [UIColor redColor];
tableView.tableHeaderView = testLabel;
[testLabel release];
//to add in footer tableView.tableFooterView = testLabel;
according to your condition you can set that to nil
tableView.tableHeaderView = testLabel;

Would implementing this in
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return someViewIWantInMyHeader;
}
}
and
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return someSize;
}
}
not work for you?

Related

How to swipe tableviewcell for particular row in a section

I have a custom UITableview which is diveded into sections, I have implemented UISwipeGestureRecognizer into it. When I swipe my table view cell, a UIButton appears. The problem I am now facing is when I swipe the first table view cell, another cell in successive sections also recognizes the swipe gesture. I am not able to find how to swipe a cell of particular a section without other cells of other sections getting swiped.
I just want to swipe, I don't want to delete/insert or add check marks.
UPDATE.... This is my customCell.m
Tableview = customTableView
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
self.symptomCellImageView.contentMode=UIViewContentModeScaleToFill;
swipeButton = [[UIButton alloc]init];
swipeButton .frame = CGRectMake(220.0, 8.0, 30.0, 30.0);
swipeButton .hidden = NO;
}
In my MainViewController.m
-(void)viewDidLoad
{
symptomSwipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandleLeft)];
symptomSwipeLeft .numberOfTouchesRequired = 1;
symptomSwipeLeft .direction = UISwipeGestureRecognizerDirectionLeft;
[customTableView addGestureRecognizer:symptomSwipeLeft];
[self addGestureRecognizer:symptomSwipeRight];
symptomSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandleRight)];
symptomSwipeRight.numberOfTouchesRequired = 1;
symptomSwipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[customTableView addGestureRecognizer:symptomSwipeRight];
[self addGestureRecognizer:symptomSwipeRight];
}
- (void)swipeHandleLeft:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer
{
CGPoint location = [aSwipeGestureRecognizer locationInView:customTableView ];
NSIndexPath * indexPath = [customTableView indexPathForRowAtPoint:location];
if(indexPath)
{
UITableViewCell * cell = (UITableViewCell *)[customTableView cellForRowAtIndexPath:indexPath];
[cell.swipeButton addTarget:self action:#selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside];
}
}
- (void)swipeHandleRight:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer
{
CGPoint location = [aSwipeGestureRecognizer locationInView:customTableView ];
NSIndexPath * indexPath = [customTableViewindexPathForRowAtPoint:location];
if(indexPath)
{
UITableViewCell * cell = (UITableViewCell *)[customTableView cellForRowAtIndexPath:indexPath];
[cell.swipeButton addTarget:self action:#selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside];
}
}
Try:
- (void)handleSwipe:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer; {
CGPoint location = [aSwipeGestureRecognizer locationInView:_tableView];
NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
if(indexPath){
UITableViewCell * cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];
[cell whateverMethodYouWant];
}
}
As a side note, the reason you are getting calls to multiple cells is because the row is not unique enough. There is a row = 0 in all of you sections. Therefore, if you want each cell to have a unique number attached to its .tag property, you would need to know the largest number of rows in any section (call it largestNumberOfRows), and then compute:
cell.swipeButton.tag = (indexPath.section * largestNumberOfRows) + indexPath.row;
Hope that helps!
EDIT:
To use the above method, in your viewDidLoad; method, add the following code:
UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)];
[_tableView addGestureRecognizer:recognizer];
EDIT 2
Looking at your code, you have put your gesture recognizers in the wrong file. Here is the setup:
In your MainViewController.m file:
- (void)viewDidLoad; {
[super viewDidLoad];
UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)];
[_tableView addGestureRecognizer:recognizer];
}
- (void)handleSwipe:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer; {
CGPoint location = [aSwipeGestureRecognizer locationInView:_tableView];
NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
if(indexPath){
UITableViewCell * cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];
if(aSwipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight){
[cell symptomCellSwipeRight];
}
else if(aSwipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionLeft){
[cell symptomCellSwipeLeft];
}
}
}
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; {
// Initial your cell. Then add:
[cell.swipeButton addTarget:self action:#selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside];
}
In your PPsymptomTableCell.m file:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; {
if((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])){
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
self.symptomCellImageView.contentMode=UIViewContentModeScaleToFill;
// SHARE ON FACEBOOK BUTTON //
shareFacebookButton = [[UIButton alloc]init];
shareFacebookButton.frame = CGRectMake(220.0, 8.0, 30.0, 30.0);
shareFacebookButton.hidden = NO;
// DISPLAY NOTIFICATION IMAGE //
selectedCellImageDisplay = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"selectedSymptomImage.png"]];
selectedCellImageDisplay.frame = CGRectMake(240.0, 8.0, 30.0, 30.0);
// SYMPTOM NAME //
symptomCellLabel=[[UILabel alloc]initWithFrame:CGRectMake(15.0,0.0 ,280.0,40.0)];
symptomCellLabel.font=[UIFont fontWithName:#"Rockwell" size:17];
symptomCellLabel.textColor=[UIColor blackColor];
symptomCellLabel.backgroundColor=[UIColor clearColor];
[self.contentView addSubview:symptomCellLabel];
[self.contentView addSubview:selectedCellImageDisplay];
[self.contentView addSubview:shareFacebookButton];
}
return self;
}
- (void)symptomCellSwipeLeft; {
[UIView beginAnimations:#"HideView" context:nil];
symptomCellLabel.frame = CGRectMake(-183.0, 0.0, 280.0, 40.0);
selectedCellImageDisplay.frame = CGRectMake(-340.0, 8.0, 30.0, 30.0);
shareFacebookButton.frame = CGRectMake(120.0, 8.0, 80.0, 30.0);
shareFacebookButton.hidden = NO;
shareFacebookButton.backgroundColor = [UIColor redColor];
[UIView setAnimationDuration:0.3f];
[UIView commitAnimations];
}
- (void)symptomCellSwipeRight; {
[UIView beginAnimations:#"HideView" context:nil];
symptomCellLabel.frame = CGRectMake(15.0,0.0 ,280.0,40.0);
selectedCellImageDisplay.frame = CGRectMake(240.0, 8.0, 30.0, 30.0);
shareFacebookButton.frame = CGRectMake(220.0, 8.0, 30.0, 30.0);
shareFacebookButton.hidden = YES;
[UIView setAnimationDuration:0.3f];
[UIView commitAnimations];
}
This should get everything working nicely.
You are implementing your table with sections, then make your tag value of combination of indexPath.sections and indexPath.row.
But make sure you are implementing without overlapping of tagValue.

IPhone: How to put UITextField and UITableView on same View? recomended way

I am new to IOS development. I want to create the view like the shown in Pic.
I also want that when user write in textfield and Keyboard appears TextField scrollup to Keyboard and should be hidden behind Key board.
thanks, Help please
You can not use a UITableViewController but a standard view controller and implement UITableViewDelegate and UITableViewDataSource and implement the necessary delegate methods in your controller, like cellForRowAtIndexPath.
This will allow you to have a table view and control it like you would with a UITableViewController and have any other interface elements you wish.
You would want to add the textfield and button inside of the footer. You can use keyboard notifications to move the view offset so that your textfield will be visible when the user is typing in the keyboard.
Footer:
*I'm assuming the textfield and send button are ivars.
// add footer to table
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 85.0f)];
if(yourTextField ==nil){
yourTextField = [[UITextField alloc] initWithFrame:CGRectMake(5,5,200,35)];
}
if(yourSendButton ==nil){
yourSendButton = [[UIButton alloc] initWithFrame:CGRectMake(225,5,75,35)];
yourSendButton addTarget:self action:#selector(sendBtnClicked) forControlEvents:UIControlEventTouchUpInside];
}
[footerView addSubview:yourTextField];
[footerView addSubview:yourSendButton];
[tableView setTableFooterView:footerView];
Here is some example code for moving the view for the keyboard:
- (void)keyboardWillShow:(NSNotification *)notification
{
NSValue *keyboardFrameValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrame = [[self view] convertRect:[keyboardFrameValue CGRectValue] fromView:nil];
NSNumber *duration = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
[UIView animateWithDuration:[duration floatValue] animations:^{
[scrollView setFrame:CGRectMake(scrollView.frame.origin.x,
scrollView.frame.origin.y,
scrollView.frame.size.width,
keyboardFrame.origin.y - scrollView.frame.origin.y)];
}];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSNumber *duration = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
[UIView animateWithDuration:[duration floatValue] animations:^{
[scrollView setFrame:CGRectMake(scrollView.frame.origin.x,
scrollView.frame.origin.y,
scrollView.frame.size.width,
self.view.frame.size.height - (iPhone ? 44.0f : 0.0f))];
}];
}
You will need to implement the UITableView's delegate methods:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
an example of adding a textfield to the footer would look something like:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
{
UIView* footer = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
footer.backgroundColor = [UIColor whiteColor];
UITextField* field = [[[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
[field setBorderStyle:UITextBorderStyleBezel];
[footer addSubview:field];
return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
return 44;
}
To get the keyboard will respond properly automatically - you just just need to resign the textfield when you are done with it.

Scrolling top to bottom using UIScrollView

I am looking for an example that show just vertical scrolling in iPhone.
The Apple supplied Scrolling example allows swiping of images left to
right.
Example Link
Example Link
I want to have my images scroll top to bottom like Instagram
App.
I research about it and found lots of example, but they all show
horizontal scrolling.
Example
Example
Example
Does anyone know how to do this?
I appreciate if you send me a link to a tutorial.
Hi i solved your problem with apple code,
just change the function with
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(0,curXLoc);
view.frame = frame;
curXLoc += (kScrollObjHeight);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake(([scrollView1 bounds].size.width),kNumImages * kScrollObjHeight)];
}
or if you want sample then i will give you
For your purpose refer your link:
http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/
There is a code like:
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
For horizontal scrolling you need to set the width of contentSize property to a higher value than the scroll view's frame width. Likewise for vertical scrolling you need to set the height of contentSize property to a higher value than the scroll view's frame height.
Reference
In the above code it can be done like:
scroll.contentSize = CGSizeMake(self.view.frame.size.width, numberOfViews * self.view.frame.size.height);
You can use the following code to scroll images from top to bottom and vice versa.!!
- (void)viewDidLoad
{
//....
UISwipeGestureRecognizer *recognizer;
//for scrolling up
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipeUp)]; // calling method SwipeUp
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[[self view] addGestureRecognizer:recognizer];
//for scrolling down
UISwipeGestureRecognizer *recognizer1;
recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipeDown)]; //calling method SwipeDown
[recognizer1 setDirection:(UISwipeGestureRecognizerDirectionDown)];
[[self view] addGestureRecognizer:recognizer1];
}
//Initialise and synthesise IBOutlet UIImageView *bgImage;
-(IBAction)SwipeUp
{
[bgImage setImage:[UIImage imageNamed:#"yourImage1"] ];
bgImage.opaque=YES;
[self.view addSubview:bgImage];
}
-(IBAction)SwipeDown
{
[bgImage setImage:[UIImage imageNamed:#"yourImage2"] ];
bgImage.opaque=YES;
[self.view addSubview:bgImage];
}
I don't know if I understood correctly what you want to accomplish but basically you can set the position of you scrollView using setContentOffset.
CGPoint bottomOffset = CGPointMake(0, scrollView.contentSize.height - self.scrollView.bounds.size.height);
[scrollView setContentOffset:bottomOffset animated:YES];
There are several ways of doing this for example:
Using the UITableView with Custom cell
Adding UIScrollingView & adding the content/images & increasing the
height of scrollview according to number of objects in array.
Here is the code snippet which creates a 2*2 Grid for images :
//Over here adding the image names into the Array
NSMutableArray *imageArray=[[NSMutableArray alloc]init];
[Arr addObject:[UIImage imageNamed:#"image_1.png"]];
[Arr addObject:[UIImage imageNamed:#"image_2.png"]];
[Arr addObject:[UIImage imageNamed:#"image_3.png"]];
[Arr addObject:[UIImage imageNamed:#"image_4.png"]];
[Arr addObject:[UIImage imageNamed:#"image_5.png"]];
// Initlaizing the ScrollView & setting the frame :
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,320, 460)];
[scrollView setBackgroundColor:[UIColor clearColor]];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
int row = 0;
int column = 0;
for(int i = 0; i < imageArray.count; i++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor=[UIColor clearColor];
button.frame = CGRectMake(column*160+0,row*210+10, 160, 190);
[button setImage:[imageArray objectAtIndex:i] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor clearColor]];
[button addTarget:self action:#selector(chooseCustomImageTapped:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[view1 addSubview:button];
if (column == 1) {
column = 0;
row++;
} else {
column++;
}
}
[scrollView setContentSize:CGSizeMake(320, (row+1) * 210)];
[self.view addSubview:view1];
//Here is the method used for Tapping :
- (IBAction)chooseCustomImageTapped:(id)sender
{
}
Now,once the scrollView is ready for Vertical scrolling, you can do your own customization accordingly. I hope this would work for you.

Dynamically Loading Tableview Header with Calculated size

Hi all I am developing an app.I want to show a table view with a header view.When i navigate from Root view to Next view(tableview) which is as shown below.But my question is when i passes some text from root view to Next view(table view) as a header to next view it shows like the following in appropriate format.i need solution for this to show some better format.Following code that i used for this.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
/* NSString *str_header1;
str_header1=[[NSString alloc]initWithString:#"Heading : "];
str_header1=[str_header1 stringByAppendingFormat:str_code1];
str_header1=[str_header1 stringByAppendingFormat:#" - "];
str_header1=[str_header1 stringByAppendingFormat:str_header]; */
// Create label with section title
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 300, 20)];
UILabel *titleLabel = tmpTitleLabel;
titleLabel.font = [UIFont boldSystemFontOfSize:12];
titleLabel.numberOfLines = 0;
titleLabel.textColor = [UIColor colorWithRed:0.30 green:0.34 blue:0.42 alpha:1.0];
titleLabel.shadowColor = [UIColor whiteColor];
titleLabel.shadowOffset = CGSizeMake(0.0, 1.0);
titleLabel.backgroundColor=[UIColor clearColor];
titleLabel.lineBreakMode = UILineBreakModeWordWrap;
titleLabel.text = str_combine;
//Calculate the expected size based on the font and linebreak mode of label
CGSize maximumLabelSize = CGSizeMake(300,9999);
expectedLabelSize= [str_combine sizeWithFont:titleLabel.font constrainedToSize:maximumLabelSize lineBreakMode:titleLabel.lineBreakMode];
//Adjust the label the the new height
CGRect newFrame = titleLabel.frame;
newFrame.size.height = expectedLabelSize.height;
titleLabel.frame = newFrame;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 320, expectedLabelSize.height+30)];
[view addSubview:titleLabel];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection: (NSInteger)section
{
return expectedLabelSize.height+30;
}
Your code assumes that -tableView:viewForHeaderInSection: is always called before -tableView:heightForHeaderInSection: but that is not the case. You could move the size calculation to -tableView:heightForHeaderInSection: to fix that.
Even better, as you seem to use only one table header view: There is a property on UITableView called tableHeaderView which you can use. Rename your method - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section to -(UIView *)headerView, delete the method -tableView:heightForHeaderInSection: and add the following line at the end of the view controller's -viewDidLoad method:
self.tableView.tableHeaderView = [self headerView];

Animating the textview to top with increased height

I have table view with a UITextView added in the footer. I want my table view to animate to top once user tap on the textview. This is done with below piece of code but I have to set the height to negative. Is there any other way of doing this?
- (void)textViewDidBeginEditing:(UITextView *)iTextView {
[UIView animateWithDuration:0.4
delay:0
options:UIViewAnimationTransitionFlipFromLeft
animations:^{
self.myTextView.frame = CGRectMake(8, 4, 200, -160);
self.myTextView.text = #"test";
self.myTextView.font = [UIFont systemFontOfSize:14];
self.myTextView.textColor = [UIColor blackColor];
}
completion:^(BOOL finished){
self.myLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, self.feedbackTextView.frame.size.height + kRunnerTopBottomMargin, kScreenWidth, 20.0f)] autorelease];
self.myLabel.backgroundColor = [UIColor clearColor];
self.myLabel.textColor = [UIColor darkGrayColor];
self.myLabel.text = #"default";
self.myLabel.textAlignment = UITextAlignmentCenter;
self.myLabel.font = [UIFont systemFontOfSize:12];
[self.view addSubview:self.myLabel];
}];
You can call this method:
[YourTableView setContentOffset:CGPointMake(0.0f, 0.0f) animated:YES];
Perhaps this can help you.
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;