iphone開發(一)

      做蘋果開發有一個多月了,還是不習慣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 實現

 <UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>三個協議,看三個協議的文檔,UITableViewDataSource要求必須實現
 – tableView:numberOfRowsInSection:  required method
– tableView:cellForRowAtIndexPath:  required method 
實現這兩個。我們實現了
 #pragma mark - Table view data source
- (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 還是沒反應,爲啥子尼?蘋果爲了解耦 ,大部分都以代理模式實現,點擊dock  中的searchbar  ,切換到connection inspector 你會發現,searchbar  deleagte 處於無連接轉狀態,趕緊拉一條直線到view controler 那裏,如圖1-5所示


在次運行代碼,並點search 會發現,會跳出一個alert 對話框,說明我麼已經搞定searchbar了,接下來的任務是實現,對tableview 的操作

我們在-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar方法中 增加[table reloaddata],運行代碼後 發現 talbeview 還會沒數據,爲什麼?? 對頭。。。跟searchbar 一樣,同樣也要設置delegate 和datasource, 把他們連接viewcontrole(如圖1-6所示)r,再次運行,哈哈,可以了。。。 


法(二),大家可以參考beginning IOs5 development 8.5 節

法(三)待續

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章