Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

iOS/iPhone Search Bar (UISearchBar) and Table View (UITableView)

iOS/iPhone Search Bar (UISearchBar) and Table View (UITableView) สำหรับ Search Bar เป็น Object ตัวหนึ่งที่เราจะได้พบเจอกันบ่อย ๆ ในการใช้งาน Application ต่าง ๆ เช่น บน Contact ของ iPhone ก็จะมีช่องสำหรับ Search ค้นหาข้อมูลรายชื่อของเพื่อน ๆ หรือใน App ของ Mail ก็จะใช้การ Search รายการอีเมล์ต่าง ๆ บทความนี้เราจะมารู้จักวิธีการเขียนเพื่อสร้าง Search Bar จาก Class ของ UISearchBar โดยทำการ Filter ข้อมูลจาก Array และแสดงผลบน Table View (UITableView)

iOS/iPhone Search Bar (UISearchBar) and Table View

iOS/iPhone Search Bar (UISearchBar) and Table View (UITableView)


บทความนี้จะใช้การทำงานร่วมกับ Table View (UITableView) แต่เราจะสร้าง Object ที่มีชื่อว่า Search Bar (UISearchBar) โดย Search Bar จะใช้ Delegate กับ Class ของ UISearchBarDelegate เพื่อทำการสร้าง method สำหรับการ Filter ข้อมูลที่อยู่ใน Array และ Reload Data ไปยัง Table View

iOS/iPhone Search Bar (UISearchBar) and Table View

เริ่มต้นด้วยการสร้าง Project แบบง่าย ๆ แบบ Single View Application

iOS/iPhone Search Bar (UISearchBar) and Table View

เลือกและไม่เลือกรายการดังรูป

iOS/iPhone Search Bar (UISearchBar) and Table View

ตอนนี้หน้าจอ View ของเรายังว่าง ๆ

iOS/iPhone Search Bar (UISearchBar) and Table View

ทำการลาก Search Bar ไปยังหน้าขอของ View








iOS/iPhone Search Bar (UISearchBar) and Table View

ทำการลาก Table View ไปยังหน้าจอของ View

iOS/iPhone Search Bar (UISearchBar) and Table View

ให้ใส่ Delegate Class ดังรูป ซึ่งจะใช้ <UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

iOS/iPhone Search Bar (UISearchBar) and Table View

ใน Class ของ .h ให้สร้าง IBOutlet ให้กับ Search Bar และ Table View

iOS/iPhone Search Bar (UISearchBar) and Table View

คลิกขวาที่ Table View ให้เชื่อม delegate และ dataSource กับ File's Owner

iOS/iPhone Search Bar (UISearchBar) and Table View

คลิกขวาที่ Search Bar ให้เชื่อม delegate กับ File's Owner เช่นเดียวกัน

iOS/iPhone Search Bar (UISearchBar) and Table View

บทความนี้จะเพิ่มเติใจากบทความ Table View (UITableView) โดยเราจะเพิ่ม Method สำหรับ Filter ข้อมูลดังรูป

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if([searchText length] == 0)
    {
        [displayObject removeAllObjects];
        [displayObject addObjectsFromArray:allObject];
    }
    else
    {
        [displayObject removeAllObjects];
        for(NSString * string in allObject)
        {
            NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if(r.location != NSNotFound)
            {
                [displayObject addObject:string];
            }
        }
    }
    
    [tableViewObj reloadData];
}

สำหรับ Code ไล่ดูแล้วไม่ยาก เพียงแค่สร้าง Loop ค่าใน Array ทั้หมดแล้ว Filter คำที่มีในประโยคที่ค้นหา แล้วสร้างเป็น Array ชุดใหม่ เพื่อแสดงผลบน Table View

Screenshot

iOS/iPhone Search Bar (UISearchBar) and Table View

แสดง Search Bar และข้อมูลบน Table View

iOS/iPhone Search Bar (UISearchBar) and Table View

เมือทำการ ค้นหาข้อมูลก็จะ Filter ข้อมูลตามที่ต้องการ

Code ทั้งหมดของ .h และ .m ในภาษา Objective-C

ViewController.h
//
//  ViewController.h
//  searchBarApp
//
//  Created by Weerachai on 11/18/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
{
    IBOutlet UISearchBar *searchBarObj;
    IBOutlet UITableView *tableViewObj;
}

@end









ViewController.m
//
//  ViewController.m
//  searchBarApp
//
//  Created by Weerachai on 11/18/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    NSMutableArray *allObject;
    NSMutableArray *displayObject;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    allObject = [[NSMutableArray alloc] init];
    [allObject addObject:@"1. One"];
    [allObject addObject:@"2. Two"];
    [allObject addObject:@"3. Three"];
    [allObject addObject:@"4. Four"];
    [allObject addObject:@"5. Five"];
    [allObject addObject:@"6. Six"];
    [allObject addObject:@"7. Seven"];
    [allObject addObject:@"8. Eight"];
    [allObject addObject:@"9. Nine"];
    [allObject addObject:@"10. Ten"];
    
    displayObject =[[NSMutableArray alloc] initWithArray:allObject];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return displayObject.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        // Use the default cell style.
        cell = [[[UITableViewCell alloc] initWithStyle : UITableViewCellStyleSubtitle
                                       reuseIdentifier : CellIdentifier] autorelease];
    }
    
    
    NSString *cellValue = [displayObject objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;
    
    return cell;
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if([searchText length] == 0)
    {
        [displayObject removeAllObjects];
        [displayObject addObjectsFromArray:allObject];
    }
    else
    {
        [displayObject removeAllObjects];
        for(NSString * string in allObject)
        {
            NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if(r.location != NSNotFound)
            {
                [displayObject addObject:string];
            }
        }
    }
    
    [tableViewObj reloadData];
}

-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
	
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];
    tableViewObj.allowsSelection = YES;
    tableViewObj.scrollEnabled = YES;
    
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES animated:YES];
   // tableViewObj.allowsSelection = NO;
    tableViewObj.scrollEnabled = NO;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    searchBar.text=@"";
    
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];
    tableViewObj.allowsSelection = YES;
    tableViewObj.scrollEnabled = YES;
}

- (void)viewDidAppear:(BOOL)animated {
    //[searchBarObj becomeFirstResponder];
    //[super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [searchBarObj release];
    [tableViewObj release];
    [super dealloc];
}
@end


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-11-20 10:03:22 / 2017-03-26 09:47:46
  Download : Download  iOS/iPhone Search Bar (UISearchBar) and Table View (UITableView)
 Sponsored Links / Related

 
iOS/iPhone Search and Bar Display Controller (UISearchDisplayController)
Rating :

 
iOS/iPhone Search Bar and Scope Bar (Objective-C , iPhone)
Rating :

 
iOS/iPhone Search Bar (UISearchBar) Data from Web Server Using JSON
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่