dev.nikolaz
dev.nikolaz

Reputation: 3184

Duplicate of actions UIView with UITableView

I have table in main view. Next I added view to above table view with height 100 points, then I have first row with 100 points height. Next I check scrollView contentOffset to recalculate position of my added view. When I scroll my table view all fine, my view is duplicate position with my first cell in table view.

But I have problem is: When I want to scroll from my view (finger in a view) and scroll down, table view is no interaction with my gestures. Yes, that normal. If I set user interaction to NO, all work fine. But I have some buttons in this view and when I disabled user interaction I can't have actions with my subviews in this view.

How I can scroll and tap simultaneously/together without user interaction or something else method. Thx.

All of code:

#import "ViewController.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
    UIView *someView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];


    someView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    someView.backgroundColor = [UIColor redColor];
    [self.view addSubview:someView];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    if (indexPath.row == 0) return 100;
    else return 44;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        if (indexPath.row == 0) cell.backgroundColor = [UIColor greenColor];
    }

    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    someView.center = CGPointMake(someView.center.x, -scrollView.contentOffset.y+50);
}

Upvotes: 0

Views: 109

Answers (1)

Alexey Kubas
Alexey Kubas

Reputation: 66

Try to add button on main view

- (void)viewDidLoad {
    [super viewDidLoad];

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];


    someView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    someView.backgroundColor = [UIColor redColor];
    [self.view addSubview:someView];

    UIButton *someBotton = [UIButton buttonWithType:UIButtonTypeCustom];
    // button initialisation
    ...
    [self.view addSubview:someBotton];
}

In this case table will not interact only below button

Second way is create subclass for someView and override method pointInside:

- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event{
   for(UIView *subview in self.subviews){       
       if([subview pointInside:point withEvent:event]){
           return YES;
       }
   }
   return NO;
}

Upvotes: 1

Related Questions