1. Trang chủ
  2. » Công Nghệ Thông Tin

CS193P - Lecture 8 pptx

64 183 0
Tài liệu được quét OCR, nội dung có thể không chính xác

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Scroll Views & Table Views
Trường học Stanford University
Chuyên ngành iPhone Application Development
Thể loại Lecture notes
Năm xuất bản 2023
Thành phố Stanford
Định dạng
Số trang 64
Dung lượng 0,93 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Using a Scroll View ¢ Create with the desired frame CGRect frame = CGRectMake0, @, 200, 200; scroLLView = [[UIScrolLView alloc] initWithFrame: frame ]; ¢ Add subviews frames may exten

Trang 1

CS193P - Lecture 8

iPhone Application Development

Scroll Views & Table Views

Trang 3

Announcements

¢ Enrolled students who requested iPod touches can pick them

up after class today

= Need Student ID

= No grade if not returned!

Trang 5

Scroll Views

Trang 6

UIScrollView

¢ For displaying more content than can fit on the screen

¢ Handles gestures for panning and zooming

¢ Noteworthy subclasses: UlTableView and UlTextView

Trang 8

Using a Scroll View

¢ Create with the desired frame

CGRect frame = CGRectMake(0, @, 200, 200);

scroLLView = [[UIScrolLView alloc] initWithFrame: frame ];

¢ Add subviews (frames may extend beyond scroll view frame)

frame = CGRectMake(0, 0, 500, 500);

myImageView = [[UIImageView alloc] initWithFrame: frame];

[scroLLView addSubview:myImageView] ;

¢ Set the content size

scroLLView contentSize = CGSizeMake(50@, 500);

Trang 9

Frame and Content

scroLLView frame

Trang 10

Frame and Content

scroLLVtew.contentS1ze

Trang 11

Frame and Content

scroLLView contentOffset

Trang 12

Demo:

Using a UlScrollView

Trang 13

Extending Scroll View Behavior

¢ Applications often want to know about scroll events

= When the scroll offset is changed

= When dragging begins & ends

= When deceleration begins & ends

Trang 14

Extending with a Subclass

¢ Create a subclass

¢ Override methods to customize behavior

¢ Issues with this approach

- Application logic and behavior is now part of a View class

= Tedious to write a one-off subclass for every scroll view instance

- Your code becomes tightly coupled with superclass

Trang 15

Extending with Delegation

¢ Delegate is a separate object

¢ Clearly defined points of responsibility

= Change behavior

- Customize appearance

* Loosely coupled with the object being extended

Trang 17

Implementing a Delegate

¢ Conform to the delegate protocol

@interface MyController : NSObject <UIScrolLViewDelegate>

¢ Implement all required methods and any optional methods

- (vo1td)scroLlVitewDidScroLL: CUIScroLLView *)scroLlLView

Trang 18

Zooming with a Scroll View

¢ Set the minimum, maximum, initial Zoom scales

scroLLView.maximumZoomScale = 2.0;

scroLLView.mrn1rmumZoomScgaLe = scroLLV1ew.s1ze.wtdth /

myImage.size.width;

¢ Implement delegate method for zooming

- (UIView *)viewForZoominginScrollView: CUIView *)view

i

return someViewThatW1LlBeScaled;

b

Trang 19

Demo:

Zooming with a UlScrollView

Trang 20

Table Views

Trang 21

Table Views

¢ Display lists of content

= Single column, multiple rows

= Vertical scrolling

- Large data sets

¢ Powerful and ubiquitous in iPhone applications

Trang 22

Table View Styles

wae! Friend Opportunity

##y Frontin' On Debra (DJ

-| Fulfillingness' First Fi

Trang 23

Table View Anatomy

Plain Style

9:42 AM

Table Header ———~> | Header

Footer

Trang 24

Table View Anatomy

Trang 25

Table View Anatomy

Trang 26

Table View Anatomy

Trang 27

Table View Anatomy

Trang 28

Table View Anatomy

Trang 29

Table View Anatomy

Trang 30

Table View Anatomy

Trang 31

Using Table Views

¢ Displaying your data in the table view

¢ Customizing appearance & behavior

Trang 32

Displaying Data in a Table View

Trang 33

A Naive Solution

¢ Table views display a list of data, so use an array

[myTablLeView setList:myListOfStuf Ff ];

¢ Issues with this approach

: All data is loaded upfront

: All data stays in memory

Trang 34

A More Flexible Solution

¢ Another object provides data to the table view

= Not all at once

- Just as it’s needed for display

¢ Like a delegate, but purely data-oriented

Trang 35

UlTableViewDataSource

¢ Provide number of sections and rows

// Optional method, defaults to 1 if not implemented

- CNSTnteger )numberOfSecttonsTnTabLeView:CUTTabLeView *)tabte;

- (UITabLeViewCeLL *)tablLeView: CUITabLeView *)tabLeView

ceLlLForRowAtIndexPath: CNSIndexPath *) indexPath;

Trang 40

Carrier

John Appleseed

Kate Bell

Anna Haro

Datasource Message Flow

tabLeView: ceL LForRowAtIndexPath:

Trang 42

NSIndexPath

¢ Generic class in Foundation

¢ Path to a specific node in a tree of nested arrays

> Be

Trang 43

NSIndexPath and Table Views

¢ Cell location described with an index path

= Section index + row index

¢ Category on NSIndexPath with helper methods

@interface NSIndexPath CUITableView)

+ CNSIndexPath *)indexPathForRow: CNSUInteger ) row

tnSection: CNSUInteger )section;

@propertyCnonatomic,readonly) NSUInteger section;

@propertyCnonatomic,readonly) NSUInteger row;

@end

Trang 44

Single Section Table View

¢ Return the number of rows

- CNSTnteger )tabLeV1ew: CUTTabLeView *)tabLeView

numberOfRowsInSection: CNSInteger )section

i

return [myStrings count];

b

¢ Provide a cell when requested

- (UITableViewCeLl *)tableView: CUITabLeView *)tableView

cel LForRowAtIndexPath: C(NSIndexPath *)indexPath

{

UITablLeViewCeLL *ceLl = .;

cell.text = LmyStrings objectAtIndex: tndexPath row ]

return [ceLL autorelease];

b

Trang 45

Cell Reuse

¢ When asked for a cell, it would be expensive to create a new cell each time

- (UITabLeViewCeLL *)tabLeView: CUITabLeView *)tabLeView

i cel LForRowAtIndexPath: C(NSIndexPath *)indexPath

UITabLeViewCeLL *ceLlL = [tabLeView

Trang 46

Triggering Updates

¢ When is the datasource asked for its data?

= When a row becomes visible

= When an update is explicitly requested by calling -reloadData

Trang 47

Additional Datasource Methods

¢ Titles for section headers and footers

¢ Allow editing and reordering cells

Trang 48

Appearance & Behavior

Trang 49

UlTableView Delegate

¢ Customize appearance and behavior

¢ Keep application logic separate from view

¢ Often the same object as datasource

Trang 50

Table View Appearance & Behavior

¢ Customize appearance of table view cell

- (vo1id)tabLeView: CUITableView *)tablLeView

wilLDispLayCeLL: UITabLeViewCeLL *)celLL

forRowAtIndexPath: CNSIndexPath *)indexPath;

¢ Validate and respond to selection changes

- CNSIndexPath *)tablLeView: CUITabLleView *)tableView

willLSelLectRowAtIndexPath: CNSIndexPath *)indexPath;

- (vo1id)tabLeView: CUITableView *)tablLeView

didSeLectRowAtIndexPath: CNSIndexPath *)indexPath;

Trang 51

Row Selection in Table Views

¢ In iPhone applications, rows rarely stay selected

¢ Selecting a row usually triggers an event

10:47 PM

All Contacts

Friends Work

Trang 52

Responding to Selection

// For a navigation hierarchy

(void)tabLeView: CUITabLeView *)tableView

didSeLectRowAtIndexPath: CNSIndexPath *)itndexPath

i

// Get the row and the object it represents

NSUInteger row = indexPath row

1d objectToDisplay = [myObjects objectAtIndex: row];

// Create a new view controller and pass it along MyViewControLler *myViewControLller = .;

myViewControLler.object = objectToDispLay;

LseLlf.navigationControLler

pushViewControT Ler:myVrewControLler animated: YES];

Trang 53

Altering or Disabling Selection

- CNSIndexPath *)tabLeView: CUITableView *)tableView

wiLLSeLectRowAtIndexPath: CNSIndexPath *)indexPath

b b

Trang 54

UlTableViewController

Trang 55

UlTableViewController

¢ Convenient starting point for view controller with a table view

= Table view is automatically created

- Controller is table view’s delegate and datasource

¢ Takes care of some default behaviors

: Calls -reloadData the first time it appears

= Deselects rows when user navigates back

= Flashes scroll indicators

Trang 56

Demo:

UlTableViewController

Trang 57

Table View Cells

Trang 58

Basic properties

¢ UlTableViewCell has image and text properties

ceLL.image = [UIImage imageNamed:@“obama.png” |;

cell.text = @“Barack Obama”;

Barack Obama

Trang 59

Accessory Types

// UITableView delegate method

- CUTTabLeViewCeLTLAccessoryType)tabLeView: CUTTabLeView *)tabLe

accessorylypeForRowWithIndexPath: CNSIndexPath *)indexPath;

UlTableViewCellAccessoryDisclosurelndicator Barack Obama

UlTableViewCellAccessoryDetailDisclosureButton Barack Obama

UlTableViewCellAccessoryCheckmark Barack Obama

- (vo1id)tabLeView: CUITableView *)tabLeView

accessoryButtonT appedForRowWithIndexPath: CNSIndexPath *)indexPath

{

// Only for the blue disclosure button

NSUInteger row = indexPath row;

Trang 60

Customizing the Content View

¢ For cases where a simple image + text cell doesn’t suffice

¢ UlTableViewCell has a content view property

- Add additional views to the content view

- (UITabLeViewCeLL *)tableView: CUITabLleView *)tableView

cel LForRowAtIndexPath: CNSIndexPath *)indexPath

{

UITablLeViewCeLL *ceLl = .;

CGRect frame = cell.contentView bounds;

UILabel *myLabel = [[UILabel alloc] initWithFrame: frame];

myLabeL.text = .;

LceLlL.contentView addSubview:myLabel |;

[myLabel release];

$

Trang 61

Custom Row Heights

¢ Rows in a table view may have variable heights

¢ NSString category in UIStringDrawing.h is very useful for

computing text sizes

- CCGFLoat)tabLeView: CUTTabLeView *)tabLeVtew

heightForRowAtIndexPath: CNSIndexPath *)indexPath

{

NSString *text = .;

UIFont *font = [UIFont systemFontOfSize: ];

CGSize withinSize = CGSizeMakeCtabLeView.width, 1200];

CGSize size = [text sizeWithFont: font

constrainedToS1ze:w1thrnStze

lL1neBreakMode :UTLtneBreakModeWordWrop |;

return size.height + somePadding;

Trang 62

Questions?

Trang 63

Presence 2

¢ Due next Tuesday 5/5 at 11:59PM

= Displaying dynamic data with table views

- Fetching data over the Internet using web services

Trang 64

Demo:

Presence - Part 2

Ngày đăng: 12/07/2014, 12:20

TỪ KHÓA LIÊN QUAN