斯坦福大学公开课:IOS 7应用开发 lecture11

1.

2.

3.UITableView Protocols:How do we connect to all this stuff in our code?Via the UITableView’s datasource and delegate.

The delegate is used to control how the table is displayed.

The dataSource provides the data what is displayed inside the cells.

UITableViewController:Automatically sets itself as its UITableView’s delegate & dataSource.Also has a property pointing to its UITableView:

@property(nonatomic,strong)UITableView *tableView;

(this property is actually == self.view in UITableViewController!)

4.UITableViewDataSource:We have to implement these 3 to be a “dynamic”(arbitrary number of rows)table...

How many sections in the table?

How many rows in each section?

Give me a UITableViewCell to use to draw each cell at a given row in a given section.

5.How do we control what is drawn in each cell in a dynamic table?Each row is drawn by its own instance of UITableViewCell(a UIView subclass).

Here is the UITableViewDataSource method to get that cell for a given row in a section...

//NSIndexPath is just an object with two important properties for use with UITableView:row and section.
-(UITableViewCell *)tableView:(UITableView *)sender cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     //get a cell to use (instance of UITableViewCell)
     UITableViewCell *cell;
     cell = [self.tableView dequeueReusableCellWithIdentifier:@“Flickr Photo Cell” forIndexPath:indexPath];
     //set @properties on the cell to prepare to display
     //there are obviously other things you can do in the cell besides setting its text(detail text,image,checkmark,etc.).
     //See how we are using indexPath.section and indexPath.row to get Model information to set up this cell.
     cell.textLabel.text = [self getMyTitleForRow:indexPath.row inSection:indexPath.section];
     return cell;
}

The cells in the table are actually reused.When one goes off-screen,it gets put into a “reuse pool.”The next time a cell is needed,one is grabbed from the reuse pool if available.If none is available,one will be put into the reuse pool if there’s a prototype in the storyboard.Otherwise this dequeue method will return nil.

6.How does a dynamic table know how many rows there are?And how many sections,too, of course?

Via these two UITableViewDataSource methods...

-(NSInteger)numberOfSectionsInTableView:(UITableview *)sender;
-(NSInteger)tableView:(UITableView *)sender numberOfRowsInSection:(NSInteger)section;

Number of sections is 1 by default.

No default for tableView:numberOfRowsInSection:,this is a required method in this protocol(as is tableView:cellForRowAtIndexPath:).

Do not implement these dataSource methods for a static table.UITableViewController will take care of that for you.

7.UITableViewDelegate:the delegate controls how the UITableView is displayed.Not what it displays(that’s the dataSource’s job).

The delegate also lets you observe what the table view is doing:The classic”will/did”sorts of things.

An important one is “user did select a row."

Usually we don’t need this because we simply segue when a row is touched.

But there are some occasions where it will be useful...

8.UITableViewDelegate method sent when row is selected:This is sort of like “tableView target/action”(only needed if you’re not segueing,of course).On the iPad,where the table might be on screen with what it updates,you might need this.

-(void)tableView:(UITableView *)sender didSelectRowAtIndexPath:(NSIndexPath *)path{
     //go do something based on information about my model
     //corresponding to indexPath.row in indexPath.section
}

Remember the little curled i?Clicking on this will not segue.Instead it will invoke this method in the UITableViewDelegate protocol...

-(void)tableView:(UITableView *)sender accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
     //Do something related to the row at indexPath,
     //but not the primary action associated with touching the row
} 

9.UITableView Segue:The sender of prepareForSegue:sender:is the UITableViewCell.Use the important method indexPathForCell:to find out the indexPath of the row that’s segueing.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];

//prepare segue.destinationController to display based on information

//about my Model corresponding to indexPath.row in indexPath.section

}

10.UITableViewController has an “activity indicator”built in.You get it via this property in UITableViewController...

@property(strong)UIRefreshControl *refreshControl;

Start it with...

-(void)beginRefreshing;

Stop it with...

-(void)endRefreshing;

11.What if your Model changes?

-(void)reloadData;

Causes the table view to call numberOfSectionsInTableView: and numberOfRowsInSection: all over again and then cellForRowAtIndexPath: on each visible cell.

Relatively heavyweight,but if your entire data structure changes,that’s what you need.

If only part of your Model changes,there are lighter-weight reloads,for examples...

-(void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animationStyle;

时间: 2024-10-12 23:31:54

斯坦福大学公开课:IOS 7应用开发 lecture11的相关文章

斯坦福大学公开课:iOS 7应用开发 笔记

2015-07-06 第一讲   课务.iOS概述 -------------------------------------------------- 开始学习斯坦福大学公开课:iOS 7应用开发留下笔记

《斯坦福大学公开课:编程方法学》随笔

这是观看网易公开课 Mehran Sahami教授的<斯坦福大学公开课:编程方法学>后的随笔. 目前只看到第三次课,<Karel与Java>,我的收获有以下要点: 1.软件工程跟普通的写代码是不同的.软件应该考虑很多东西,比如:可移植性.便于升级维护等等,而不仅仅是写出代码实现功能那么简单. 2.代码是写给机器执行的,但更重要的是人要让人能看懂.(代码后期维护等等的工作量或者时间是写代码的10倍及更多,所以让代码更规范更易被人读懂很重要) 3.准确定义一个函数.一个类.一个包的功能

斯坦福大学公开课:iPad和iPhone应用开发(iOS5) 学习笔记 2

继续学习公开课 第二节课做了一个简单的计算器作为例子.大概Touch了如下知识点: 讲解了XCode4,我看了一下最新下载的是XCode8了. XCode创建工程, singleViewApplication还是有的,界面对比起XCode4来,更简洁些了,操作跟视频讲解里的差不多. 体会了下第一节课讲的MVC View的代码看不到这个感觉不太爽,特别是前面操作是将number的button拷贝到了 operation的 button,结构导致operation button也都连接到了digit

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture7

1.One is CGFloat.It’s a floating point number.All floating point numbers that have to do with drawing on the screen or getting touch events or whatever are CGFloats.This might be a double.It might be just a regular floating point number.Not only usin

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture4

1.All objects in an array are held onto strongly in the heap.So as long as that array itself is in the heap,as long as someone has a strong pointer to the array itself,all the objects the are in the array will stay in the heap as well.Because it has

斯坦福大学公开课:IOS 7应用开发 lecture10

1.Now,this line of code could cause trouble.If self.image is nil,because I told you that if you have a method,this is just a getter of the image that returns a struct,and you send it to nil,you’ll get undefined results.(47:00) 2.Zomming in,really sim

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture6

1.Abstract means that this class cannot be instantiated and used,it’s only useful as a superclass for other classes that are concrete. (04:00) 2.And I also like to document,even on my implementation any methods that basically are abstract.Any method

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture5

1.There is a class called NSNotificationCenter .It has a class method called defaultCenter.That returns a shared instance kind of like NSUserDefault,standard UserDefault did — a shared instance.That’s the object you use to tune into radio stations. A

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture1

1.The difference is card.h is the public API.That’s what your dot h is.It’s your public API.It’s what methods in your class you want to make public so that other people can call them.Card.m is your private API and all your implementation. 2.It’s impo