Re: Scrollable GridView with fixed row and column
Re: Scrollable GridView with fixed row and column
- Subject: Re: Scrollable GridView with fixed row and column
- From: BareFeetWare <email@hidden>
- Date: Wed, 16 Oct 2013 17:19:45 +1100
On 16 Oct 2013, at 5:05 am, Vyacheslav Karamov <email@hidden> wrote:
> I need to implement Grid view for iOS 7 and newer.
> The users should have ability to scroll its contents both horizontally and vertically.
> They also should have ability to mark the very first row and column as fixed i.e. non-scrollable.
A UITableViewController will take care of the vertical scrolling for you. You just need to add horizontal scrolling views to each cell and to the section header.
Here's a fairly easy way that works very well:
1. Create a UITableViewController (your own subclass of it), as normal
2. In tableView:cellForRowAtIndexPath: call createColumnsInView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
BOOL doCreateColumns = [cell.contentView viewWithTag:scrollViewTag] == nil;
if (doCreateColumns)
{
[self createColumnsInView:cell.contentView];
}
// Configure cell here
return cell;
}
3. Implement createColumnsInView: to add a UIScrollView which contains the columns you want (eg multiple UILabels). Set the UIScrollView's contentSize.width to the required width to encompass all of the columns and set the delegate to self (ie the UITableViewController subclass). Set the other properties as shown:
- (void)createColumnsInView:(UIView*)containerView
{
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:containerView.bounds];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
scrollView.scrollsToTop = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.alwaysBounceVertical = NO;
scrollView.alwaysBounceHorizontal = NO;
scrollView.directionalLockEnabled = YES; // otherwise user can start scrolling horizontal then vertical
scrollView.tag = scrollViewTag;
scrollView.delegate = self;
[containerView addSubview:scrollView];
// Probably add your columns (eg series of UILabels) here.
scrollView.contentSize = CGSizeMake(totalWidthRequiredForColumns, scrollView.frame.size.height - 1); // -1 so doesn't scroll vertically
}
4. Implement tableView:viewForHeaderInSection:section, returning a view containing the same sized scrollview that you return in cells. This will be your fixed non vertically scrolling header.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return sectionHeight;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return self.sectionHeader;
}
- (UIView*)sectionHeader
{
if (_sectionHeader == nil)
{
_sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, sectionHeight)];
_sectionHeader.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.8 alpha:0.8];
[self createColumnsInView:_sectionHeader];
}
return _sectionHeader;
}
5. Implement scrollViewDidScroll: to synchronize horizontal scrolling of all the UIScrollViews:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView != self.tableView)
{
NSMutableArray* containers = [NSMutableArray arrayWithObject:self.sectionHeader];
[containers addObjectsFromArray:self.tableView.visibleCells];
for (UIView* container in containers)
{
UIScrollView* containerScrollView = (UIScrollView*)[container viewWithTag:scrollViewTag];
containerScrollView.contentOffset = scrollView.contentOffset;
}
}
}
Hope this helps,
Tom
Tom Brodhurst-Hill
BareFeetWare 👣
--
iPhone/iPad/iPod and Mac software development, specialising in databases
email@hidden
--
Follow us on Twitter: http://twitter.com/barefeetware/
Like us on Facebook: http://www.facebook.com/BareFeetWare
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden