Xcode 初学者;重用标识符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24569803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Beginner in Xcode; reuse identifiers?
提问by user3804662
I have recently started to build a very basic To-Do List in Xcode 5 as my first project. Having practically finished the UI design and build I am now stuck on the implementation of data into my TableView. I have added a list of 13 items in my list and given the first in the list the 'ListPrototypeCell' identifier and all of my code seems correct. My project has 3 identical issues:
我最近开始在 Xcode 5 中构建一个非常基本的待办事项列表作为我的第一个项目。几乎完成了 UI 设计和构建后,我现在坚持将数据实现到我的 TableView 中。我在我的列表中添加了一个包含 13 个项目的列表,并为列表中的第一个指定了“ListPrototypeCell”标识符,我的所有代码似乎都是正确的。我的项目有 3 个相同的问题:
Unsupported Configuration; Prototype cells must have reuse identifiers
I have played around with identifiers on every item, although I have been told I don't need to use an identifier on every item, and I still get these errors.
我在每个项目上都使用了标识符,尽管有人告诉我我不需要在每个项目上都使用标识符,但我仍然遇到这些错误。
I am willing to send my project onto anyone that thinks they may be able to help me resolve the issues, to the trained eye it is probably a very basic mistake I have made.
我愿意将我的项目发送给任何认为他们可以帮助我解决问题的人,对于训练有素的人来说,这可能是我犯的一个非常基本的错误。
I appreciate any help!
我感谢任何帮助!
回答by Suhail kalathil
Try this Check your storyboard and confirm there is a reuse identifier for your prototypeCell,
试试这个检查你的故事板并确认你的prototypeCell有一个重用标识符,
And use the same identifier in your
并在您的
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"sameReuseIdentifier"];
回答by michaelsnowden
Go into your storyboard, go to the view controller, go to the table view, go to the tableviewcell, go to the identity inspector, and enter something into the field that says "Reuse Identifier"
进入你的故事板,进入视图控制器,进入表格视图,进入表格视图单元,进入身份检查器,然后在显示“重用标识符”的字段中输入一些内容
You use the reuse identifier for initializing cells based off of the style they are in the table view like this:
您可以使用重用标识符根据它们在表格视图中的样式来初始化单元格,如下所示:
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
回答by Oxcug
The reason you're getting that error is because some of your prototype cells do not have a reuse identifier on them.
您收到该错误的原因是您的某些原型单元格上没有重用标识符。
If you don't understand how/when to use prototypes and reuse-id's. You should read: Table View Programming Guide for iOS
如果您不了解如何/何时使用原型和重用 ID。您应该阅读:iOS 表视图编程指南
回答by Moinuddin Girach
reuseidentifier is an id from which you can get cell from it. if you set reuse id "cell" you can access this cell in cellForRowAtIndexPath method
Reuseidentifier 是一个 id,您可以从中获取单元格。如果您设置重用 ID“单元格”,您可以在 cellForRowAtIndexPath 方法中访问此单元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
which means that get cell from stroyboard with reuseid "cell". in your case you have to write above two line as follows
这意味着从带有重用标识“单元格”的 stroyboard 中获取单元格。在您的情况下,您必须按如下方式写在两行以上
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
this line shows that every time new cell will be alloc and initialized and it does not use prototype cell.
此行显示每次将分配和初始化新单元格并且它不使用原型单元格。
so prototype cell from storyboard is never used. if you want i can give you demo to mention how it works.
所以从不使用故事板中的原型单元格。如果你愿意,我可以给你演示来说明它是如何工作的。
回答by Natalia
I know this is old but i saw this explanation of reuse identifiers and it really helped me understand why to use reuse identifiers, so I wanted to share in case it helps others.
我知道这是旧的,但我看到了这个对重用标识符的解释,它确实帮助我理解了为什么要使用重用标识符,所以我想分享一下,以防它对其他人有帮助。
"The reuseIdentifier is used to group together similar rows in an UITableView; i.e., rows that differ only in their content, but otherwise have similar layouts.
“reuseIdentifier 用于将 UITableView 中的相似行组合在一起;即,仅在内容上不同的行,但在其他方面具有相似的布局。
A UITableView will normally allocate just enough UITableViewCell objects to display the content visible in the table. If reuseIdentifier is set to a non-nil value, then when the table view is scrolled, UITableView will first attempt to reuse an already allocated UITableViewCell with the same reuseIdentifier. If reuseIdentifier has not been set, the UITableView will be forced to allocate new UITableViewCell objects for each new item that scrolls into view, potentially leading to laggy animations."
UITableView 通常会分配足够的 UITableViewCell 对象来显示表格中可见的内容。如果reuseIdentifier 设置为一个非nil 值,那么当table view 滚动时,UITableView 将首先尝试重用已经分配的具有相同reuseIdentifier 的UITableViewCell。如果未设置重用标识符,UITableView 将被迫为滚动到视图中的每个新项目分配新的 UITableViewCell 对象,这可能会导致动画滞后。”