I'd like to to make 2 segments, something like this
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
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;
}
}
You can do it either way... one UITableView would require you to change the datasource and when the segmentation control changes. alternatively and preferred you would have 2 UITableView's with their own controllers and simple toggle the visibility of them with the segmentation control..
Another approach which would follow the Apple design guidelines would be to replace the segmentation control with a toolbar control, you could then use the build in UIToolBarController to manage the UI stack and have distinct views for each state.
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;
}
}
}
//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
精彩评论