Refresh tableView inside UIViewController - ios

Objective-C beginner here.
My app structure is as follows:
TabBarController -> NavigationController -> UIViewController -> Table View.
In the table view you can find post objects which I would like to refresh and load more.
If user scrolls from top to bottom I want to delete all posts and then load 5 newest posts.
If user reaches the end of the list I want to load 5 older post.
I tried some suggestions from here and other sites but nothing seems to work.

Thanks for the comment. That helped and no I did not found that post. What I have right now is:
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
CGPoint offset = aScrollView.contentOffset;
CGRect bounds = aScrollView.bounds;
CGSize size = aScrollView.contentSize;
UIEdgeInsets inset = aScrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
float reload_distance = -100;
if(offset.y (h + reload_distance)) {
NSLog(#"load more rows");
}
}
The code block deletes a if statement before the one that you can see. How is that possible?

Related

Simple way to redraw view programatically when device orientation changes

I'm new to iOS development and for my assignment, I'm tasked changing updating the ViewController programmatically when the device's orientation changes. I've found a snippet of an answer here, but it doesn't get the job done.
I tried adding this to the viewWillLayoutSubviews of my View Controller, but all I get is an unused variable warning.
CGRect rotatedFrame = [self.view convertRect:self.view.frame fromView:self.view.superview];
viewWillLayoutSubviews and rotation
As a "hint", I've been told it's simple to implement in viewWillLayoutSubviews. Going through and changing all the CGRects in my VC doesn't sound like a couple of lines of code. There's got to be a simpler, more efficient way to do this, but I've only found snippets of solutions digging around on this site. Thanks for reading.
The line of code you are using is assigning a CGRect to the rotatedFrame variable, it's not updating anything on your view controller.
There's many ways to approach this but it depends on what is contained in your view and how it's been configured. Things like Auto Layout for example could let you configure almost everything in Interface Builder and let you avoid doing most things in code.
You've been tasked to do this programatically and since we know that viewWillLayoutSubviews is called every time the device is rotated that's a good place to start. Here's a lazy way I've gone about rotating a video to fit a new orientation using a transformation:
//Vertical
CGSize size = self.view.frame.size;
someView.transform = CGAffineTransformMakeRotation((M_PI * (0) / 180.0))
someView.frame = CGRectMake(0, 0, MIN(size.width, size.height), MAX(size.width, size.height));
//Horizontal
CGSize size = [UIScreen mainScreen].bounds.size;
int directionModifier = ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft) ? -1 : 1;
someView.bounds = CGRectMake(0, 0, MAX(size.width, size.height), MIN(size.width, size.height));
someView.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0) *directionModifier);
someView.transform = CGAffineTransformTranslate(someView.transform,0,0);
How many subviews are in your view? Are they grouped? If you're using auto-resizing masks you might get away with only adjusting the frames of one or two views. If your root view has a number of subviews you can loop through views that need similar adjustments to avoid having to write excess code. It really depends on how everything has been set up.
I figured out how to determine the viewWidth and viewHeight and set those as CGFloats. I then added an if-else statement which figures out if the display is in portrait or landscape and sets the problematic calculateButton accordingly.
Apologies for lengthy code, but I've found in searching this site I find "snippets" of answers, but being new to iOS it's difficult to figure out what goes where. Hopefully, this helps someone later. (and hopefully it's correct)
- (void) viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
CGFloat viewWidth = self.view.bounds.size.width;
CGFloat viewHeight = self.view.bounds.size.height;
CGFloat padding = 20;
CGFloat itemWidth = viewWidth - padding - padding;
CGFloat itemHeight = 44;
// Bunch of setup code for layout items
// HOMEWORK: created if-else statement to deal w/ portrait vs. landscape placement of calculateButton.
if (viewWidth > viewHeight) {
// portrait
CGFloat bottomOfLabel = viewHeight;
self.calculateButton.frame = CGRectMake(padding, bottomOfLabel - itemHeight, itemWidth, itemHeight);
} else {
// landscape
CGFloat bottomOfLabel = CGRectGetMaxY(self.resultLabel.frame);
self.calculateButton.frame = CGRectMake(padding, bottomOfLabel + padding, itemWidth, itemHeight);
}
}

Changing the UITableView height based on scroll in iOS

I want to change the height of my UITableView based on the scrolled content. Right now I do it by getting the scrollViewDidScroll event and then getting scrolled value and then change the height. Here is my code (for simplification I omitted the irrelevant code):
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
float currentOffset = scrollView.contentOffset.y
double delta = currentOffset - storedOffset;
if(delta != 0)
{
delta = abs(delta);
CGRect newListFrame = myTableView.frame;
float newListHeight = newListFrame.size.height + delta;
newListFrame.size.height = newListHeight;
myTableView.frame = newListFrame;
}
storedOffset = currentOffset;
}
But this approach is wrong because with this approach my UITableView's content is scrolled only a little bit and that's not what I want. I just want to get the value of that list that would be scrolled without actually scrolling it. Is there any way to do that? I thing I could get raw finger moved event but can I get it on UITableVIew? Can I do something like this using a UITableView method?

UICollectionView animate individual items during transition

Is it possible to animate each item individually in a UICollectionView when I transition to a child View Controller? I have a UICollectionViewController and when an item is selected I want all items surrounding that item to bounce away from the selected item.
What you describe sounds like a layoutTransition in your UICollectionView. My suggestion would be to create a new layout by subclassing UICollectionViewLayout and override
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
There you can modify the frames of your items to create the effect you are looking for. In particular, you could keep track of the indexPath that was selected and remain that item unaffected. For the rest you could do something like this:
CGRect selectedItemFrame = [[super layoutAttributesForItemAtIndexPath:self.selectedIndexPath] frame];
CGFloat xOffset = attributes.frame.origin.x <= selectedItemFrame.origin.x ? -400.0 : 400.0;
CGFloat yOffset = attributes.frame.origin.y <= selectedItemFrame.origin.y ? -600.0 : 600.0;
CGRect newFrame = CGRectOffset(attributes.frame, xOffset, yOffset);
attributes.frame = newFrame;
Then, just trigger the transition from one layout to the other.

UItableViews in a ScrollView or UICollectionView? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am making an iOS app and I need to show some kind of a horizontal scroll view with some table views in it. It must look this way:
https://dl.dropboxusercontent.com/u/53942814/Pic1.png
https://dl.dropboxusercontent.com/u/53942814/Pic2.png
When user swipes left or right the corresponding table view is to be displayed.
I found two ways to do it:
a UIScrollView with UITableViews
a UICollectionView.
Does anyone have in idea what is the best way and how to implement it?
I have implemented exact same in my code which replicates android like control…
Please see some part of my code you will get some ideas
I have done it by adding Views to UIScrollView.
Used 2 scrollviews one for title and one for tables
1. Adding table views to scrollview for table
pageNo = 1;
for (Page *page in pageArray)
{
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
PageContentViewController *contentViewController = [mystoryboard instantiateViewControllerWithIdentifier:#"pageContentViewController"];
contentViewController.detailsDictionary = page.deviceDetailDictionary; //assigning dictionary will use as table data source
/* Content page's x position calculation */
// Formula: xPos = scrollViewWidth * ( pageNo -1 )
// Where,
// pageNo starts with 1
int xPos = self.detailScrollView.frame.size.width * (pageNo -1);
contentViewController.view.frame = CGRectMake(xPos, 0, self.detailScrollView.frame.size.width, contentViewController.view.frame.size.height);
pageNo ++;
[self addChildViewController:contentViewController];
[self.detailScrollView addSubview:contentViewController.view];
[contentViewController didMoveToParentViewController:self];
}
2. Add UILabels to titleScrollView
Just check this formula to add title UILabel views to top scroll view
// Formula: xPos = 75 + 10 * pageNo + (pageNo -1) * 150;
// Where,
// pageNo starts with 1
// 75 = Left margin of first title
// 10 = Space between two labels
// 150 = Width of label
3. Use Delegate method of ScrollView (in which you added table) to handle swipes
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
{
CGFloat pageWidth = self.detailScrollView.frame.size.width;
//1. Calculate page no. if 50 percent or more area of any page visible make it as current page
int pageNo = floor((self.detailScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 2;
//2. Make only current page (calculated in 1.) visible by scrolling to that page in detailScrollView
//Formula : xPos = pageWidth * (pageNo -1)
CGRect frame = CGRectMake( pageWidth * (pageNo -1), 0, pageWidth, 50);
[self.detailScrollView scrollRectToVisible:frame animated:YES];//OR set content offset
//3. Make title visible of current page by scrolling to that title in titleScrollView
//Formula : xPos = (pageWidth / 2) * (pageNo -1)
CGRect titleFrame = CGRectMake( 160 * (pageNo -1), 0, pageWidth, 10);
[self.titleScrollView scrollRectToVisible:titleFrame animated:YES];//OR set content offset
//Fade effect for non current titles
//Set all page title alpha to low value
for (UILabel *title in pageTitleArray)
{
[title setAlpha:0.5];
}
//set current page alpha to 1
UILabel *title = [pageTitleArray objectAtIndex:pageNo -1];
[title setAlpha:1];
}
Code in 3 takes care of moving to next / prev page on swipe with animation
Hope this will be good starting point for you...

Centering UIView

I have a UIViewController subclass that instantiates a UIView subclass (let's call that viewA). Then, viewA will sometimes instantiate another UIView which we'll call viewB.
I want viewB to be centered within the view controller.
My question is, "What is a (correct) way of doing this?"
TIA
There are many correct ways, but maybe the best one is to use the center-property:
[viewB setCenter: viewA.center];
Or maybe you need to use..
[viewB setCenter: viewA.navigationController.center];
You have to be careful to not end up on a fraction of a point. On non-retina you need to be on full points but for retina you can be on 0.5
One way would be to use center and then adjust
viewB.center = viewA.center;
viewB.frame = CGRectIntegral(viewB.frame);
I don't know a shorter way to do it than this one:
CGFloat x = CGRectGetMidX(self.view.bounds) - viewBWidth / 2;
CGFloat y = CGRectGetMidY(self.view.bounds) - viewBHeight / 2;
viewB.frame = CGRectMake(x,y,viewBWidth,viewBHeight);
viewB.frame = [MyClass centeredFrameForSize:desiredSize inRect:viewA.bounds];
+ (CGRect)centeredFrameForSize:(CGSize)size inRect:(CGRect)rect
{
CGRect frame;
frame.origin.x = rintf((rect.size.width - size.width)/2) + rect.origin.x;
frame.origin.y = rintf((rect.size.height - size.height)/2) + rect.origin.y;
frame.size = size;
return frame;
}

Resources