2012-04-09 62 views
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 

回答

3
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  { 

if (buttonIndex == 1) { 
    [recent removeAllObjects]; 
    [[NSUserDefaults standardUserDefaults] setObject:recent forKey:@"Recent"]; 
    [yourTableView reloadData]; 
}} 

应该这样做。

+0

我试过这个,我得到的问题是[yourTableView reloadData];粗糙的yourTableView不适合我。我试过UITableView和reloadData不知道。只是tableView抛出错误,并建议我使用UITableView。 – user1264599 2012-04-09 22:21:54

+0

我没有提到这是UIViewController中的一个可用视图。所以,我通过添加@property(nonatomic,retain)来修复未知方法IBOutlet UITableView * myTableView;到我的.h并连接到IB。所以,现在表格清除了,但只要我返回到tableview,它就会再次填充所有数据。我错过了什么? – user1264599 2012-04-10 05:01:54

+0

你的问题是' - (void)viewDidLoad {0} = {[[[[NSUserDefaults standardUserDefaults] arrayForKey:@“Recent”] mutableCopy]; [super viewDidLoad]; }' 每次加载视图时,'recent'都会被赋予用户默认的值。如果您想永久清除这些值,则需要重置'arrayForKey:@“最近的”'“。否则,您将需要找到另一种方法来加载值 - 可能是通过给用户一个选项来重新加载最近值。 (查看编辑答案) – AMayes 2012-04-10 13:49:49