使用警告按钮删除或清除UITableView中的所有数据对象?

时间:2012-04-09 16:00:12

标签: xcode uitableview nsarray nsobject

好的,我真的已经搜索了似乎涵盖这个的主题,但仍然找不到适合我的主题。此列表是通过在此处发送每个UIWebView页面加载来完成的。在某些时候,如果用户想要清除此列表,我有一个按钮,它会显示一个警报视图,确认他们想要清除,然后按OK清除。请告诉我如何做到这一点。大约一半的时候你可以找到我的clearAllData,它是clearAllRecents警告按钮但是我的空白函数是我的吗?

#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

1 个答案:

答案 0 :(得分:3)

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

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

应该这样做。