V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
tedd
V2EX  ›  iDev

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

  •  
  •   tedd · 2014-02-21 22:43:24 +08:00 · 3620 次点击
    这是一个创建于 3728 天前的主题,其中的信息可能已经有所发展或是发生改变。
    正在学习看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
    12 条回复    1970-01-01 08:00:00 +08:00
    alexrezit
        1
    alexrezit  
       2014-02-21 22:44:44 +08:00   ❤️ 1
    到下面找 sample.
    tedd
        2
    tedd  
    OP
       2014-02-21 22:46:16 +08:00
    @alexrezit 请问下面是指?我将那页文档翻到最后也没有范例
    alexrezit
        3
    alexrezit  
       2014-02-21 22:50:53 +08:00   ❤️ 1
    @tedd
    在 UITableView 的 reference 裏面搜 "Related Sample Code".
    tedd
        4
    tedd  
    OP
       2014-02-21 22:55:46 +08:00
    @alexrezit 感谢alexrezit!找到啦!这下好了
    iPandaios
        5
    iPandaios  
       2014-02-21 22:58:55 +08:00   ❤️ 1
    @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
        6
    tedd  
    OP
       2014-02-22 00:01:56 +08:00
    @iPandaios 这段代码清晰明了,请问是在哪里查到的呢


    @alexrezit 另外想再请问问两位,一般要从何处了解到要成为某个Object的代理,要服从哪些protocol呢?譬如拿UIViewController成为UITableView的代理,要服从UITableViewDelegate和UITableViewDataSource,如果我不知道的情况下,应该如何查看呢?
    vixvix
        7
    vixvix  
       2014-02-22 00:48:33 +08:00   ❤️ 1
    @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
        8
    zwzmzd  
       2014-02-22 00:57:15 +08:00   ❤️ 1
    刚入门的时候开发者文档不那么好懂,建议弄个项目练练手,后面看文档就很自然了
    cielpy
        9
    cielpy  
       2014-02-22 01:10:51 +08:00   ❤️ 1
    delegate这个我刚入门的时候好长时间没搞懂(基础本身不好,见笑了),建议如果delegate这个不懂的话就先记住怎么用吧,比如要使用tableView,就必须实现tableView的dataSource,不实现就不能用。如果obj1成为obj2的delegate,想看obj1要服从哪些delegate的话,去obj2的类里找@protocol,都有写明。熟练了也可以自己写delegate,不过现在觉得单纯做回调的话,block似乎更方便些。
    qdvictory
        10
    qdvictory  
       2014-02-22 01:18:12 +08:00 via iPhone   ❤️ 1
    转来转去永远是文档和demo,头文件也可以参考。重要的还是熟能生巧
    laihj
        11
    laihj  
       2014-02-22 01:20:52 +08:00   ❤️ 1
    建议先看Guide,再看Class Referance,比较好懂
    tedd
        12
    tedd  
    OP
       2014-02-22 01:24:14 +08:00
    @vixvix 这就是我希望找的!再回头去看了看文档,这么重要的一段话居然在第五段...一来就说多好
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4685 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 04:06 · PVG 12:06 · LAX 21:06 · JFK 00:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.