如何制作专属的UITableView列表用于复选标记?

发布于 2024-10-31 09:23:20 字数 1433 浏览 3 评论 0原文

我正在尝试制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有心的人 2024-11-07 09:23:20

那里有一两个缺失的部分。据我所知,您需要

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

在运行代码之前将其放在文件的顶部,然后在类中声明几个实例变量:

@interface myClass
{
  MyCustomCategoryClass *currentCategory; // pointer to the currently checked item
  NSArray *taskCategories; // pointer to an array of your list items
      ... // other instance variables here
}

    ... // other @property declarations here or in your .m file
@property (assign) MyCustomCategoryClass *currentCategory;
@property (retain) NSArray *taskCategories;

MyCustomCategoryClass 可以是任何自定义类你喜欢,但这就是你的用户正在选择的。如果你愿意的话,它甚至可以只是一个 NSString 。我为 currentCategoryassign 是因为这实际上不是具有项目所有权的指针,拥有的引用应该位于数组中(即 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

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

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:

@interface myClass
{
  MyCustomCategoryClass *currentCategory; // pointer to the currently checked item
  NSArray *taskCategories; // pointer to an array of your list items
      ... // other instance variables here
}

    ... // other @property declarations here or in your .m file
@property (assign) MyCustomCategoryClass *currentCategory;
@property (retain) NSArray *taskCategories;

MyCustomCategoryClass can be whatever custom class you like, but that's what your user is choosing between. it can even just be an NSString if you want. I say assign for the currentCategory because that's really not the pointer with ownership of the item, the owning reference should be in the array (which is retained). You might also be able to make taskCategories an @property(nonatomic, retain), but I would probably leave currentCategory as atomic (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.

那请放手 2024-11-07 09:23:20
@property (nonatomic,retain) NSIndexPath *oldIndexPath;
@synthesize oldIndexPath;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];

   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.oldIndexPath];
        if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
            oldCell.accessoryType = UITableViewCellAccessoryNone;
        }
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    self.oldIndexPath = indexPath;
} else if(cell.accessoryType == UITableViewCellAccessoryCheckmark){
    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.oldIndexPath];
    if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        oldCell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell.accessoryType = UITableViewCellAccessoryNone;

    self.oldIndexPath = indexPath;
}//cell acctype

}
@property (nonatomic,retain) NSIndexPath *oldIndexPath;
@synthesize oldIndexPath;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];

   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.oldIndexPath];
        if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
            oldCell.accessoryType = UITableViewCellAccessoryNone;
        }
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    self.oldIndexPath = indexPath;
} else if(cell.accessoryType == UITableViewCellAccessoryCheckmark){
    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.oldIndexPath];
    if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        oldCell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell.accessoryType = UITableViewCellAccessoryNone;

    self.oldIndexPath = indexPath;
}//cell acctype

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文