UITableView in UIView in UIScrollview : On tap in

2019-08-06 07:10发布

For work purposes I need to create a UIScrollView which embeds a UIView which in his turn embeds an UITableView via the container feature in Xcode.

My UIScrollView is a full page scrollview with Paging enabled. My UIView is filled with a UIImage, some UIButton's and a container linking to a UITableView.

On initial launch, the data is loaded perfectly, meaning the UITableView is filled with the data, the UIImage is filled, and the Buttons are placed correctly.

But for some strange reason the when I try to tap or scroll in the UITableView in the container all the data from my UITableView gets cleared.

I'm posting this question here, as I have not found any other similar issue on StackOverFlow or any other website.

UITableViewCode:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.productTable setBackgroundView:nil];
    self.productTable.backgroundColor = [UIColor clearColor];
    self.productTable.delegate = self;
    self.productTable.dataSource = self;
}

- (void) viewDidAppear:(BOOL)animated {
    /*CGSize tmp = self.productTable.contentSize;
    self.productTable.frame = CGRectMake(0, 0, tmp.width, tmp.height * 3);*/


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    NSLog(@"section count : %i", [self.Products count]);
    return [self.Products count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    xcsSectionInfo *sectionInfo = [self.Products objectAtIndex:section];
    if (sectionInfo.isOpen == NO) {
        return 1;
    } else {
        return 3;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    xcsSectionInfo *sectionInfo = [self.Products objectAtIndex:indexPath.section];

    if (indexPath.row == 0)  {
        static NSString *CellIdentifier = @"Header";
        xcsProductHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.articleNumber.text = sectionInfo.product.articleNumber;
        cell.articleColor.text = sectionInfo.product.articleColor;
        cell.backgroundColor = [UIColor grayColor];
        if (sectionInfo.isOpen == YES && sectionInfo == self.currentSectionInfo) {
            cell.expandImage.image = [UIImage imageNamed:@"arrow_down.png"];
        } else if (sectionInfo.isOpen == NO) {
            cell.expandImage.image = [UIImage imageNamed:@"arrow_up.png"];
        }
        return cell;
    } else if (indexPath.row == 1) {
         static NSString *CellIdentifier = @"ProductHeader";
        xcsProductTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.colorTempHeader.text = @"Color Temperature";
        cell.sourceQualityHeader.text = @"Source Quality";
        cell.sourceTypeHeader.text = @"Source Type";
        cell.luminaireFluxHeader.text = @"Luminaire Flux";
        cell.powerConsumptionHeader.text = @"Power Consumption";
        cell.luminaireEfficacyHeader.text = @"Luminaire Efficacy";
        cell.backgroundColor = [UIColor grayColor];
        return cell;
    } else if (indexPath.row == 2) {

        static NSString *CellIdentifier = @"Product";
        xcsProductCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.colorTemp.text = sectionInfo.product.colorTemperature;
        cell.sourceQuality.text = sectionInfo.product.sourceQuality;
        cell.sourceType.text = sectionInfo.product.sourceType;
        cell.luminaireFlux.text = sectionInfo.product.luminaireFlux;
        cell.powerConsumption.text = sectionInfo.product.powerConsumption;
        cell.luminaireEfficacy.text = sectionInfo.product.luminaireEfficacy;
        cell.backgroundColor = [UIColor grayColor];
        return cell;
    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        xcsSectionInfo *sectionInfo = [self.Products objectAtIndex:indexPath.section];
    NSIndexPath *path0 = [NSIndexPath indexPathForRow:[indexPath row]+1 inSection:[indexPath section]];
    NSIndexPath *path1 = [NSIndexPath indexPathForRow:[indexPath row]+2 inSection:[indexPath section]];
            NSArray *indexPathArray = [NSArray arrayWithObjects: path0, path1, nil];
    if (sectionInfo.isOpen == NO) {
        sectionInfo.isOpen = YES;
        [tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:NO];
    } else {
        sectionInfo.isOpen = NO;
        [tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:NO];
    }
    [self.Products replaceObjectAtIndex:indexPath.section withObject:sectionInfo];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.currentSectionInfo = sectionInfo;
    [tableView reloadData];
}

Btw.: I'm using storyboards

Regards and thanks in advance.

1条回答
你好瞎i
2楼-- · 2019-08-06 08:05

UPDATE 2:

I think a UIPageViewController would be more appropriate (link‌​). It looks like it accomplishes what you are trying to achieve. And probably much more simple than managing scroll views embedded in other scroll views.


UPDATE:

It looks like what you are trying to achieve is made possible in the UIPageViewController (link). If this works, it would be better than trying to manage scroll views embedded in other views.


Embedding a UITableView is specifically NOT recommended by Apple. Conflicts arise when the system is trying to figure out where to send events:

Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled. (source)

But here is the stupid part, when you go to the source link, you will notice that appears in the docs for the UIWebView. Apple forgot to include it in the docs for UITableView.

查看更多
登录 后发表回答