我正在iOS中創建一個基於練習的應用程序。我想包括的是我的最愛列表。就目前來看,我有一個練習庫,您可以點擊一個按鈕並將其添加到通過導航控制器訪問的收藏夾列表中。我能夠正確地將標題傳遞給商店,然後將商店放在收藏夾表格視圖中。我可以根據表格視圖單元格的標題加載特定的exerciseViewController
並在運行之間保存。不過,我嘗試刪除練習時遇到問題。我一直在掙扎幾個小時。如何讓UITableView正確地重新加載?
這是情景:
我已經添加了兩個練習我的最愛:exercise A
這是第一排,exercise B
這是第二個。但是,當訪問收藏夾列表時,我選擇exercise A
來查看其詳細信息,並決定不公開它,然後導航(返回)至收藏夾列表,但未正確更新。雖然它正確加載行數(現在爲1),但它仍顯示exercise A
標題(它應顯示exercise B
標題)。但是,當我一直導航到主頁並選擇收藏夾時,它會正確顯示 - 現在只顯示exercise B
標題。我曾嘗試致電[[self tableview] reload]
viewWillAppear
(這似乎是最常見的答案),編寫通知,協議等,並且仍然存在此問題。唯一能正常工作的時間是通過主頁訪問收藏夾列表並調用viewDidLoad
。
如何在表格顯示時正確顯示錶格而不必加載?
收藏泰伯維
FavsVC.m
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
NSLog(@"APPEARING");
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
NSArray *fav = [[NSUserDefaults standardUserDefaults] objectForKey:@"Favorites"];
NSString *name = [fav objectAtIndex:indexPath.row];
cell.textLabel.text = name;
}
return cell;
}
//This the method is in my store, and gets called when I click on an exercise to add or delete.
- (id)addToFavorites:(NSString *)title
{
favoriteTitles = [[NSUserDefaults standardUserDefaults] objectForKey:@"Favorites"];
if ([favoriteTitles containsObject:title]) {
NSLog(@"exercise removed");
//[favoriteTitles removeObject:title];
NSMutableArray *newArray = [NSMutableArray arrayWithArray:favoriteTitles];
[newArray removeObject:title];
[[NSUserDefaults standardUserDefaults] setObject:newArray forKey:@"Favorites"]; //newArray
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
NSLog(@"exercise added");
//[favoriteTitles addObject:title];
NSMutableArray *newArray = [NSMutableArray arrayWithArray:favoriteTitles];
[newArray addObject:title];
[[NSUserDefaults standardUserDefaults] setObject:newArray forKey:@"Favorites"]; //newArray
[[NSUserDefaults standardUserDefaults] synchronize];
}
return self;
}
感謝您的幫助,這解決了我的問題。 – BradRS
高興,但隨時把它作爲正確的問題。 – Martol1ni