SlideShare ist ein Scribd-Unternehmen logo
1 von 65
iOS Application Development
Working with Table Views and Search
Bar
These are confidential sessions - please refrain from streaming, blogging, or taking pictures
Session 14
Vu Tran Lam
IAD-2013
• Introduction to table views
• Customizng the table view appearance
• Navigating a data hierarchy with table views
• Table view manipulation
• Introduction to search bar
• Customizing search bar appearance
• Working with search bar in table view
Today’s Topics
Table Views
• A table view presents data in a scrollable list of multiple rows that
may be divided into sections.
• It presents data in a single-column list of multiple rows and is a
means for displaying and editing hierarchical lists of information.
• In normal mode, selecting a message allows the user to read it.
• In editing mode, selecting a message allows the user to delete it
from the inbox.
• Table views provide a simple yet versatile interface for managing
and interacting with collections of data.
Introduction to Table Views
Introduction to Table Views
• Navigate through hierarchically structured data
• View an indexed list of items
• See detail information and controls in visually distinct groupings
• Interact with a selectable list of options
Purpose
• Table views are implemented in the UITableView class
• Individual table cell are implemented in the UITableViewCell class
• Table headers and footers are implemented in the
UITableViewHeaderFooterView class
Implementation
• Configure table views in Interface Builder, in the Table View section
of the Attributes Inspector:
Configuration
• Content of table views
• Behavior of table views
• Appearance of table views
• Table view styles
Customizing Table Views
• To display content, a table view must have a data source.
• The data source mediates between the app’s data model and the
table view. A table view’s data source must conform to the
UITableViewDataSource protocol.
• Each individual table cell can display a variety of content.
• Cells that use the default basic style can display an image and text
label, and detail text label.
Content of Table Views
• Table views need a delegate to manages the appearance and
behavior.
• By assigning a view controller as the table view’s delegate and
implementing the UITableViewDelegate methods, you can allow
the delegate to manage selections, configure headers and
footers,delete and reorder cells.
• Table selection style controls the number of cells a user can select at
a given time.
• There are three types of selection available for individual cells in a
table view: single, multiple, or none.
Behavior of Table Views
Appearance of Table Views
• Plain table views
• Grouped table views
• Standard styles for table view cells
Table View Styles
• A table view in the plain (or regular) style displays rows that stretch
across the screen and have a creamy white background.
• A plain table view can have one or more sections, sections can
have one or more rows, and each section can have its own header
or footer title.
Plain View Styles
Plain Table Views
Plain Table Views
Plain Table Views
Plain Table Views
Plain Table Views
Plain Table Views
Plain Table Views
• A grouped table view also displays a list of information, but it
groups related rows in visually distinct sections.
• Each section has rounded corners and by default appears against a
bluish-gray background.
• Each section may have text or an image for its header or footer to
provide some context or summary for the section.
Grouped View Styles
Grouped Table Views
Padding
Header
Table cell
Footer
Padding
Standard Styles for Table View Cells
Navigating a Data Hierarchy with Table Views
• Hierarchical Data Models and Table Views
• View Controllers and Navigation-Based Apps
Hierarchical Data Models and Table Views
• Hierarchical Data Models and Table Views
Hierarchical Data Models and Table Views
• Hierarchical Data Models and Table Views
Hierarchical Data Models and Table Views
• Hierarchical Data Models and Table Views
Hierarchical Data Models and Table Views
• Hierarchical Data Models and Table Views
Hierarchical Data Models and Table Views
• Hierarchical Data Models and Table Views
View Controllers and Navigation-Based Apps
• Navigation Bars
Control to manage data itemNavigation control
View Controllers and Navigation-Based Apps
• Managing Table Views in a Navigation-Based App
• Creating Table View
• Inserting and Deleting Rows and Sections
Table View Manipulation
• Creating table views using storyboard
• Creating table views programmatically
Creating Table View
• Drag a navigation controller from the library.
• Interface Builder creates a navigation controller and a view
controller, and it creates a relationship between them.
• Display it as the first view controller by selecting the option “Is Initial
View Controller” in the Attributes inspector.
Creating a Table Views Using Storyboard
1
2
3
• Create root view controller for the navigation object.
Creating Table Views Programmatically
1
UIViewController *myViewController = [[MyViewController alloc] init];
• Create root view controller for navigation object.
• Initializing initWithRootViewController: method (navigation
controller).
Creating Table Views Programmatically
1
2
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:myViewController];
• Create root view controller for navigation object.
• Initializing initWithRootViewController: method (navigation
controller).
• Set navigation controller as root view controller of your window.
Creating Table Views Programmatically
1
2
3
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = navigationController;
[window makeKeyAndVisible];
Creating Table Views Programmatically
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// Create the root view controller for the navigation object
UIViewController *myViewController = [[MyViewController alloc] init];
// Initializing navigation controller using initWithRootViewController: method
navigationController = [[UINavigationController alloc]
initWithRootViewController:myViewController];
!
// Set navigation controller as root view controller of your window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = navigationController;
[window makeKeyAndVisible];
}
• Change table view to editing mode
• Deleting a table view row
• Adding a table view row
Inserting and Deleting Rows and Sections
• A Table view goes into editing mode when it receives a
setEditing:animated: message.
• Calling sequence for inserting or deleting rows in a table view
Change Table View to Editing Mode
Deleting a Table View Row
• Set the right item of the navigation bar to be standard Edit button
• View controller responding to setEditing:animated:
• Customizing the editing style of rows
• Updating the data-model array and deleting the row
1
2
3
4
Deleting a Table View Row
• Set the right item of the navigation bar to be standard Edit button1
self.navigationItem.rightBarButtonItem = self.editButton;
Deleting a Table View Row
• Set the right item of the navigation bar to be standard Edit button
• View controller responding to setEditing:animated:
1
2
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
if (editing)
{
addButton.enabled = NO;
}
else
{
addButton.enabled = YES;
}
}
Deleting a Table View Row
• Set the right item of the navigation bar to be standard Edit button
• View controller responding to setEditing:animated:
• Customizing the editing style of rows
1
2
3
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
SimpleEditableListViewController *controller =
(SimpleEditableListViewController *)[[UIApplication
sharedApplication] delegate];
if (indexPath.row == [controller countOfList]-1)
{
return UITableViewCellEditingStyleInsert;
}
else
{
return UITableViewCellEditingStyleDelete;
}
}
Deleting a Table View Row
• Set the right item of the navigation bar to be standard Edit button
• View controller responding to setEditing:animated:
• Customizing the editing style of rows
• Updating the data-model array and deleting the row
1
2
3
4
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete)
{
SimpleEditableListViewController *controller =
(SimpleEditableListViewController *)[[UIApplication
sharedApplication] delegate];
[controller removeObjectFromListAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
• Adding an Add button to the navigation bar
• Responding to a tap on the Add button
• Adding the new item to the data-model array
Adding a Table View Row
1
2
3
Adding a Table View Row
• Adding an Add button to the navigation bar1
UIBarButtonItem addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:@selector(addItemAction:)];
self.navigationItem.rightBarButtonItem = addButton;
Adding a Table View Row
• Adding an Add button to the navigation bar
• Responding to a tap on the Add button
1
2
- (void)addItemAction:sender
{
if (itemInputController == nil)
{
itemInputViewController = [[ItemInputViewController alloc] init];
}
UINavigationController *navigationController = [[UINavigationController
alloc] initWithRootViewController:itemInputViewController];
[[self navigationController] presentModalViewController:navigationController
animated:YES];
}
Adding a Table View Row
• Adding an Add button to the navigation bar
• Responding to a tap on the Add button
• Adding the new item to the data-model array
1
2
3
- (void)save:sender
{
UITextField *textField = [(EditableTableViewTextField *)[self.tableView
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0
inSection:0]] textField];
SimpleEditableListViewController *controller =
(SimpleEditableListViewController *)[[UIApplication
sharedApplication] delegate];
NSString *newItem = textField.text;
if (newItem != nil)
{
[controller insertObject:newItem inListAtIndex:[controller countOfList]];
}
[self dismissModalViewControllerAnimated:YES];
}
Demo
Table Manipulation Demo
Search Bar
• A search bar provides an interface for text-based searches with a
text box and buttons such as search and cancel.
• A search bar accepts text from users, which can be used as input
for a search (shown here with placeholder text).
• A scope bar, which is available only in conjunction with a search bar
- allows users to define the scope of a search (shown here below a
search bar).
Introduction to Search Bar
• Quickly find a value in a large collection
• Create a scope filter
Purpose
• Search bars are implemented in the UISearchBar class
• Configure search bars in Interface Builder, in the Search Bar section
of the Attributes Inspector
Implementation and Configuration
• You can customize the appearance of a search bar by setting
the properties depicted below:
Customizing Search Bar Appearance
• UISearchBar = Search bar + Scope
• Creating a search bar in table view
• Creating a search bar scope
• Displaying the result in table view
Working with Search Bar in Table View
• Drag a search bar and search display controller from the library.
• Interface Builder creates a search bar view controller and search
display controllers, and it creates relationships between them.
Creating a Search Bar in Table View
• Creating a search bar scope
Creating a Search Bar in Table View
• Displaying the result in table view
Creating a Search Bar in Table View
#pragma mark - UISearchDisplayController Delegate Methods
!
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchScope:(NSInteger)searchOption
{
NSString *searchString = [self.searchDisplayController.searchBar text];
NSString *scope;
if (searchOption > 0)
{
scope = [[Product deviceTypeNames] objectAtIndex:(searchOption - 1)];
}
[self updateFilteredContentForProductName:searchString type:scope];
// Return YES to cause the search result table view to be reloaded
return YES;
}
Demo
Products Searching
UITableView
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/
UITableView.html
Table View Programming Guide for iOS
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TableView_iPhone/
AboutTableViewsiPhone/AboutTableViewsiPhone.html
UISearchBar
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/
UISearchBar.html
Documentation
many thanks
to
lamvt@fpt.com.vn
please
say
Stanford University
https://developer.apple.com
Developer Center
http://www.stanford.edu/class/cs193p
xin
chào
Next: Working with Image, Scroll, Collection,
Picker, Web View and Page Control

Weitere ähnliche Inhalte

Ähnlich wie Session 14 - Working with table view and search bar

iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)Jonathan Engelsma
 
Implementing views
Implementing views Implementing views
Implementing views sqlschoolgr
 
Collection view layout
Collection view layoutCollection view layout
Collection view layoutCiklum Ukraine
 
Introducing collection views - Mark Pospesel
Introducing collection views - Mark PospeselIntroducing collection views - Mark Pospesel
Introducing collection views - Mark PospeselJigar Maheshwari
 
SFDC UI - Introduction to Visualforce
SFDC UI -  Introduction to VisualforceSFDC UI -  Introduction to Visualforce
SFDC UI - Introduction to VisualforceSujit Kumar
 
Designing and Creating Views, Inline Functions, and Synonyms
 Designing and Creating Views, Inline Functions, and Synonyms Designing and Creating Views, Inline Functions, and Synonyms
Designing and Creating Views, Inline Functions, and SynonymsTayba Farooqui
 
Advanced collection views
Advanced collection viewsAdvanced collection views
Advanced collection viewsMike Bluestein
 
04.Navigation on Windows Phone
04.Navigation on Windows Phone04.Navigation on Windows Phone
04.Navigation on Windows PhoneNguyen Tuan
 
foodmunch 2.pptx hdshid hdbfhdbfhkd vcn vbds
foodmunch 2.pptx hdshid hdbfhdbfhkd vcn  vbdsfoodmunch 2.pptx hdshid hdbfhdbfhkd vcn  vbds
foodmunch 2.pptx hdshid hdbfhdbfhkd vcn vbdsputtipavan23022023
 
Creating Layouts and Landing Pages for Drupal 8 - DrupalCon Dublin
Creating Layouts and Landing Pages for Drupal 8 - DrupalCon DublinCreating Layouts and Landing Pages for Drupal 8 - DrupalCon Dublin
Creating Layouts and Landing Pages for Drupal 8 - DrupalCon DublinSuzanne Dergacheva
 
Introduction to Responsive Web Development
Introduction to Responsive Web DevelopmentIntroduction to Responsive Web Development
Introduction to Responsive Web DevelopmentNikhil Baby
 
Introduction to Listview in Android
Introduction to Listview in AndroidIntroduction to Listview in Android
Introduction to Listview in Androidtechnoguff
 

Ähnlich wie Session 14 - Working with table view and search bar (20)

iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)
 
iOS: Table Views
iOS: Table ViewsiOS: Table Views
iOS: Table Views
 
Table views
Table viewsTable views
Table views
 
Oracle Apps - Forms
Oracle Apps - FormsOracle Apps - Forms
Oracle Apps - Forms
 
Implementing views
Implementing views Implementing views
Implementing views
 
Collection view layout
Collection view layoutCollection view layout
Collection view layout
 
Introducing collection views - Mark Pospesel
Introducing collection views - Mark PospeselIntroducing collection views - Mark Pospesel
Introducing collection views - Mark Pospesel
 
SFDC UI - Introduction to Visualforce
SFDC UI -  Introduction to VisualforceSFDC UI -  Introduction to Visualforce
SFDC UI - Introduction to Visualforce
 
Ui 5
Ui   5Ui   5
Ui 5
 
Odoo views
Odoo viewsOdoo views
Odoo views
 
Designing and Creating Views, Inline Functions, and Synonyms
 Designing and Creating Views, Inline Functions, and Synonyms Designing and Creating Views, Inline Functions, and Synonyms
Designing and Creating Views, Inline Functions, and Synonyms
 
Advanced collection views
Advanced collection viewsAdvanced collection views
Advanced collection views
 
VIEWS.pptx
VIEWS.pptxVIEWS.pptx
VIEWS.pptx
 
04.Navigation on Windows Phone
04.Navigation on Windows Phone04.Navigation on Windows Phone
04.Navigation on Windows Phone
 
IOS APPs Revision
IOS APPs RevisionIOS APPs Revision
IOS APPs Revision
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
foodmunch 2.pptx hdshid hdbfhdbfhkd vcn vbds
foodmunch 2.pptx hdshid hdbfhdbfhkd vcn  vbdsfoodmunch 2.pptx hdshid hdbfhdbfhkd vcn  vbds
foodmunch 2.pptx hdshid hdbfhdbfhkd vcn vbds
 
Creating Layouts and Landing Pages for Drupal 8 - DrupalCon Dublin
Creating Layouts and Landing Pages for Drupal 8 - DrupalCon DublinCreating Layouts and Landing Pages for Drupal 8 - DrupalCon Dublin
Creating Layouts and Landing Pages for Drupal 8 - DrupalCon Dublin
 
Introduction to Responsive Web Development
Introduction to Responsive Web DevelopmentIntroduction to Responsive Web Development
Introduction to Responsive Web Development
 
Introduction to Listview in Android
Introduction to Listview in AndroidIntroduction to Listview in Android
Introduction to Listview in Android
 

Mehr von Vu Tran Lam

Session 12 - Overview of taps, multitouch, and gestures
Session 12 - Overview of taps, multitouch, and gestures Session 12 - Overview of taps, multitouch, and gestures
Session 12 - Overview of taps, multitouch, and gestures Vu Tran Lam
 
Session 9-10 - UI/UX design for iOS 7 application
Session 9-10 - UI/UX design for iOS 7 applicationSession 9-10 - UI/UX design for iOS 7 application
Session 9-10 - UI/UX design for iOS 7 applicationVu Tran Lam
 
Session 8 - Xcode 5 and interface builder for iOS 7 application
Session 8 - Xcode 5 and interface builder for iOS 7 applicationSession 8 - Xcode 5 and interface builder for iOS 7 application
Session 8 - Xcode 5 and interface builder for iOS 7 applicationVu Tran Lam
 
Session 7 - Overview of the iOS7 app development architecture
Session 7 - Overview of the iOS7 app development architectureSession 7 - Overview of the iOS7 app development architecture
Session 7 - Overview of the iOS7 app development architectureVu Tran Lam
 
Session 5 - Foundation framework
Session 5 - Foundation frameworkSession 5 - Foundation framework
Session 5 - Foundation frameworkVu Tran Lam
 
Session 4 - Object oriented programming with Objective-C (part 2)
Session 4  - Object oriented programming with Objective-C (part 2)Session 4  - Object oriented programming with Objective-C (part 2)
Session 4 - Object oriented programming with Objective-C (part 2)Vu Tran Lam
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Vu Tran Lam
 
Session 2 - Objective-C basics
Session 2 - Objective-C basicsSession 2 - Objective-C basics
Session 2 - Objective-C basicsVu Tran Lam
 
Session 16 - Designing universal interface which used for iPad and iPhone
Session 16  -  Designing universal interface which used for iPad and iPhoneSession 16  -  Designing universal interface which used for iPad and iPhone
Session 16 - Designing universal interface which used for iPad and iPhoneVu Tran Lam
 
iOS 7 Application Development Course
iOS 7 Application Development CourseiOS 7 Application Development Course
iOS 7 Application Development CourseVu Tran Lam
 
Session 1 - Introduction to iOS 7 and SDK
Session 1 -  Introduction to iOS 7 and SDKSession 1 -  Introduction to iOS 7 and SDK
Session 1 - Introduction to iOS 7 and SDKVu Tran Lam
 
Succeed in Mobile career
Succeed in Mobile careerSucceed in Mobile career
Succeed in Mobile careerVu Tran Lam
 
Android Application Development Course
Android Application Development Course Android Application Development Course
Android Application Development Course Vu Tran Lam
 
Your Second iPhone App - Code Listings
Your Second iPhone App - Code ListingsYour Second iPhone App - Code Listings
Your Second iPhone App - Code ListingsVu Tran Lam
 
Introduction to MVC in iPhone Development
Introduction to MVC in iPhone DevelopmentIntroduction to MVC in iPhone Development
Introduction to MVC in iPhone DevelopmentVu Tran Lam
 
Building a Completed iPhone App
Building a Completed iPhone AppBuilding a Completed iPhone App
Building a Completed iPhone AppVu Tran Lam
 
Introduction to iPhone Programming
Introduction to iPhone Programming Introduction to iPhone Programming
Introduction to iPhone Programming Vu Tran Lam
 
Responsive Web Design
Responsive Web DesignResponsive Web Design
Responsive Web DesignVu Tran Lam
 
HTML5 Web Standards
HTML5 Web StandardsHTML5 Web Standards
HTML5 Web StandardsVu Tran Lam
 
3D & Animation Effects Using CSS3 & jQuery
3D & Animation Effects Using CSS3 & jQuery3D & Animation Effects Using CSS3 & jQuery
3D & Animation Effects Using CSS3 & jQueryVu Tran Lam
 

Mehr von Vu Tran Lam (20)

Session 12 - Overview of taps, multitouch, and gestures
Session 12 - Overview of taps, multitouch, and gestures Session 12 - Overview of taps, multitouch, and gestures
Session 12 - Overview of taps, multitouch, and gestures
 
Session 9-10 - UI/UX design for iOS 7 application
Session 9-10 - UI/UX design for iOS 7 applicationSession 9-10 - UI/UX design for iOS 7 application
Session 9-10 - UI/UX design for iOS 7 application
 
Session 8 - Xcode 5 and interface builder for iOS 7 application
Session 8 - Xcode 5 and interface builder for iOS 7 applicationSession 8 - Xcode 5 and interface builder for iOS 7 application
Session 8 - Xcode 5 and interface builder for iOS 7 application
 
Session 7 - Overview of the iOS7 app development architecture
Session 7 - Overview of the iOS7 app development architectureSession 7 - Overview of the iOS7 app development architecture
Session 7 - Overview of the iOS7 app development architecture
 
Session 5 - Foundation framework
Session 5 - Foundation frameworkSession 5 - Foundation framework
Session 5 - Foundation framework
 
Session 4 - Object oriented programming with Objective-C (part 2)
Session 4  - Object oriented programming with Objective-C (part 2)Session 4  - Object oriented programming with Objective-C (part 2)
Session 4 - Object oriented programming with Objective-C (part 2)
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
 
Session 2 - Objective-C basics
Session 2 - Objective-C basicsSession 2 - Objective-C basics
Session 2 - Objective-C basics
 
Session 16 - Designing universal interface which used for iPad and iPhone
Session 16  -  Designing universal interface which used for iPad and iPhoneSession 16  -  Designing universal interface which used for iPad and iPhone
Session 16 - Designing universal interface which used for iPad and iPhone
 
iOS 7 Application Development Course
iOS 7 Application Development CourseiOS 7 Application Development Course
iOS 7 Application Development Course
 
Session 1 - Introduction to iOS 7 and SDK
Session 1 -  Introduction to iOS 7 and SDKSession 1 -  Introduction to iOS 7 and SDK
Session 1 - Introduction to iOS 7 and SDK
 
Succeed in Mobile career
Succeed in Mobile careerSucceed in Mobile career
Succeed in Mobile career
 
Android Application Development Course
Android Application Development Course Android Application Development Course
Android Application Development Course
 
Your Second iPhone App - Code Listings
Your Second iPhone App - Code ListingsYour Second iPhone App - Code Listings
Your Second iPhone App - Code Listings
 
Introduction to MVC in iPhone Development
Introduction to MVC in iPhone DevelopmentIntroduction to MVC in iPhone Development
Introduction to MVC in iPhone Development
 
Building a Completed iPhone App
Building a Completed iPhone AppBuilding a Completed iPhone App
Building a Completed iPhone App
 
Introduction to iPhone Programming
Introduction to iPhone Programming Introduction to iPhone Programming
Introduction to iPhone Programming
 
Responsive Web Design
Responsive Web DesignResponsive Web Design
Responsive Web Design
 
HTML5 Web Standards
HTML5 Web StandardsHTML5 Web Standards
HTML5 Web Standards
 
3D & Animation Effects Using CSS3 & jQuery
3D & Animation Effects Using CSS3 & jQuery3D & Animation Effects Using CSS3 & jQuery
3D & Animation Effects Using CSS3 & jQuery
 

Session 14 - Working with table view and search bar

  • 2. Working with Table Views and Search Bar These are confidential sessions - please refrain from streaming, blogging, or taking pictures Session 14 Vu Tran Lam IAD-2013
  • 3. • Introduction to table views • Customizng the table view appearance • Navigating a data hierarchy with table views • Table view manipulation • Introduction to search bar • Customizing search bar appearance • Working with search bar in table view Today’s Topics
  • 5. • A table view presents data in a scrollable list of multiple rows that may be divided into sections. • It presents data in a single-column list of multiple rows and is a means for displaying and editing hierarchical lists of information. • In normal mode, selecting a message allows the user to read it. • In editing mode, selecting a message allows the user to delete it from the inbox. • Table views provide a simple yet versatile interface for managing and interacting with collections of data. Introduction to Table Views
  • 7. • Navigate through hierarchically structured data • View an indexed list of items • See detail information and controls in visually distinct groupings • Interact with a selectable list of options Purpose
  • 8. • Table views are implemented in the UITableView class • Individual table cell are implemented in the UITableViewCell class • Table headers and footers are implemented in the UITableViewHeaderFooterView class Implementation
  • 9. • Configure table views in Interface Builder, in the Table View section of the Attributes Inspector: Configuration
  • 10. • Content of table views • Behavior of table views • Appearance of table views • Table view styles Customizing Table Views
  • 11. • To display content, a table view must have a data source. • The data source mediates between the app’s data model and the table view. A table view’s data source must conform to the UITableViewDataSource protocol. • Each individual table cell can display a variety of content. • Cells that use the default basic style can display an image and text label, and detail text label. Content of Table Views
  • 12. • Table views need a delegate to manages the appearance and behavior. • By assigning a view controller as the table view’s delegate and implementing the UITableViewDelegate methods, you can allow the delegate to manage selections, configure headers and footers,delete and reorder cells. • Table selection style controls the number of cells a user can select at a given time. • There are three types of selection available for individual cells in a table view: single, multiple, or none. Behavior of Table Views
  • 14. • Plain table views • Grouped table views • Standard styles for table view cells Table View Styles
  • 15. • A table view in the plain (or regular) style displays rows that stretch across the screen and have a creamy white background. • A plain table view can have one or more sections, sections can have one or more rows, and each section can have its own header or footer title. Plain View Styles
  • 23. • A grouped table view also displays a list of information, but it groups related rows in visually distinct sections. • Each section has rounded corners and by default appears against a bluish-gray background. • Each section may have text or an image for its header or footer to provide some context or summary for the section. Grouped View Styles
  • 25. Standard Styles for Table View Cells
  • 26. Navigating a Data Hierarchy with Table Views • Hierarchical Data Models and Table Views • View Controllers and Navigation-Based Apps
  • 27. Hierarchical Data Models and Table Views • Hierarchical Data Models and Table Views
  • 28. Hierarchical Data Models and Table Views • Hierarchical Data Models and Table Views
  • 29. Hierarchical Data Models and Table Views • Hierarchical Data Models and Table Views
  • 30. Hierarchical Data Models and Table Views • Hierarchical Data Models and Table Views
  • 31. Hierarchical Data Models and Table Views • Hierarchical Data Models and Table Views
  • 32. View Controllers and Navigation-Based Apps • Navigation Bars Control to manage data itemNavigation control
  • 33. View Controllers and Navigation-Based Apps • Managing Table Views in a Navigation-Based App
  • 34. • Creating Table View • Inserting and Deleting Rows and Sections Table View Manipulation
  • 35. • Creating table views using storyboard • Creating table views programmatically Creating Table View
  • 36. • Drag a navigation controller from the library. • Interface Builder creates a navigation controller and a view controller, and it creates a relationship between them. • Display it as the first view controller by selecting the option “Is Initial View Controller” in the Attributes inspector. Creating a Table Views Using Storyboard 1 2 3
  • 37. • Create root view controller for the navigation object. Creating Table Views Programmatically 1 UIViewController *myViewController = [[MyViewController alloc] init];
  • 38. • Create root view controller for navigation object. • Initializing initWithRootViewController: method (navigation controller). Creating Table Views Programmatically 1 2 UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
  • 39. • Create root view controller for navigation object. • Initializing initWithRootViewController: method (navigation controller). • Set navigation controller as root view controller of your window. Creating Table Views Programmatically 1 2 3 window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; window.rootViewController = navigationController; [window makeKeyAndVisible];
  • 40. Creating Table Views Programmatically - (void)applicationDidFinishLaunching:(UIApplication *)application { // Create the root view controller for the navigation object UIViewController *myViewController = [[MyViewController alloc] init]; // Initializing navigation controller using initWithRootViewController: method navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController]; ! // Set navigation controller as root view controller of your window window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; window.rootViewController = navigationController; [window makeKeyAndVisible]; }
  • 41. • Change table view to editing mode • Deleting a table view row • Adding a table view row Inserting and Deleting Rows and Sections
  • 42. • A Table view goes into editing mode when it receives a setEditing:animated: message. • Calling sequence for inserting or deleting rows in a table view Change Table View to Editing Mode
  • 43. Deleting a Table View Row • Set the right item of the navigation bar to be standard Edit button • View controller responding to setEditing:animated: • Customizing the editing style of rows • Updating the data-model array and deleting the row 1 2 3 4
  • 44. Deleting a Table View Row • Set the right item of the navigation bar to be standard Edit button1 self.navigationItem.rightBarButtonItem = self.editButton;
  • 45. Deleting a Table View Row • Set the right item of the navigation bar to be standard Edit button • View controller responding to setEditing:animated: 1 2 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; [self.tableView setEditing:editing animated:YES]; if (editing) { addButton.enabled = NO; } else { addButton.enabled = YES; } }
  • 46. Deleting a Table View Row • Set the right item of the navigation bar to be standard Edit button • View controller responding to setEditing:animated: • Customizing the editing style of rows 1 2 3 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { SimpleEditableListViewController *controller = (SimpleEditableListViewController *)[[UIApplication sharedApplication] delegate]; if (indexPath.row == [controller countOfList]-1) { return UITableViewCellEditingStyleInsert; } else { return UITableViewCellEditingStyleDelete; } }
  • 47. Deleting a Table View Row • Set the right item of the navigation bar to be standard Edit button • View controller responding to setEditing:animated: • Customizing the editing style of rows • Updating the data-model array and deleting the row 1 2 3 4 - (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // If row is deleted, remove it from the list. if (editingStyle == UITableViewCellEditingStyleDelete) { SimpleEditableListViewController *controller = (SimpleEditableListViewController *)[[UIApplication sharedApplication] delegate]; [controller removeObjectFromListAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } }
  • 48. • Adding an Add button to the navigation bar • Responding to a tap on the Add button • Adding the new item to the data-model array Adding a Table View Row 1 2 3
  • 49. Adding a Table View Row • Adding an Add button to the navigation bar1 UIBarButtonItem addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addItemAction:)]; self.navigationItem.rightBarButtonItem = addButton;
  • 50. Adding a Table View Row • Adding an Add button to the navigation bar • Responding to a tap on the Add button 1 2 - (void)addItemAction:sender { if (itemInputController == nil) { itemInputViewController = [[ItemInputViewController alloc] init]; } UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:itemInputViewController]; [[self navigationController] presentModalViewController:navigationController animated:YES]; }
  • 51. Adding a Table View Row • Adding an Add button to the navigation bar • Responding to a tap on the Add button • Adding the new item to the data-model array 1 2 3 - (void)save:sender { UITextField *textField = [(EditableTableViewTextField *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] textField]; SimpleEditableListViewController *controller = (SimpleEditableListViewController *)[[UIApplication sharedApplication] delegate]; NSString *newItem = textField.text; if (newItem != nil) { [controller insertObject:newItem inListAtIndex:[controller countOfList]]; } [self dismissModalViewControllerAnimated:YES]; }
  • 54. • A search bar provides an interface for text-based searches with a text box and buttons such as search and cancel. • A search bar accepts text from users, which can be used as input for a search (shown here with placeholder text). • A scope bar, which is available only in conjunction with a search bar - allows users to define the scope of a search (shown here below a search bar). Introduction to Search Bar
  • 55. • Quickly find a value in a large collection • Create a scope filter Purpose
  • 56. • Search bars are implemented in the UISearchBar class • Configure search bars in Interface Builder, in the Search Bar section of the Attributes Inspector Implementation and Configuration
  • 57. • You can customize the appearance of a search bar by setting the properties depicted below: Customizing Search Bar Appearance • UISearchBar = Search bar + Scope
  • 58. • Creating a search bar in table view • Creating a search bar scope • Displaying the result in table view Working with Search Bar in Table View
  • 59. • Drag a search bar and search display controller from the library. • Interface Builder creates a search bar view controller and search display controllers, and it creates relationships between them. Creating a Search Bar in Table View
  • 60. • Creating a search bar scope Creating a Search Bar in Table View
  • 61. • Displaying the result in table view Creating a Search Bar in Table View #pragma mark - UISearchDisplayController Delegate Methods ! - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { NSString *searchString = [self.searchDisplayController.searchBar text]; NSString *scope; if (searchOption > 0) { scope = [[Product deviceTypeNames] objectAtIndex:(searchOption - 1)]; } [self updateFilteredContentForProductName:searchString type:scope]; // Return YES to cause the search result table view to be reloaded return YES; }
  • 63. UITableView https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/ UITableView.html Table View Programming Guide for iOS https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TableView_iPhone/ AboutTableViewsiPhone/AboutTableViewsiPhone.html UISearchBar https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/ UISearchBar.html Documentation
  • 65. Next: Working with Image, Scroll, Collection, Picker, Web View and Page Control