【问题标题】:UITableView in UIView in UIScrollview : On tap in UITableView data gets clearedUIScrollview 中的 UIView 中的 UITableView :点击时 UITableView 数据被清除
【发布时间】:2013-02-19 15:00:54
【问题描述】:

出于工作目的,我需要创建一个嵌入 UIView 的 UIScrollView,而 UIView 又通过 Xcode 中的容器功能嵌入 UITableView。

我的 UIScrollView 是启用了分页的整页滚动视图。 我的 UIView 充满了 UIImage、一些 UIButton 和一个链接到 UITableView 的容器。

在初始启动时,数据已完美加载,这意味着 UITableView 已填充数据,UIImage 已填充,并且 Button 已正确放置。

但是由于某些奇怪的原因,当我尝试在容器中的 UITableView 中点击或滚动时,我的 UITableView 中的所有数据都会被清除。

我在这里发布这个问题,因为我在 StackOverFlow 或任何其他网站上没有发现任何其他类似的问题。

UITableView代码:

- (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];
}

顺便说一句:我正在使用故事板

提前致以问候和感谢。

【问题讨论】:

  • 您能发布您的UITableViewDataSource 和UITableViewDelegate 实现代码吗?

标签: ios cocoa-touch uitableview uiview uiscrollview


【解决方案1】:

更新 2:

我认为UIPageViewController 会更合适(link‌​)。看起来它完成了您想要实现的目标。而且可能比管理嵌入在其他滚动视图中的滚动视图简单得多。


更新:

看起来您正在努力实现的目标在UIPageViewController (link) 中成为可能。如果这可行,它会比尝试管理嵌入在其他视图中的滚动视图更好。


Apple 明确不推荐嵌入UITableView。当系统试图找出将事件发送到哪里时会出现冲突:

重要提示:您不应将 UIWebView 或 UITableView 对象嵌入 UIScrollView 对象。如果这样做,可能会导致意外行为 因为两个对象的触摸事件可能会混淆和错误 处理。 (source)

但这是愚蠢的部分,当您转到源链接时,您会注意到 UIWebView 的文档中出现了该链接。 Apple 忘记将其包含在文档中 UITableView。

【讨论】:

  • 我了解 Apple 不推荐。这意味着它可以做到。
  • @NDakotaBE 可以,但它往往比它的价值更麻烦(如果你愿意,你可以筛选these 帖子。但我们不控制任何这些类的内部情况,所以你发现的任何工作都可能在未来打破。
  • 这是事实。问题是我需要创建一个视图控制器,我可以在其中分页浏览动态生成的视图,这些视图中又包含 TableViewControllers。我认为我不能使用其他任何东西,因为我必须在表格上方放置额外的数据。有什么想法吗?
  • @NDakotaBE 也许使用UIPageViewController 会更合适(link)。我从未使用过它,但看起来它完成了您想要实现的目标。而且可能比管理嵌入在其他滚动视图中的滚动视图要简单得多..
  • 您能否将其添加到您的答案中?到时候我会接受的。我太傻了,我没想到
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2015-01-20
  • 1970-01-01
  • 2017-01-15
  • 2014-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多