UITableView scrolling but not as expected? - ios

I have a tableview and there are loads of element in it, when i scroll down, it does not stay where i left the scroll, it again goes to top.
How to solve it?
tableView = [[UITableView alloc]init];
tableView.frame = CGRectMake(0,66 ,320.0, 768.0);
tableView.delegate = self;
tableView.dataSource = self;
[self addSubview:tableView];
my row height is 110

your table size is more than the visible area for table.
tableView.frame = CGRectMake(0,66 ,320.0, 768.0); // here is the problem
change change height of your table view up to visible area(say if your table is in whole screen for iPhone 5)
tableView.frame = CGRectMake(0,66 ,320.0, 548.0) // set according your need.

Related

how to do horizontal scroll tableview

I need to do a horizontal scroll in my table view and I search for all the Google and I don't find anything, I do that
CGRect tableFrame = CGRectMake(15, heightView-360, widthView+50, heightView-200);
UITableView *tableView = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];
tableView.layer.cornerRadius=7;
tableView.rowHeight = 40;
tableView.sectionFooterHeight = myData.count;
tableView.sectionHeaderHeight = myData.count;
tableView.scrollEnabled = YES;
tableView.showsVerticalScrollIndicator = YES;
tableView.showsHorizontalScrollIndicator=YES;
tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
tableView.userInteractionEnabled = YES;
tableView.bounces = YES;
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
what are wrong? why don't go the horizontal scroll?
What you are looking for is a UICollectionView, not a UITableView. Here you can implement cells and scroll in either direction.
In Interface Builder when you select the CollectionView it has a property called 'Scroll Direction' - change that to 'horizontal'
From the docs
You were able to access properties related to horizontal scrolling because UITableView is a subclass of UIScrollView, but they aren't meant for use in UITableView
Please take a look at this. This should solve your problem.
https://github.com/alekseyn/EasyTableView
I did this when I was Stuck:
CGRect frame = tblView.frame;
tblView.transform = CGAffineTransformRotate(stressTblView.transform, M_PI / 2);
tblView.frame = frame;
Follow these steps to make a scrollable horizontal table view :
Subclass UIView and create a table view with appropriate frames.
Rotate the table view by -90 degrees ( self.tableView.transform = CGAffineTransformMakeRotation(-M_PI_2);).
Set Autoresizing mask (self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin);).
Create a cell View and rotate it by 90 degrees and populate the table view with that cell.
implement - (void)handleTapGestureRecognizer:(UITapGestureRecognizer *)tapGesture; delegate method to get the location of the tapped cell and scroll to it automatically.
Hope that helps!!!!

Table view frame showing up at unintended location

This is what my view controller looks like with these two table views. As you can see, the left looks like the frame is in the intended place and the right does not. I've posted my code and the origin y is in the same place in both. What could be causing this?
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGFloat halfLength = self.view.frame.size.width / 2;
CGRect ingredientsFrame = CGRectMake(0, 0, halfLength - 1, self.view.frame.size.height);
CGRect modsFrame = CGRectMake(halfLength + 1, 0, halfLength - 1, self.view.frame.size.height);
_ingredientsTableView = [[UITableView alloc] initWithFrame:ingredientsFrame style:UITableViewStyleGrouped];
_ingredientsTableView.delegate = self;
_ingredientsTableView.dataSource = self;
_ingredientsTableView.tag = 1;
[self.view addSubview:_ingredientsTableView];
_modsTableView = [[UITableView alloc] initWithFrame:modsFrame style:UITableViewStyleGrouped];
_modsTableView.delegate = self;
_modsTableView.dataSource = self;
_modsTableView.tag = 2;
[self.view addSubview:_modsTableView];
}
You need to set the second table view's contentInset and scrollIndicatorInsets to compensate for the fact that the top of the table view is up underneath the navigation bar.
The reason you don't see the same problem in the first table view is that this done for you automatically for the first scroll view in your interface.

UITableView header doesn't listen to scroll

I have a simple UITable and i want a little image before the table starts, so i use a tableheader so far so good, this works quite nicely
self.table.tableHeaderView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:img]];
only the weird thing is when i try to scroll on a table view row the table scrolls but when i try to scroll on the header nothing happens, its like the scroll listener isnt listening to to the scroll event on the header.
Just to be clear the header does scroll when you scroll the table as one piece ( and that is the desired behaviour )
Im googleling like crazy but kant seem to find the answer, thanks!!
some extra code
- (void)viewDidLoad
{
[super viewDidLoad];
// set the table header
self.table.tableHeaderView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"header"]];
// add empty footer view to hide empty cells
self.table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
// set title
self.navigationItem.title = [self.catData objectForKey:#"titlePage"] ? [self.catData objectForKey:#"titlePage"] : [self.catData objectForKey:#"title"];
}
Use Grouped Tableview. then you can scroll your table view.
Have you tried setting your header view with :
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [[UIImageView alloc] initWithImage:[UIImage imageNamed:img]];
}
You need to implement this below method:--
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect rect = self.tableView.tableHeaderView.frame;
rect.origin.y = MIN(0, self.tableView.contentOffset.y);
self.tableView.tableHeaderView.frame = rect;
}

iOS: Can't scroll between UITableViews in paged UIScrollView

I have a UIScrollView, in which I want to contain a number of UITableViews, each one being a page. I've added them to my scroll view, and verified that the frames of the UITableViews are set correctly. The first UITableView is displayed correctly, but it will not let me scroll horizontally to see the others. Here's the code I'm using to at the table views:
scrollView.delegate=self;
scrollView.scrollEnabled=YES;
tableViews = [[NSMutableArray alloc] initWithCapacity:recipOrgs.count];
for (NSDictionary *orgDict in recipOrgs) {
int index = [recipOrgs indexOfObject:orgDict];
NSLog(#"Creating table view for index: %d",index);
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * index;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
OrganizationTableView *tableView = [[OrganizationTableView alloc] init];
tableView.index=index;
tableView.parentCon=self;
tableView.dataSource=tableView;
tableView.delegate=tableView;
[tableViews addObject:tableView];
[scrollView addSubview:tableView];
[tableView setFrame:frame];
}
Any ideas why this isn't working? As I mentioned, I checked the x values of the UITableView origins, and I get 0, 320, 640, etc. like I would expect.
You are probably forgetting to set the contentSize for your scrollView.
scollView.contentSize = CGSizeMake(recipOrgs * CGRectGetWidth(scrollView.frame), CGRectGetHeight(scrollView.frame));
But you should probably consider using UICollectionView instead of UIScrollView, then you get reuse logic for free and you do not have to worry about the contentSize.

How do I get a UITableView to autoresize?

I have a couple of side-by-side UITableViews in a UIView, and I would like to get the whole thing to autoresize. I have a UIView In my init() method am doing:
// I don't know how big frontBack should be, so I'd like it to autosize
UIView *frontBack = [[UIView alloc] initWithFrame:CGRectZero];
frontBack.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UITableView *table = [[UITableView alloc]
initWithFrame:CGRectMake(0, 0, r.size.width / 2, height) style:UITableViewStyleGrouped];
table.autoresizingMask = UIViewAutoresizingFlexibleHeight;
table.dataSource = model1;
[table reloadData];
[frontBack addSubview:table];
... (add second table similarly, except with model2)
...
controller.view = frontBack;
This does not work. The tables are 'height' pixels tall (which is smaller than what they need; the text is cut off).
I've tried several ways of getting the UITableViews to resize, with no luck
// contentSize is correct, but frame size does not change
[table reloadData];
table.frame.size.height = table.contentSize.height;
// contentSize is correct, but bounds does not change
[table reloadData];
table.bounds.size.height = table.contentSize.height;
// Nothing appears to change
[table setNeedsLayout];
[table layoutIfNeeded];
// Again, no change
[table sizeToFit];
I assume I am missing something basic here, but I'd be grateful if someone could point out what it is.
table.bounds.size.height = table.contentSize.height;
This is a read-only property, you need to set the frame again.
Also, Are you sure your containing UIView isn't cutting off the table content? You should resize it as well.

Resources