How to scroll only one cell In UICollectionview while swiping the Cell? - iphone

I am displaying the 3 cells at the same time in UICollectionView. How to force the user to horizontally scroll only one item at a time in UICollectionView?

This should be enough:
self.collectionView.pagingEnabled = YES;
Swift version:
self.collectionView.pagingEnabled = true

If you want your cells to be moved only one at a time then you need to set the frame of cell equals to the collection view and then set the collectionView's paging enabled as below,
[collectionView setPagingEnabled:YES];
or
collectionView.pagingEnabled=YES;

Related

UICollectionView with paging some content is off screen Objective-c

I've tried some solutions found on stackoverflow such as
self.collectionView.clipsToBounds = NO;
[self.scrollView setContentOffset:CGPointMake(0, 0)];
And nothing is working.
My collection view has a cell in it and should display one cell at a time. I'm using paging and evertyhing is working fine except that when you go to the second page it cuts a little of the cell off by the screen. And every page you go after that cuts more of the cell of till you get to the last page where half the cell is cut off. I'm doing this on iphone.
What is happening is, your cell is not centered, so as you swipe between them, and the UICollectionView dynamically figures out the spacing of each cell, it gradually becomes more and more off centre, getting the effect you explain.
Ensure that your UICollectionViewCell + UICollectionViewLayout insets equal the width of the contentView of your UICollectionView.
e.g.
On a 320.0f,100.0f UICollectionView. Your cell could be 300.0f,90.0f. And your UICollectionViewLayout (if using a flowLayout), could have the edge insets set to 5.0f,10.0f,5.0f,10.0f.
E.g.
[(UICollectionViewFlowLayout *)collectionView.collectionViewLayout setItemSize:CGSizeMake(320.0f, 100.0f)];
[(UICollectionViewFlowLayout*)collectionView.collectionViewLayout setSectionInset:UIEdgeInsetsMake(5.0f, 10.0f, 5.0f, 10.0f)];
It was min spacing for lines was on 10 so I set it to 0. I think thats a bug in xcode 5 because my collection view is scrolling horizontal not vertical so one would think it would be my min spacing for cells would be wrong
Please make each item in a section, NOT all items in one section.
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return [self.items count];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 1;
}
That's done.

How to lock a UITable from scrolling beyond the items in the table

I have a UITableView that scrolls up too far, to the point where the list of items in the UITableView scroll up and then a large amount of whitespace follows it.
How can I stop UITableView from scrolling that extra space?
Hope I described it well enough.
I think this may work for you. Run this code after calling reloadData on the table :
if (table.contentSize.height < table.frame.size.height)
{
table.scrollEnabled = NO;
}
else
{
table.scrollEnabled = YES;
}
Here ,table.frame.size.height is the actual size of the object (you can see this in Interface Builder) displayed on the screen, whereas table.contentSize.height is the height of the header, the footer, and the height of every cell added together.
I found it here : How to disable scrolling in UITableView table when the content fits on the screen
Hope you can get something from the answers given their also.
This will fix your problem
TableView.bounces=NO;
This will remove empty cells
TableView.separatorStyle = UITableViewCellSeparatorStyleNone;

How to get the visible center of a UITable

I have a standard UITable. When the user scrolls through the rows and then clicks on one of the cells; they will be transfered to another view. There will be some process time from the tap of the cell to the new view. I would like to display a UIActivityIndicator in a view that over lays the center of the UITable. I am not sure though how to obtain the visible center of the UITable.
The code I have:
myView.center = self.tableView.center;
Puts it in the center of the UITable until I start scrolling further and further down. Then the UIActivityIndicator stays up at the top, so it appears UITableView.center does not represent the "Visible Center".
Any tips on how I can achieve this?
Thanks!
Flea
There are two ways to achieve this.
Either you add your activity indicator view to the same view holding the tableView and THEN they can have the same center:
UIView* superview = [tableView superview];
yourActivityIndicator.center = tableView.center;
[superview addSubview:yourActivityIndicator];
or you decide to add your monitor view to the tableView but then the center of this view must be calculated and will depend on the size of indicator view.
CGSize tSize = tableView.frame.size;
CGSize aSize = yourActivityIndicator.frame.size;
yourActivityIndicator.center = CGRectMake(
(tSize.width-aSize.width)*0.5,
(tSize.height-aSize.height)*0.5,
aSize.width,
aSize.height);
[tableView addSubview:yourActivityIndicator];
Put the activity indicator in the tableview's parent instead of in the tableview itself, then it won't scroll with the table view

Change image or style of specific UITableViewCell

Is there any way that I can get a specific cell to change its style or background image while scrolling the table view?
I want to change the image of a cell which is on top of the visible cells. But only its image is going to be changed the others will stay same, until after scrolling the others come to top of the cells which are shown on the screen. Then the top one's image is going to change this time.
You need to implement scrollViewDidScroll: method of UIScrollViewDelegate in your controller then use visibleCells property of UITableView to get the visible cells of the table.
Something like the following code should work
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
UITableView* tableView;
NSArray* visibleCells;
BOOL first;
tableView = (UITableView*)scrollView;
visibleCells = tableView.visibleCells;
first = YES;
for (UITableViewCell* cell in visibleCells) {
if (first) {
//customize the top cell
first = NO;
}else {
//customize the other visible cells
}
}
}

Get a UITableView to scroll up from the bottom / snap to the bottom, not the top

I have a table view which is many cases will only have one or two cells that don't fill the screen. In this case, I would like the cells to sit at the bottom, rather than the top. In other words they should "snap" to the bottom of the tableview.
I can force the table view to scroll them to the bottom like this:
CGPoint bottomOffset = CGPointMake(0, [self.tableView contentSize].height - self.tableView.frame.size.height);
[self.tableView setContentOffset:bottomOffset animated:NO];
However, this is only partially successful. First, it doesn't work if I put it in viewDidLoad or viewWillAppear, only in viewDidAppear, which means that the user sees the tableview with the cells at the top first, then they move to the bottom. Second, if they scroll the table view, when they let go it automatically "snaps" back up to the top.
Does anyone know how to change this behaviour?
One option to consider is to resize the UITableView itself based on how many rows you will be displaying. Presuming that your UITableViewDelegate implements heightForRowAtIndexPath one can then set the height of the UITableView in a viewWillAppear method by multiplying the number of rows by the height of each row.
Something like this:
CGRect frame = [myTableView frame];
frame.size.height = [[myTableView dataSource] tableView: myTableView numberOfRowsInSection: 0] *
[[myTableView delegate] tableView: myTableView heightForRowAtIndexPath: [NSIndexPath indexPathForRow: 0 inSection: 0]];
[myTableView setFrame: frame];
This example assumes your table has one section and each row is the same height. Computing the size would have to get a little more complicated if multiple sections are involved or if different rows might be different heights.
Regardless of how the height is calculated the essence of the idea though is to just make the table itself shorter, no taller than the one or two rows that it is displaying, rather than trying to force it into behaving differently.