cellForRowAtIndexPath 如何工作? [英] How does cellForRowAtIndexPath work?

查看:23
本文介绍了cellForRowAtIndexPath 如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了苹果文档,对于像我这样的 Objective-C 初学者来说,这是不可理解的.我正在尝试按照这个 link 示例实现多列 UITableView,它只是不起作用,所以我需要理解 cellForRowAtIndexPath 是如何工作的,因为对我个人来说,这个方法似乎很复杂.

I HAVE READ apple documentation and it's not understandable for such a beginner in Objective-C as me. I'm trying to implement multicolumn UITableView following this link example and it just doesn't work so i need to comprehend how cellForRowAtIndexPath work, cause for me personally this method seems pretty complicated.

1) 它返回什么?UITableViewCell?但是为什么看起来这么奇怪呢?

1) What does it return? UITableViewCell? But why does it look so odd?

-(UITableViewCell *)tableView:(UITableView *)tableView 

  • 那是什么?你能解释一下吗?
  • 2) 它是如何被调用的,更重要的是我如何将它连接到某个 UITableView ???如果我有两个 UITableView 名为 firstTableViewsecondTableView 并且我希望它们不同(以执行 cellForRowAtIndexPath> 不同)?我应该如何将我的 UITableViews 链接到这个

    2) How does it get called and what is more important how am i to connect it to a certain UITableView??? What if i have two UITableView's named firstTableView and secondTableView and i want them to be different (to perform cellForRowAtIndexPath differently)? How am i supposed to link my UITableViews to this

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

    该方法接受NSIndexPath,而不是UITableView.我该怎么办?

    the method accepts NSIndexPath, not UITableView. What am i gonna do?

    推荐答案

    1) 该函数返回一个表格视图的单元格是吗?因此,返回的对象属于 UITableViewCell 类型.这些是您在表的行中看到的对象.这个函数基本上返回一个单元格,用于表格视图.但是您可能会问,函数如何知道要为哪一行返回哪个单元格,这在第二个问题中得到了回答

    1) The function returns a cell for a table view yes? So, the returned object is of type UITableViewCell. These are the objects that you see in the table's rows. This function basically returns a cell, for a table view. But you might ask, how the function would know what cell to return for what row, which is answered in the 2nd question

    2)NSIndexPath 本质上是两件事-

    2)NSIndexPath is essentially two things-

    • 你的部分
    • 你的行

    因为你的表格可能被分成许多部分,每个部分都有自己的行,这个 NSIndexPath 将帮助你准确地识别哪个部分和哪一行.它们都是整数.如果您是初学者,我会说只尝试一个部分.

    Because your table might be divided to many sections and each with its own rows, this NSIndexPath will help you identify precisely which section and which row. They are both integers. If you're a beginner, I would say try with just one section.

    如果您在视图控制器中实现了 UITableViewDataSource 协议,则会调用它.一种更简单的方法是添加一个 UITableViewController 类.我强烈推荐这个,因为 Apple 为您编写了一些代码,可以轻松实现可以描述表格的功能.无论如何,如果你选择自己实现这个协议,你需要创建一个 UITableViewCell 对象并为任何行返回它.查看它的类参考以了解可重用性,因为在表格视图中显示的单元格会被一次又一次地重用(顺便说一句,这是一个非常有效的设计).

    It is called if you implement the UITableViewDataSource protocol in your view controller. A simpler way would be to add a UITableViewController class. I strongly recommend this because it Apple has some code written for you to easily implement the functions that can describe a table. Anyway, if you choose to implement this protocol yourself, you need to create a UITableViewCell object and return it for whatever row. Have a look at its class reference to understand re-usablity because the cells that are displayed in the table view are reused again and again(this is a very efficient design btw).

    至于当你有两个表视图时,看方法.表视图被传递给它,所以你应该没有问题.

    As for when you have two table views, look at the method. The table view is passed to it, so you should not have a problem with respect to that.

    这篇关于cellForRowAtIndexPath 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆