Reputation: 2198
I want to add horizontal scroll and vertical scroll in a single UITableview.
I tried my level best but I doesn't get solution. Can anyone please help me out here.
All solutions are appreciated.
Upvotes: 0
Views: 2040
Reputation: 3804
You need to Create a custom scrollView by subclassing it and just put below code in there. In Storyboard or whatever, use this custom scrollView class. Then TableView and Scroll View would scroll simultaneously. You need to do this because ScrollView's gestureRecognizer delegate CANNOT be accessed from your ViewController, it must be obtained from from ScrollView class itself by subclassing it.
- (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Upvotes: 0
Reputation: 358
For better understanding with UITableView scrolling you can follow this two links
i hope it helps you for better understanding and make familiar with UITableViewDelegate.
Upvotes: 0
Reputation: 1055
This will work for me as your requirement.Please try this.
#pragma mark -- Table view delegate and datasource --
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellid = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
self.tblHistory.showsHorizontalScrollIndicator = NO;
self.tblHistory.showsVerticalScrollIndicator = NO;
if (indexPath.row == 0) {
UIScrollView *scrollViewFood = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 44)];
scrollViewFood.pagingEnabled = YES;
UILabel *lblFood;
NSInteger numberOfFoods = arrFoods.count;
CGFloat xOrigin = 0;
for (int i = 0; i < numberOfFoods; i++) {
NSString *strNames = [arrFoods objectAtIndex:i];
CGSize textSize=[strNames sizeWithAttributes:
@{NSFontAttributeName: [UIFont boldSystemFontOfSize:14.0]}];
xOrigin = CGRectGetMaxX(lblFood.frame);
lblFood = [[UILabel alloc] initWithFrame:CGRectMake(xOrigin, 0, textSize.width + 10, 44)];
lblFood.text = strNames;
lblFood.font = [UIFont boldSystemFontOfSize:14.0];
lblFood.textAlignment = NSTextAlignmentCenter;
lblFood.backgroundColor = [UIColor clearColor];
[scrollViewFood addSubview:lblFood];
}
scrollViewFood.contentSize = CGSizeMake(lblFood.frame.size.width + xOrigin + 10,44);
scrollViewFood.showsHorizontalScrollIndicator = NO;
scrollViewFood.showsVerticalScrollIndicator = NO;
scrollViewFood.delegate=self ;
[cell.contentView addSubview:scrollViewFood];
}else if (indexPath.row == 2){
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 44)];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(cell.frame.size.width , 100);
[cell.contentView addSubview:scrollView];
}
return cell;
}
You change height,width of label as your requirements.
Upvotes: 1
Reputation: 420
Yes you can add scroll at each cell.
You need to create custom cell with UICollectionView inside put delegate, datasource from your controller.
Then implement UICollectionViewDelegate, UICollectionViewDatasource methods in controller.
To recognize which cell currently showing create property in cell class, for example tableViewIndexPath which you can set in tableView willDisplay Cell.
I think its help you.
Upvotes: 1
Reputation: 3677
Yes You can do this..
For that you need to add scrollView
inside each cell and resize their content size so that it can scroll horizontally.
Let me show you briefly how could you do this..
If You are using using AutoLayouts you should insert a UIScrollView in the ContentView of TableView Cell and give them Constraints (.Top .Trailing .Bottom .Leading)
and all of these constraints have constant = 0
.
Then add an other UIView
inside this view and set the same constraints that you have set for scrollView
. This will show you red colour constraints indicating that these constraints are ambiguous but dnt worry just control draw your cursor form this view to the parentView of Scroll View and set equal width and equal height. when you finish this please change the priority of equal width constraint from 1000 to 720
.
Now final add you actual content view in the above UIView
and insert what ever you want to insert in this View it may be extends beyond the boundary of its parent View. When finish adding the object just put the constraints like above mentioned (.Top .Trailing .Bottom .Leading)
with same constants but this time you should also provide the width constraint as well.
This width constraint will actually act as a constraint height. You can change the size later by making this width constraint as IBOutLet and then resize scrollContent width programatically.
For good practice you should create a CustomClass
for your TableViewCell
Upvotes: 1