需要使用分段控制来显示表格吗? [英] Need approach to show tables using segmented control?

查看:72
本文介绍了需要使用分段控制来显示表格吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi there I using a segmented control on a view. With the help of this segmented control I would like to display to different tables on my view, Suppose I have two segments in my table on tap of segment 1 I would like to display table 1 and on tap of segment 2 I would like to display table 2 my table 1 is a Plain table and table 2 is a grouped table, Apple is using approach to display differnt apps in differnt categories on app store but I am not sure how do I do that. Please suggest any approach or any code sample for the same will also appriciated.

谢谢
Sandy

Thanks Sandy

推荐答案

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

We do this by having a single tableview, and then doing an if/case statement in each tableview callback method to return the right data based on which value is selected in the segmented control.

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

First, add the segmentedControl to the titleView, and set a callback function for when it is changed:

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

接下来,当分段控件被更改时,您需要加载数据新段,并重置表视图以显示此数据:

Next, when the segmented control is changed, you need to load the data for the new segment, and reset the table view to show this data:

- (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回调中,你需要有逻辑每段值返回正确的东西。我将向您展示一个回调作为示例,但实现其余的如下:

Then in your tableView callbacks, you need to have logic per segment value to return the right thing. I'll show you one callback as an example, but implement the rest like this:

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

这就是它,希望它有所帮助。

That's about it, hope it helps.

这篇关于需要使用分段控制来显示表格吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆