简体   繁体   English

如何在Objective-C中创建对象?

[英]How to create object in Objective-C?

I am new in iOS development. 我是iOS开发的新手。 Currently I am reading this tutorial http://www.appcoda.com/how-to-handle-row-selection-in-uitableview/ . 目前,我正在阅读本教程http://www.appcoda.com/how-to-handle-row-selection-in-uitableview/ I am facing problem when I am reading this line 我在阅读此行时遇到问题

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;

I know object in objective-c is created by following way 我知道objective-c中的对象是通过以下方式创建的

 classname *objecname = [[classname alloc]init];

My confusion point is here UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 我的困惑点是UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

How cell object is created here? 这里如何创建cell对象? Please tell details. 请告诉细节。

Based on the tutorial you linked to, I'm assuming you are dealing with selecting a row. 根据您链接到的教程,我假设您正在处理选择行。 When you select a row, you have access to the NSIndexPath of that row, which contains two parts: 选择一行时,您可以访问该行的NSIndexPath ,其中包含两个部分:

  1. The section of the cell 单元格部分
  2. The row of the cell 单元格的行

With that information, let's break down the confusing code: UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 有了这些信息,让我们分解混乱的代码: UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

UITableViewCell *cell

This part declares your variable. 这部分声明您的变量。 You've chosen to name it cell . 您已选择将其命名为cell You could just as easily have named it theCell , like so: 您可以轻松地将其命名为theCell ,如下所示:

UITableViewCell *theCell

The * means that you're declaring a pointer, which is just a reference to an actual object, or the table view cell. *表示您正在声明一个指针,该指针仅是对实际对象或表视图单元格的引用。

[tableView cellForRowAtIndexPath:indexPath]

The tableView refers to the UITableView that was just selected. tableView引用刚刚选择的UITableView UITableView has a method called cellForRowAtIndexPath , and what that method does is retrieve the cell at the specified section and row of the UITableView . UITableView有一个称为cellForRowAtIndexPath的方法,该方法的作用是在UITableView的指定节和行中检索单元格。 In your case, it retrieves the row that you've just selected and stores the reference to it in your cell variable. 在您的情况下,它将检索刚刚选择的行,并将对该行的引用存储在cell变量中。

When declaring a new object, yes, it would take on the following syntax: 是的,当声明一个对象时,它将采用以下语法:

classname *objecname = [[classname alloc] init];

The key word here is new . 这里的关键词是new When dealing with selecting rows in a UITableView , you don't want to create a new cell because you can't select a cell that doesn't exist. 在处理UITableView行时,您不想创建新的单元格,因为您无法选择不存在的单元格。 You want to get the cell that the user has just selected. 您要获取用户刚刚选择的单元格。

I assume you have this code in your didSelectRowAtIndexPath: method, is that right? 我假设您在didSelectRowAtIndexPath:方法中有此代码,对吗?

If so, you're tapping on a cell that is on the screen, so the cell object already exists. 如果是这样,则您正在点击屏幕上的一个单元格,因此该单元格对象已经存在。 Where and how was it created? 它是在哪里创造的? Inside cellForRowAtIndexPath: , which you have already written. cellForRowAtIndexPath:内部,您已经编写了。

So when you make this call 所以当你打电话时

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

it just gives you the cell object to work with, but it doesn't create a new one. 它只是为您提供要使用的单元对象,但不会创建新的单元对象。

The cell is not created with that line. 单元格不是用该行创建的。 That line gets a reference to a cell that is already in the table view at that indexPath. 该行将引用该indexPath的表视图中已存在的单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM