1
好吧,我真的已經搜遍了似乎覆蓋這個主題,但仍然沒有找到一個適合我的主題。這個列表是通過在這裏發送每個UIWebView頁面加載來完成的。在某些時候,如果用戶想要清除這個列表,我有一個按鈕,顯示一個警報視圖,確認他們想要清除,然後按確定清除。請告訴我我該如何做到這一點。大約一半你可以找到我clearAllData這是clearAllRecents警報按鈕,但我下面的void函數是我的?使用提醒按鈕刪除或清除UITableView中的所有數據對象?
#import "RecentViewController.h"
#import "ViewController.h"
@interface RecentViewController()
@end
@implementation RecentViewController
@synthesize recent, explorerView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
recent = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"Recent"] mutableCopy];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (IBAction)cancelButtonTapped
{
[self setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self.presentingViewController dismissModalViewControllerAnimated:true];
}
- (IBAction)clearAllRecents
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"clear all recents?"
message:@"press ok to clear"
delegate: self
cancelButtonTitle:@"cancel"
otherButtonTitles:@"ok", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
//clear all recent objects??????????????????????????????????????????????
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [recent count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyle)UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [recent objectAtIndex:(recent.count - indexPath.row - 1)];
cell.textLabel.textColor = [UIColor whiteColor];;
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
explorerView.urlField.text = [recent objectAtIndex:(recent.count - indexPath.row - 1)];
[explorerView textFieldShouldReturn:explorerView.urlField];
[self.presentingViewController dismissModalViewControllerAnimated:true];
explorerView = nil;
recent = nil;
tableView = nil;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[recent removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[[NSUserDefaults standardUserDefaults] setObject:recent forKey:@"Recent"];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
我試過這個,我得到的問題是[yourTableView reloadData];粗糙的yourTableView不適合我。我試過UITableView和reloadData不知道。只是tableView拋出錯誤,並建議我使用UITableView。 – user1264599 2012-04-09 22:21:54
我沒有提到這是UIViewController中的一個可用視圖。所以,我通過添加@property(nonatomic,retain)來修復未知方法IBOutlet UITableView * myTableView;到我的.h並連接到IB。所以,現在表格清除了,但只要我返回到tableview,它就會再次填充所有數據。我錯過了什麼? – user1264599 2012-04-10 05:01:54
你的問題是' - (void)viewDidLoad {0} = {[[[[NSUserDefaults standardUserDefaults] arrayForKey:@「Recent」] mutableCopy]; [super viewDidLoad]; }' 每次加載視圖時,'recent'都會被賦予用戶默認的值。如果您想永久清除這些值,則需要重置'arrayForKey:@「最近的」'「。否則,您將需要找到另一種方法來加載值 - 可能是通過給用戶一個選項來重新加載最近值。 (查看編輯答案) – AMayes 2012-04-10 13:49:49