【问题标题】:Need approach to show tables using segmented control?需要使用分段控件显示表格的方法吗?
【发布时间】:2010-01-16 17:03:04
【问题描述】:

您好,我在视图上使用了分段控件。在这个分段控件的帮助下,我想在我的视图上显示到不同的表格,假设我的表格中有两个段,点击段 1 我想显示表 1,点击段 2 我想显示表 2 我的表 1 是普通表,表 2 是分组表,Apple 正在使用方法在应用商店中显示不同类别的不同应用,但我不知道该怎么做。请建议任何方法或任何代码示例也将适用。

谢谢 桑迪

【问题讨论】:

    标签: iphone objective-c uitableview


    【解决方案1】:

    我们通过使用单个 tableview 来做到这一点,然后在每个 tableview 回调方法中执行 if/case 语句,以根据在分段控件中选择的值返回正确的数据。

    首先,将segmentedControl添加到titleView中,并设置改变时的回调函数:

    - (void) addSegmentedControl {
        NSArray * segmentItems = [NSArray arrayWithObjects: @"One", @"Two", nil];
        segmentedControl = [[[UISegmentedControl alloc] initWithItems: segmentItems] retain];
        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        segmentedControl.selectedSegmentIndex = 0;
        [segmentedControl addTarget: self action: @selector(onSegmentedControlChanged:) forControlEvents: UIControlEventValueChanged];
        self.navigationItem.titleView = segmentedControl;
    }
    

    接下来,当分段控件发生变化时,您需要为新分段加载数据,并重置表格视图以显示此数据:

    - (void) onSegmentedControlChanged:(UISegmentedControl *) sender {
        // lazy load data for a segment choice (write this based on your data)
        [self loadSegmentData:segmentedControl.selectedSegmentIndex];
    
        // reload data based on the new index
        [self.tableView reloadData];
    
        // reset the scrolling to the top of the table view
        if ([self tableView:self.tableView numberOfRowsInSection:0] > 0) {
            NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
            [self.tableView scrollToRowAtIndexPath:topIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
        }
    }
    

    然后在您的 tableView 回调中,您需要为每个段值设置逻辑以返回正确的内容。我将向您展示一个回调作为示例,但像这样实现其余的:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"GenericCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"GenericCell" owner:self options:nil] objectAtIndex: 0];
        }   
    
        if (segmentedControl.selectedSegmentIndex == 0) { 
            cell.textLabel.text = @"One";
        } else if (segmentedControl.selectedSegmentIndex == 1) {
            cell.textLabel.text = @"Two";
        }
        return cell;
    }
    

    就是这样,希望对你有帮助。

    【讨论】:

      【解决方案2】:

      另一种选择是拥有一个容器视图,您可以将当前的tableView 添加为子视图。您甚至可以通过创建表视图控制器然后将 .view 添加为容器的子视图,将每个表放在不同的视图控制器中以将事物分开,但如果这样做,则必须手动调用 @987654322 @ 和 viewWillDisapear (这并不难,因为您只需在触摸分段控件时换出表格时调用它们)。

      【讨论】:

      • 这是救命稻草。非常感谢你的想法。
      • 另外不要忘记将 viewWillAppear 和 viewWillDisappear 从主视图控制器转发到当前选定的控制器。
      • 好的.. 让你做到了这一点......但你失去了我。如何转发 viewWillAppear 和 viewWillDisappear?当我想倒退时,是否在子视图的 viewWillDisappear 方法中声明 removeFromSuperView ?如果不造成不便,我们将不胜感激。
      • 只需在容器视图控制器中放置一个 viewWillDisappear/Appear 调用,它将调用传递给容器视图中当前视图控制器的任何内容。抱歉,没有我可以发布的示例代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 2021-04-23
      相关资源
      最近更新 更多