xcode 使用警报按钮删除或清除 UITableView 中的所有数据对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10075949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete or clear ALL data objects in UITableView with alert button?
提问by user1264599
OK, I really have searched through the topics that seem to cover this but still have not found one that works for me. This list is made by sending every UIWebView page load here. At some point if the user would like to clear this list I have a button that brings up an alert view that just confirms that they want to clear and then press OK to clear. Please, tell me how I can do this. About halfway down you can find my clearAllData which is clearAllRecents alert button but my void function underneath that is my ?
好的,我确实搜索了似乎涵盖此内容的主题,但仍然没有找到适合我的主题。这个列表是通过在此处发送每个 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
回答by AMayes
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[recent removeAllObjects];
[[NSUserDefaults standardUserDefaults] setObject:recent forKey:@"Recent"];
[yourTableView reloadData];
}}
That should do it.
那应该这样做。