Iphone 从plist填充的应用程序添加收藏夹
我使用一个带有字典数组的plist来填充TableView和TableView中选定单元格(原型单元格)的DetailView。我想在按下所选词典的按钮时将其添加到收藏夹选项卡(另一个表视图)。这是我目前的代码: 到目前为止,DetailViewController中的代码:Iphone 从plist填充的应用程序添加收藏夹,iphone,xcode,nsdictionary,nsnotificationcenter,favorites,Iphone,Xcode,Nsdictionary,Nsnotificationcenter,Favorites,我使用一个带有字典数组的plist来填充TableView和TableView中选定单元格(原型单元格)的DetailView。我想在按下所选词典的按钮时将其添加到收藏夹选项卡(另一个表视图)。这是我目前的代码: 到目前为止,DetailViewController中的代码: -(IBAction)FavoriteButton:(id)sender { [[NSNotificationCenter defaultCenter] postNotificationName:@"ItemSel
-(IBAction)FavoriteButton:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"ItemSelected"
object:selectedObject];
}
[[NSNotificationCenter defaultCenter] addObserverForName:@"ItemSelected"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification* notif) {
[favoritedObjects release];
favoritedObjects = [[notif object] retain];
[self.tableView reloadData];
}];
//And populate the TableView with the objects:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [favoritedObjects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Favcell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSString *cellValue = [favoritedObjects valueForKey:@"Name"];
cell.textlabel.text = cellValue;
return cell;
}
到目前为止,用于捕获FavoritesViewController中的对象的代码:
-(IBAction)FavoriteButton:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"ItemSelected"
object:selectedObject];
}
[[NSNotificationCenter defaultCenter] addObserverForName:@"ItemSelected"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification* notif) {
[favoritedObjects release];
favoritedObjects = [[notif object] retain];
[self.tableView reloadData];
}];
//And populate the TableView with the objects:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [favoritedObjects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Favcell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSString *cellValue = [favoritedObjects valueForKey:@"Name"];
cell.textlabel.text = cellValue;
return cell;
}
这显示了20个单元格,其中的值来自我为其按下“添加到收藏夹”按钮的DetailViewController中最后一个对象的“名称”。例如,如果我在收藏夹中添加“Gato Negro”,它将在20个单元格中显示“Gato Negro”。如果我将“Luna”添加到fav中,它将在FavoritesViewController中的20个单元格中用“Luna”替换“Gato Negro”。。那么,如何在收藏夹表视图中逐个显示它们呢
我如何让NotificationCenter在应用程序关闭时保存更改,以便下次记住收藏夹
这似乎是某个地方的通信问题。您正在发布带有对象的通知
selectedObject
(我假设这是您的字典)。当您收到该通知时,您将使用此对象替换现有收藏夹:
favoritedObjects=[[notif object]retain]代码>
因此,事实上,favoritedObjects现在是单一selectedObject字典。这本字典可能有20个条目,所以你最终会有20个单元格
要使代码正常工作,您需要(至少)进行两次更改:
将上面的那一行更改为将通知对象添加到您的favoritedObjects数组中(我假设这是一个NSMutableArray
),而不是将favoritedObjects替换为selectedObject字典。您还需要创建favoritedObjects实例一次(当您第一次想要向其添加某些内容时,可以按需创建,例如,检查favoritedObjects==nil
,如果为nil,则创建一个新的NSMutableArray,或者在适当的位置创建它,例如viewDidLoad
(在某个时候,您可能也会保存和加载收藏夹,因此将其创建放到其中一个初始化方法中是一个好主意,即使您还没有准备好使用持久收藏夹)
在tableView:cellForRowAtIndexPath:
中,利用传递给您的indexath
从favoritedObjects数组中实际获取正确的条目
此外,如果没有可用的单元格出列,则您的tableView:cellForRowAtIndexPath:
缺少创建新单元格的代码。可能您在这里的问题中忽略了这段代码?如果没有,您需要添加该代码
用小写字母(-(iAction)favoriteButton:(id)sender
而不是-(iAction)favoriteButton:(id)sender
,或者更好的方法:-(iAction)favoriteButton pressed:(id)sender
),因为每个人都希望Objective-C方法是这样的:)非常感谢您提供的大量信息和建议,在我弄明白这一点之前,我似乎有很多阅读要做,但您帮了我很大的忙,因为现在我知道从哪里开始了。