cse438 lecture7 6-25-20 - compatibility modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3extensible...

23
1 - CSE 438 – Mobile Application Development Today’s Topics Scroll views Table views Displaying data Controlling appearance & behavior UITableViewController Table view cells Collection views 1 2 - CSE 438 – Mobile Application Development Scroll Views 2

Upload: others

Post on 25-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 11 - CSE 438 – Mobile Application Development

Today’s Topics

• Scroll views

• Table views– Displaying data – Controlling appearance & behavior

• UITableViewController

• Table view cells

• Collection views

1

Extensible Networking Platform 22 - CSE 438 – Mobile Application Development

Scroll Views

2

Page 2: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 33 - CSE 438 – Mobile Application Development

UIScrollView

• For displaying more content than can fit on the screen

• Handles gestures for panning and zooming

• Noteworthy subclasses: UITableView and UITextView

3

Extensible Networking Platform 44 - CSE 438 – Mobile Application Development

Scrolling Examples

4

Page 3: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 55 - CSE 438 – Mobile Application Development

Using a Scroll View• Create with the desired frame

let theFrame = CGRect(x:0, y:0, width:200, height:200) let scrollView = UIScrollView(frame: theFrame)

• Add subviews (frames may extend beyond scroll view frame)let bigFrame = CGRect(x:0, y:0, width:500, height:500)let myImageView = UIImageView(frame: bigFrame) scrollView.addSubview(myImageView)

• Set the content size–scrollView.contentSize = CGSize(width: 500, height: 500)

5

Extensible Networking Platform 66 - CSE 438 – Mobile Application Development

Frame and Content

scrollView.frame

6

Page 4: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 77 - CSE 438 – Mobile Application Development

Frame and Content

scrollView.contentSize

7

Extensible Networking Platform 88 - CSE 438 – Mobile Application Development

Frame and Content

scrollView.contentOffset

8

Page 5: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 99 - CSE 438 – Mobile Application Development

Demo

Using a UIScrollView

9

Extensible Networking Platform 1010 - CSE 438 – Mobile Application Development

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

10

Page 6: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 1111 - CSE 438 – Mobile Application Development

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

11

Extensible Networking Platform 1212 - CSE 438 – Mobile Application Development

Extending with Delegation

• Delegate is a separate object

• Clearly defined points of responsibility– Change behavior – Customize appearance

• Loosely coupled with the object being extended

12

Page 7: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 1313 - CSE 438 – Mobile Application Development

UIScrollView Delegatepublic protocol UIScrollViewDelegate:NSObjectProtocol

// Respond to interesting eventsoptional public func scrollViewDidScroll(_ scrollView: UIScrollView)optional public func scrollViewDidZoom(_ scrollView: UIScrollView)

...// Influence behavior// return a yes if you want to scroll to the top. if not defined, assumes YESoptional public func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool

13

Extensible Networking Platform 1414 - CSE 438 – Mobile Application Development

Implementing a Delegate

• Conform to the delegate protocolclass ViewController: UIViewController, UIScrollViewDelegate {

• Implement all required methods and any optional methods

func scrollViewDidScroll(_ scrollView: UIScrollView){// Do something in response to the new scroll position

if (scrollView.contentOffset ...) {

}}

14

Page 8: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 1515 - CSE 438 – Mobile Application Development

Zooming with a Scroll View

• Set the minimum, maximum, initial zoom scalesscrollView.maximumZoomScale = 5.0scrollView.minimumZoomScale = 1.0

• Implement delegate method for zoomingfunc viewForZooming(in scrollView: UIScrollView) -> UIView? {

return someViewThatWillBeScaled}

15

Extensible Networking Platform 1616 - CSE 438 – Mobile Application Development

DemoZooming with a UIScrollView

16

Page 9: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 1717 - CSE 438 – Mobile Application Development

Table Views

17

Extensible Networking Platform 1818 - CSE 438 – Mobile Application Development

Table Views

• Display lists of content– Single column, multiple rows– Vertical scrolling– Large data sets

• Powerful and ubiquitous in iPhone applications

18

Page 10: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 1919 - CSE 438 – Mobile Application Development

Table View StylesUITableViewStylePlain UITableViewStyleGrouped UITableViewStyleInsetGrouped

19

Extensible Networking Platform 2020 - CSE 438 – Mobile Application Development

Table View Anatomy – Plain Style

Table Header ->

20

Page 11: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 2121 - CSE 438 – Mobile Application Development

Table View Anatomy – Plain Style

Section Header ->

21

Extensible Networking Platform 2222 - CSE 438 – Mobile Application Development

Table View Anatomy – Plain Style

Table Cell->

22

Page 12: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 2323 - CSE 438 – Mobile Application Development

Table View Anatomy – Plain Style

Section Footer ->

23

Extensible Networking Platform 2424 - CSE 438 – Mobile Application Development

Table View Anatomy – Plain Style

Section ->

24

Page 13: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 2525 - CSE 438 – Mobile Application Development

Table View Anatomy – Plain Style

Table Footer->

25

Extensible Networking Platform 2626 - CSE 438 – Mobile Application Development

Table View Anatomy – Plain Style

Table Cell->Section Footer ->

Section Header ->

Section ->

Table Footer->

Table Header ->

26

Page 14: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 2727 - CSE 438 – Mobile Application Development

Table View Anatomy – Grouped Style

Table Cell->

Section Footer ->

Section Header ->

Section ->

Table Footer->

Table Header ->

27

Extensible Networking Platform 2828 - CSE 438 – Mobile Application Development

Table View Anatomy – Grouped Inset Style

Table Cell->

Section Footer ->

Section Header ->

Section ->

Table Footer->

Table Header ->

28

Page 15: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 2929 - CSE 438 – Mobile Application Development

Displaying Data in a Table View

29

Extensible Networking Platform 3030 - CSE 438 – Mobile Application Development

A Naïve Solution

• Table views display a list of data, so use an arraymyTableView.setList(myListOfStuff)

• Issues with this approach– All data is loaded upfront– All data stays in memory

30

Page 16: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 3131 - CSE 438 – Mobile Application Development

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

31

Extensible Networking Platform 3232 - CSE 438 – Mobile Application Development

UITableViewDataSource

• Provide number of sections and rows

// Optional method, defaults to 1 if not implementedfunc numberOfSections(in tableView: UITableView) -> Int

// Required methodfunc tableView(_ tableView: UITableView, numberOfRowsInSection

section: Int) -> Int

• Provide cells for table view as needed // Required method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

32

Page 17: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 3333 - CSE 438 – Mobile Application Development

Datasource Message Flow

numberOfSections(in tableView: UITableView) -> Int

Datasource

How many Sections?

5

33

Extensible Networking Platform 3434 - CSE 438 – Mobile Application Development

Update Datasource Message FlowtableView(_ tableView: UITableView,

numberOfRowsInSection section section:Int ) -> Int

Datasource

How many rows in Section 0?

1

34

Page 18: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 3535 - CSE 438 – Mobile Application Development

Datasource Message FlowtableView(_ tableView: UITableView, cellForRowAt

indexPath: IndexPath) -> UITableViewCell

Datasource

What to display at section 0, row 0?

“John Appleseed”

35

Extensible Networking Platform 3636 - CSE 438 – Mobile Application Development

IndexPath

• Generic class in Foundation• Path to a specific node in a tree of nested arrays

36

Page 19: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 3737 - CSE 438 – Mobile Application Development

Single Section Table View

Return the number of rows

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

{return myStrings.count

}

Provide a cell when requestedoverride func tableView(_ tableView: UITableView, cellForRowAt indexPath:

IndexPath) -> UITableViewCell {let cell = …cell.textLabel.text = myStrings[indexPath.row]return cell

}

37

Extensible Networking Platform 3838 - CSE 438 – Mobile Application Development

Creating Cells without reuse

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let myCell = UITableViewCell(style: .default, reuseIdentifier: nil)

myCell.textLabel.text = myStrings[indexPath.row]

return myCell}

38

Page 20: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 3939 - CSE 438 – Mobile Application Development

Cell Reuse

• When asked for a cell, it would be expensive to create a new cell each time.– First register the class for use in creating new cells

override func viewDidLoad(){ …

tableView.register (UITableViewCell.self, forCellReuseIdentifier: “theCell”)}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let myCell = tableView.dequeueReusableCellWithIdentifier(”theCell")! as UITableViewCellcell.textLabel.text = myStrings[indexPath.row]return cell

}

39

Extensible Networking Platform 4040 - CSE 438 – Mobile Application Development

Triggering Updates

• When is the datasource asked for its data?– When a row becomes visible– When an update is explicitly requested by calling -

reloadData

override func viewWillAppear(animated: Bool) {super.viewWillAppear(animated)tableView.reloadData()

}

40

Page 21: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 4141 - CSE 438 – Mobile Application Development

Basic properties

• UITableViewCell has image and text properties

cell.imageView?.image = UIImage.init(named: “obama.png”)cell.textLabel?.text = “Barack Obama”

41

Extensible Networking Platform 4242 - CSE 438 – Mobile Application Development

Demo

42

Page 22: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 4343 - CSE 438 – Mobile Application Development

UICollectionView

• Convenient way to show information in a grid format

• Similar to a UITableView– But with multiple columns

• Flow based layout makes displaying rows and columns of data very easy

43

Extensible Networking Platform 4444 - CSE 438 – Mobile Application Development

UICollectionViewfunc numberOfSections (in collectionView: UICollectionViews) -> Int {

//return number of sections}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

//return number of items per section}

func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ”someCellNameId” for: indexPath)

// configure the cell // return UICollectionView cell for item at IndexPathreturn cell

}

44

Page 23: cse438 lecture7 6-25-20 - Compatibility Modetodd/cse438/cse438_lecture7_6-25-20.pdf · 3Extensible Networking Platform-CSE 438 –Mobile Application Development 3 UIScrollView •For

Extensible Networking Platform 4545 - CSE 438 – Mobile Application Development

UICollectionView Demo

45

Extensible Networking Platform 4646 - CSE 438 – Mobile Application Development

Lab 3 Demo

46