Ada banyak cara untuk melakukan apa yang Anda minta. Yang termudah tentu saja hanya membuatnya di pembuat antarmuka tetapi saya berasumsi ini bukan yang Anda pikirkan. Saya membuat contoh gambar yang Anda posting di atas. Ini tidak persis sama tetapi Anda dapat bermain-main dengan banyak properti untuk mendapatkan tampilan dan nuansa dari apa yang Anda cari.
Dalam ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
@end
Di ViewController.m
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) UISegmentedControl *mySegmentControl;
@property (strong, nonatomic) UISearchBar *mySearchBar;
@property (strong, nonatomic) UITableView *myTableView;
@property (strong, nonatomic) NSMutableArray *tableDataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// create a custom UIView
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, 320, 84)];
myView.tintColor = [UIColor lightGrayColor]; // change tiny color or delete this line to default
// create a UISegmentControl
self.mySegmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"All", @"Not on this iPhone", nil]];
self.mySegmentControl.selectedSegmentIndex = 0;
[self.mySegmentControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
self.mySegmentControl.frame = CGRectMake(20, 10, 280, 30);
[myView addSubview:self.mySegmentControl]; // add segment control to custom view
// create UISearchBar
self.mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 40, 320, 44)];
[self.mySearchBar setDelegate:self];
self.mySearchBar.searchBarStyle = UISearchBarStyleMinimal;
[myView addSubview:self.mySearchBar]; // add search bar to custom view
[self.view addSubview:myView]; // add custom view to main view
// create table data array
self.tableDataArray = [[NSMutableArray alloc] initWithObjects:
@"Line 1",
@"Line 2",
@"Line 3",
@"Line 4",
@"Line 5",
@"Line 6",
@"Line 7",
@"Line 8",
@"Line 9",
@"Line 10",
@"Line 11",
@"Line 12", nil];
self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 160, 320, 320)];
[self.myTableView setDataSource:self];
[self.myTableView setDelegate:self];
[self.view addSubview:self.myTableView]; // add table to main view
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
NSLog(@"search text = %@",searchBar.text);
// code for searching...
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tableDataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [self.tableDataArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Selected table item: %@",[self.tableDataArray objectAtIndex:indexPath.row]);
// do something once user has selected a table cell...
}
-(void)segmentAction:(id)sender {
NSLog(@"Segment control changed to: %@",[self.mySegmentControl titleForSegmentAtIndex:[self.mySegmentControl selectedSegmentIndex]]);
// do something based on segment control selection...
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end