2012-04-09 2 views
1

좋아요,이 주제를 다뤄 봤지만 아직도 저에게 맞는 작품을 찾지 못했습니다. 이 목록은 모든 UIWebView 페이지를로드하여 전송합니다. 어떤 시점에서 사용자가이 목록을 지우려면 알림보기를 표시하는 버튼이 있습니다. 버튼을 클릭하면 선택을 취소하고 확인을 눌러 선택을 취소합니다. 내가 어떻게이 일을 할 수 있는지 말해줘. 중도에 관해서는 clearAllData를 찾을 수 있습니다 clearAllRecents 경고 단추가 내 밑의 함수는 내 것입니까? 그것을해야경고 단추로 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]이다. 너의 거친 테이블 뷰는 나에게 적합하지 않다. 내가 UITableView 시도하고 reloadData 알 수 없습니다. 그냥 tableView 오류를 throw하고 내가 UITableView 사용할 제안합니다. – user1264599

+0

이것이 UIViewController의 uitableview라는 것을 언급하지 못했습니다. 그래서, @property (nonatomic, retain)를 추가하여 알려지지 않은 메소드를 수정했습니다. IBOutlet UITableView * myTableView; 내 .h에 연결하고 IB로 연결하십시오. 이제 테이블이 지워졌지만 테이블 뷰로 돌아 가면 모든 데이터가 다시 채워집니다. 내가 뭘 놓치고 있니? – user1264599

+0

문제는 다음과 같습니다. - (void) viewDidLoad { recent = [[[NSUserDefaults standardUserDefaults] arrayForKey : @ "Recent"] mutableCopy]; [super viewDidLoad]; }' 보기가로드 될 때마다 '최근'에 사용자 기본값의 값이 할당됩니다. 값을 영구적으로 지우려면'arrayForKey : @ "Recent"를 재설정해야합니다. 그렇지 않으면 사용자에게 recents를 다시로드 할 수있는 옵션을 제공하여 값을로드하는 다른 방법을 찾아야합니다. (편집 된 답변보기) – AMayes

관련 문제