做蘋果開發有一個多月了,還是不習慣MaC系統,人家都說MAC可以提供碼農的開發效率,還是沒感覺出來。看來大部分AIr筆記本上 裝的是window7 系統。。。。。
我的開發系統:10.7 +xcode 4.3(注:xcode4.2 之前 區別很大),xcode 4.3 的資料也相對較少,對想入門的菜鳥們,頭痛的!同時IOS5 引入了 Storyboard ,給開發多了一個選擇,有時候多一個選擇未必是好事,尤其對全局不是很清楚的條件下。接下來我 一三種方式來實現一個帶有搜索的視圖頁面,如圖1 所示
圖1
方法一 基於strongboard 形式
打開 xcode--->file---->new project ---->application ---->single project, 具體步驟如1-1 1-2 1-3 所示
圖1-1
圖1-2
、
圖1-3
從工具箱中拖出,search bar 和table view 如圖1 佈局,這時候run下,可以看到圖1的界面已有,接下來是實現它的搜索功能:
把Editor 視圖 切換至 show the assistant Editor ,view視圖 換到utilities。選擇剛纔的SearchBar ,右鍵,在reference outlet 拖出一直線到,Bidviewcontroller 建立Searchbar 與視圖控制器關聯(如圖1-4所示);對table View 進行同樣操作。
你會發現,ide 會在我們BidViewControler.h 裏增加了
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet UITableView *table
在BidViewControler.m 增加了
@synthesize searchBar;
@synthesize table;
由於我們要對seachbar 和tableview的操作,所以必須讓BidViewControler 實現
– tableView:cellForRowAtIndexPath: required method
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(allTableData == nil)
return 0;
else {
return [self.allTableData count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
static NSString *sectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sectionsTableIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionsTableIdentifier] ;
}
cell.textLabel.text = [allTableData objectAtIndex:row];
return cell;
}
#pragma mark -Table view Delegate Methods
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[searchBar resignFirstResponder];
return indexPath;
}
#pragma mark- Search Bar Delegate Methods
-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
allTableData = [[NSMutableArray alloc] initWithObjects:
@"Steak",@"Rare",
@"Steak" ,@"Medium",
nil ];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"first" message:@"first by segment" delegate:self cancelButtonTitle:@"are you sure" otherButtonTitles: nil];
[alert show];
}
在次運行代碼,並點search 會發現,會跳出一個alert 對話框,說明我麼已經搞定searchbar了,接下來的任務是實現,對tableview 的操作
我們在-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar方法中 增加[table reloaddata],運行代碼後 發現 talbeview 還會沒數據,爲什麼?? 對頭。。。跟searchbar 一樣,同樣也要設置delegate 和datasource, 把他們連接viewcontrole(如圖1-6所示)r,再次運行,哈哈,可以了。。。
法(二),大家可以參考beginning IOs5 development 8.5 節
法(三)待續