【问题标题】:Delete or clear ALL data objects in UITableView with alert button?使用警报按钮删除或清除 UITableView 中的所有数据对象?
【发布时间】:2012-04-09 16:00:12
【问题描述】:

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

#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

【问题讨论】:

    标签: xcode uitableview nsarray nsobject


    【解决方案1】:
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex      {
    
    if (buttonIndex == 1) {
        [recent removeAllObjects];
        [[NSUserDefaults standardUserDefaults] setObject:recent forKey:@"Recent"];
        [yourTableView reloadData];
    }}
    

    应该可以的。

    【讨论】:

    • 我试过这个,我得到的问题是 [yourTableView reloadData];粗略的 yourTableView 不适合我。我试过 UITableView 并且 reloadData 是未知的。只是 tableView 抛出错误并建议我使用 UITableView。
    • 我没有提到这是 UIViewController 中的 uitableview。因此,我通过添加 @property (nonatomic, retain) IBOutlet UITableView *myTableView; 来修复未知方法到我的 .h 并在 IB 中连接它。因此,现在表格已清除,但一旦我返回表格视图,它就会再次填充所有数据。我错过了什么?
    • 您的问题是- (void)viewDidLoad { recent = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"Recent"] mutableCopy]; [super viewDidLoad]; } 每次加载视图时,都会为recent 分配用户默认值。如果要永久清除这些值,则需要重置 arrayForKey:@"Recent"。否则,您将需要找到另一种加载值的方法——也许通过为用户提供重新加载最近的选项。 (见编辑答案)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 2016-07-15
    • 2021-12-30
    相关资源
    最近更新 更多