Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UISegmentedControl with UITableView

I'd like to to make 2 segments, something like this

enter image description here

the deparature segment will display the deparature fly in a tableView and the comeback segment the comeback fly . Can somene please explain me how should I do this? Should I make 2 tableView or just one? thank you

like image 695
user567 Avatar asked May 24 '11 12:05

user567


People also ask

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.

When would you use segmented control?

A segmented control is a linear set of segments, each of which functions as a button that can display a different view. Use a segmented control to offer choices that are closely related but mutually exclusive. A tab bar gives people the ability to switch between different subtasks, views, or modes in an app.

What is UITableView?

UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content. The standard cell configurations display a simple combination of text and images, but you can define custom cells that display any content you want.


1 Answers

You Can use One UITableView for this purpose and reload table data on segmentcontrolindexchange method.Look At code

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



}
like image 181
iProgrammer Avatar answered Nov 16 '22 00:11

iProgrammer