请教如何看 iOS 开发文档,有带范的吗

2014-02-21 22:43:24 +08:00
 tedd
正在学习看iOS官方开发文档,但有点看不懂,还望得到大家的指点,举个具体例子吧,我一个UIViewController作为UITableView的Delegate,看到文档说到conform UITableViewDataSource必须实现的tableView:cellForRowAtIndexPath方法,链接到该方法(如下),除了知道需要return cell外,余下的要怎么实现呢?也没有实例参考,请问下一步方向应该是怎么做呢


Asks the data source for a cell to insert in a particular location of the table view. (required)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Parameters
tableView
A table-view object requesting the cell.
indexPath
An index path locating a row in tableView.
Return Value
An object inheriting from UITableViewCell that the table view can use for the specified row. An assertion is raised if you return nil.

Discussion
The returned UITableViewCell object is frequently one that the application reuses for performance reasons. You should fetch a previously created cell object that is marked for reuse by sending a dequeueReusableCellWithIdentifier: message to tableView. Various attributes of a table cell are set automatically based on whether the cell is a separator and on information the data source provides, such as for accessory views and editing controls.

Availability
Available in iOS 2.0 and later.
Declared In
UITableView.h
3629 次点击
所在节点    iDev
12 条回复
alexrezit
2014-02-21 22:44:44 +08:00
到下面找 sample.
tedd
2014-02-21 22:46:16 +08:00
@alexrezit 请问下面是指?我将那页文档翻到最后也没有范例
alexrezit
2014-02-21 22:50:53 +08:00
@tedd
在 UITableView 的 reference 裏面搜 "Related Sample Code".
tedd
2014-02-21 22:55:46 +08:00
@alexrezit 感谢alexrezit!找到啦!这下好了
iPandaios
2014-02-21 22:58:55 +08:00
@protocol UITableViewDataSource<NSObject>

@required

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

@optional

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented

查看协议UITableViewDataSource,里面的关键字@required是必需要实现的,而@optional是非必需的
tedd
2014-02-22 00:01:56 +08:00
@iPandaios 这段代码清晰明了,请问是在哪里查到的呢


@alexrezit 另外想再请问问两位,一般要从何处了解到要成为某个Object的代理,要服从哪些protocol呢?譬如拿UIViewController成为UITableView的代理,要服从UITableViewDelegate和UITableViewDataSource,如果我不知道的情况下,应该如何查看呢?
vixvix
2014-02-22 00:48:33 +08:00
@tedd

UITableView
Overview
...
A UITableView object must have an object that acts as a data source and an object that acts as a delegate; typically these objects are either the application delegate or, more frequently, a custom UITableViewController object. The data source must adopt the UITableViewDataSource protocol and the delegate must adopt the UITableViewDelegate protocol.
...
zwzmzd
2014-02-22 00:57:15 +08:00
刚入门的时候开发者文档不那么好懂,建议弄个项目练练手,后面看文档就很自然了
cielpy
2014-02-22 01:10:51 +08:00
delegate这个我刚入门的时候好长时间没搞懂(基础本身不好,见笑了),建议如果delegate这个不懂的话就先记住怎么用吧,比如要使用tableView,就必须实现tableView的dataSource,不实现就不能用。如果obj1成为obj2的delegate,想看obj1要服从哪些delegate的话,去obj2的类里找@protocol,都有写明。熟练了也可以自己写delegate,不过现在觉得单纯做回调的话,block似乎更方便些。
qdvictory
2014-02-22 01:18:12 +08:00
转来转去永远是文档和demo,头文件也可以参考。重要的还是熟能生巧
laihj
2014-02-22 01:20:52 +08:00
建议先看Guide,再看Class Referance,比较好懂
tedd
2014-02-22 01:24:14 +08:00
@vixvix 这就是我希望找的!再回头去看了看文档,这么重要的一段话居然在第五段...一来就说多好

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/101294

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX