我有一個項目列表顯示在一個表視圖。每個項目都有其名稱,圖片,排名等屬性。 我的目標是,每當用戶選擇一行時,具有其屬性的項目將被添加到新列表中。訪問對象數組內的對象的屬性
我創建了一個名爲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
什麼「但它沒有工作」實際上是什麼意思? –