app*_*eak 0 iphone runtime objective-c uitableview ios
我为我的iPhone应用程序实现了"添加到收藏夹"功能.它工作正常,除了在运行时将单元格添加到我的收藏表视图中.例如,我给出了以下方法.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
tableView.hidden = YES;
warningLabel.hidden = YES;
// Load any change in favourites
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:kFavouriteItemsKey];
self.favourites = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if([self.favourites count] > 0)
tableView.hidden = NO;
else
warningLabel.hidden = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.favourites count]; // Favorites is a dictionary contains required data
}
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:@"index: %d",indexPath.row];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常,只是第一次正确显示行!加载tableview后如果我在收藏夹中添加新项目或删除任何项目,它对我的tableview没有任何影响!我想准确显示收藏夹词典中可用的内容.似乎再次ViewAppear时不会调用CellForRowAtIndexPath.我可以使用TableView的任何方法来实现我的要求吗?