我有一个项目列表显示在一个表视图。每个项目都有其名称,图片,排名等属性。 我的目标是,每当用户选择一行时,具有其属性的项目将被添加到新列表中。访问对象数组内的对象的属性
我创建了一个名为listOfBugs
的新列表,因为我希望它成为全球我已经分配并初始化它在viewDidLoad
内。 (那是一个正确的事是什么?)
这里是我的代码:
MasterViewController.h
@interface MasterViewController : UITableViewController
{
NSMutableArray *listOfBugs;
}
@property (strong) NSMutableArray *bugs;
MasterViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
listOfBugs = [[NSMutableArray alloc]init];
self.title = @"Scary Bugs";
}
...
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ScaryBugDoc *bug = [self.bugs objectAtIndex:indexPath.row];
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:@"Row Selected" message:bug.data.title delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[messageAlert show];
[listOfBugs addObject:bug];
NSLog(@"this is %@",listOfBugs);
}
使用NSLog
我可以看到,添加对象:
ScaryBugs[1195:11303] this is <ScaryBugDoc: 0x75546e0>
2012-12-05 17:45:13.100
ScaryBugs[1195:11303] this is <ScaryBugDoc: 0x75546e0>
我有几个问题。
1.如何访问数组listOfBugs中的对象的属性?
更新:这为我工作:
NSLog(@"this is %@",((ScaryBugDoc *)[listOfBugs objectAtIndex:0]).data.title);
但我不能从另一个类访问listOfBugs
。
我把它变成了一个属性,建议让我的生活更轻松,但仍然无法从另一个类访问它。 例如在listOfBugsViewController.m
return [_listOfBugs count];
会给我未声明的标识符的“_listOfBugs”
2.我想abale来填充自定义列表表视图的错误使用,我该怎么办呢?
完成后,我想将列表保存为plist,并且可以轻松地添加和删除对象,因此我需要考虑这一点。
This is the code that I'm based on, I only made a few adjustments to create the new list
难道你不能只是创建一个临时对象,它是你想要的对象的类型,并将该值设置为objectAtIndex:并使用它的属性? – CBredlow
我不知道我跟着你,但想法是有一个音乐专辑列表,每张专辑都有一个歌曲列表,每首歌都有它的属性。用户将能够从任何专辑中选择歌曲来创建新的歌曲列表。 – Segev
什么“但它没有工作”实际上是什么意思? –