如何制作专属的UITableView列表用于复选标记?
我正在尝试制作一个 UITableView,当用户选择一个单元格时,它会显示一个复选标记,如果用户选择另一个单元格,它会删除先前选定的单元格的复选标记,并将复选标记放在新选定的单元格上。我在网上搜索过,但没有真正有效的好例子。 Apple 有一些示例代码位于 这里 解释并完全执行了我想要做的事情,但是当我将其复制并粘贴到我的项目中时,我收到错误。我试图修复这些错误,但苹果遗漏了一些声明或其他内容。我现在收到的唯一似乎无法修复的错误是 self.currentCategory 不是对象并且未找到。所以我的问题是如何修复Apple的代码以使其正常工作?有人可以为我声明/解释所有缺失的部分吗?这是苹果的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSInteger catIndex = [taskCategories indexOfObject:self.currentCategory];
if (catIndex == indexPath.row) {
return;
}
NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:catIndex inSection:0];
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
if (newCell.accessoryType == UITableViewCellAccessoryNone) {
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
self.currentCategory = [taskCategories objectAtIndex:indexPath.row];
}
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
}
I'm trying to make a UITableView that when a user selects a cell it display a checkmark and if the user selects a different cell it removes the previous selected cell's checkmark and puts the checkmark on the new selected cell. I've searched all around the web but there is no good examples that actually work. Apple has some sample code located here that explains and does exactly what I'm wanting to do but when I copy and paste it into my project I get errors. I've tried to fix the errors but Apple has left out some declarations or something. The only error I receive now that I can't seem to fix is self.currentCategory is not an object and not found. So my question is how can I fix Apple's code to make it work? Can someone declare/explain all the missing pieces for me? Here's Apple's code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSInteger catIndex = [taskCategories indexOfObject:self.currentCategory];
if (catIndex == indexPath.row) {
return;
}
NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:catIndex inSection:0];
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
if (newCell.accessoryType == UITableViewCellAccessoryNone) {
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
self.currentCategory = [taskCategories objectAtIndex:indexPath.row];
}
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那里有一两个缺失的部分。据我所知,您需要
在运行代码之前将其放在文件的顶部,然后在类中声明几个实例变量:
MyCustomCategoryClass
可以是任何自定义类你喜欢,但这就是你的用户正在选择的。如果你愿意的话,它甚至可以只是一个NSString
。我为currentCategory
说assign
是因为这实际上不是具有项目所有权的指针,拥有的引用应该位于数组中(即retain
>编辑)。您也许还可以将taskCategories
设为@property(nonatomic, keep)
,但我可能会将currentCategory
保留为atomic< /code> (默认值)以防万一当您想要阅读用户选择的内容时它正在更改。
哦,在允许用户从列表中进行选择之前,不要忘记用项目填充您的
taskCategories
。There are one or two missing pieces there. From what I can see, you need to put
at the top of the file that code is in before it will run, and then declare in the class a couple of instance variables:
MyCustomCategoryClass
can be whatever custom class you like, but that's what your user is choosing between. it can even just be anNSString
if you want. I sayassign
for thecurrentCategory
because that's really not the pointer with ownership of the item, the owning reference should be in the array (which isretain
ed). You might also be able to maketaskCategories
an@property(nonatomic, retain)
, but I would probably leavecurrentCategory
asatomic
(the default) just in case it's in the middle of changing when you want to read what the user has picked.Oh, and don't forget to populate your
taskCategories
with items before allowing the user to select from the list.