如何在UITableView中使用UISegmentedControl

时间:2011-05-24 12:20:27

标签: iphone objective-c

我想制作两个片段,就像这样

enter image description here

deparature段将在tableView中显示deverature fly,并且将返回的回归段返回。请问我能解释一下我该怎么做?我应该制作2个tableView还是只制作一个?谢谢

5 个答案:

答案 0 :(得分:17)

您可以将One UITableView用于此目的,并在segmentcontrolindexchange方法上重新加载表数据。查看代码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{ if(segment.selectedSegmentIndex==0)
{
    return [List count];
}
    else
        if (segment.selectedSegmentIndex==1) {
            return[List1 count];

        }

    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
           cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];



    lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 100, 20) ];

    // Configure the cell...
lbl =[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 100, 20) ];
    if(segment.selectedSegmentIndex==0)
    {
    cell.textLabel.text=[List objectAtIndex:indexPath.row];

        lbl.text = [List3 objectAtIndex:indexPath.row];
        [cell.contentView addSubview:lbl];

        lbl1.text = [List objectAtIndex:indexPath.row];
        [cell.contentView addSubview:lbl1];
    }
    else if(segment.selectedSegmentIndex==1) {
        cell.textLabel.text=[List1 objectAtIndex:indexPath.row];

        lbl.text = [List objectAtIndex:indexPath.row];
        [cell.contentView addSubview:lbl];
    }


    return cell;
}




-(IBAction) segmentedControlIndexChanged
{
    switch (self.segment.selectedSegmentIndex) {
        case 0:
            i=0;
            [table reloadData];
            break;
        case 1:
            i=1;

            [table reloadData];
        default:
            break;
    }



}

答案 1 :(得分:3)

您可以这样做......一个UITableView需要您更改数据源以及分段控件何时更改。或者首选,您将拥有2个带有自己的控制器的UITableView,并使用分段控件简单地切换它们的可见性。

答案 2 :(得分:0)

遵循Apple设计指南的另一种方法是用工具栏控件替换分段控件,然后可以使用UIToolBarController中的构建来管理UI堆栈并为每个状态提供不同的视图。

答案 3 :(得分:0)

    My case was also same (UISegment Control and UITableView with two prototype cells.  


    class DashBoardViewController: UIViewController,UITableViewDataSource {

        @IBOutlet weak var dashBoardSegment: UISegmentedControl!
        @IBOutlet weak var dashBoardTableView: UITableView!


        //TableView Variables
        var CellIdentifier: String = "dashBoardTableReportsCellID"
        var cell:UITableViewCell?
    var rowCount:Int?


        // MARK: - Table view data source
        func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            switch CellIdentifier
            {
            case "dashBoardTableCellID":
                rowCount = 2

            case "dashBoardTableReportsCellID":
                rowCount = 4
            default:
                break;
            }

            return rowCount!
        }
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            switch CellIdentifier
            {
            case "dashBoardTableCellID":
                cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! 
                print("CELL 1")

            case "dashBoardTableReportsCellID":
                cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! 
                print("CELL 2")
            default:
                break;
            }
            return cell!
        }


// OR 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        if (CellIdentifier == "dashBoardTableCellID")
        {
            let surveyCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! DashBoardTableViewCell
            surveyCell.itemDetailLabel.text = "Survey Cell Title"            

            return surveyCell
        }
        else
        {
            let reportsCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! DashBoardTableViewReportsCell


            return reportsCell
        }
      }


    //Mark:- Segment Control Action
        @IBAction func dashBoardSegmentValueChanged(sender: AnyObject) {
            switch dashBoardSegment.selectedSegmentIndex
            {
            case 0:
                CellIdentifier = "dashBoardTableCellID"
                self.dashBoardTableView.reloadData()

            case 1:
                CellIdentifier = "dashBoardTableReportsCellID"
                self.dashBoardTableView.reloadData()
            default:
                break;
            }

        }
    }

答案 4 :(得分:0)

//UISegmentedControl with TableViewController

//complete working code

@interface TabTwoScheduleViewController () <UITableViewDelegate ,    UITableViewDataSource>
{
    CGRect rect;
    NSArray *list;
    NSArray *list1;
    NSArray *list2;
    NSArray *list3;
    NSArray *list4;
    UITableView *segmentTableView;
}
@end

@implementation TabTwoScheduleViewController

- (void)viewDidLoad {
[super viewDidLoad];

rect = [[UIScreen mainScreen]bounds];

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 10, rect.size.width-20, rect.size.height/10-23)];
scroll.contentSize = CGSizeMake(rect.size.width, rect.size.height * 2);
scroll.showsHorizontalScrollIndicator = YES;
scroll.backgroundColor = [UIColor yellowColor];

NSArray *itemArray = [NSArray arrayWithObjects: @"ONLINE", @"CLASSROOM", @"WEBCASTING", nil];
list = [NSArray arrayWithObjects:@"list.1",@"list.2",@"list.3",@"list.4",@"list.5", nil];
list1 = [NSArray arrayWithObjects:@"list1.1",@"list1.2",@"list1.3",@"list1.4",@"list1.5", nil];
list2 = [NSArray arrayWithObjects:@"list2.1",@"list2.2",@"list2.3",@"list2.4",@"list2.5", nil];
list3 = [NSArray arrayWithObjects:@"list3.1",@"list3.2",@"list3.3",@"list3.4",@"list3.5", nil];
list4 = [NSArray arrayWithObjects:@"list4.1",@"list4.2",@"list4.3",@"list4.4",@"list4.5", nil];

// segmentedControl is declared as property
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
self.segmentedControl.frame = CGRectMake(0, 0, rect.size.width-20, 50);
self.segmentedControl.segmentedControlStyle =UISegmentedControlStylePlain;
[self.segmentedControl addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
self.segmentedControl.selectedSegmentIndex = 0;
[scroll addSubview:self.segmentedControl];
[self.view addSubview:scroll];

//ADDING TABLEVIEW OVER VIEW(I added this view to get leading and trailing space for tableViewCell)

UIView *vw = [[UIView alloc]initWithFrame:CGRectMake(0, 70, rect.size.width, rect.size.height)];
vw.backgroundColor = [UIColor redColor];
[self.view addSubview:vw];

//TABLE VIEW
segmentTableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 0, rect.size.width-20, rect.size.height-230) style:UITableViewStylePlain];
segmentTableView.backgroundColor = [UIColor yellowColor];

segmentTableView.delegate = self;
segmentTableView.dataSource = self;

[vw addSubview:segmentTableView];
}

// number of sections for tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
NSInteger a=0;
switch (self.segmentedControl.selectedSegmentIndex)
{
    case 0:
        a=list.count;
        break;

    case 1:
        a=list1.count;
        break;

case 2:
        a=list1.count;
        break;

    default:
        a=list1.count;
        break;
}
return a;
}
// number of row in the section
- (NSInteger)tableView:(UITableView *)theTableView   numberOfRowsInSection:(NSInteger)section
{
return 1;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ScheduleCustomTableViewCell";

//ScheduleCustomTableViewCell is name of custom TabelViewCell
ScheduleCustomTableViewCell *cell =(ScheduleCustomTableViewCell *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ScheduleCustomTableViewCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

// conditions to get different values on label
if (self.segmentedControl.selectedSegmentIndex==0)
{
    cell.labelAddress1.text = [list objectAtIndex:indexPath.section];
    cell.labelAddress2.text = [list1 objectAtIndex:indexPath.section];
}
else if (self.segmentedControl.selectedSegmentIndex==1)
{
    cell.labelAddress1.text = [list1 objectAtIndex:indexPath.section];
    cell.labelAddress2.text = [list2 objectAtIndex:indexPath.section];
}
else
{
    cell.labelAddress1.text = [list2 objectAtIndex:indexPath.section];
    cell.labelAddress2.text = [list3 objectAtIndex:indexPath.section];
}
cell.backgroundColor = [UIColor yellowColor];

return cell ;

}

-(CGFloat)tableView:(UITableView* )tableView heightForRowAtIndexPath:(NSIndexPath* )indexPath
{
return 130;

}

// Header is given to get spacing between TableViewCells
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor redColor];
return headerView;
}

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

- (void)MySegmentControlAction:(UISegmentedControl *)segment
{
    [segmentTableView reloadData];
}

@end