0

退出我的应用程序并重新加载似乎清除了我的数组,或者我没有正确地重新加载它。我看不到我错过了什么。webview 加载并将 urltextfield 添加到对象 Pasturls 到数组中。每次用户开始输入他们已经从文本字段发送到 webiew 的内容时,它都会显示在表格视图中。每次都很好,直到应用程序被杀死。后台返回没问题。请?

视图控制器.h

@interface ViewController : UIViewController <UITextFieldDelegate, UIWebViewDelegate, UIActionSheetDelegate, MFMailComposeViewControllerDelegate, UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *pastUrls;
NSMutableArray *autocompleteUrls;
UITableView *autocompleteTableView;

}
@property (nonatomic, retain) IBOutlet UITableView *autocompleteTableView;
@property (nonatomic, retain) NSMutableArray *pastUrls;
@property (nonatomic, retain) NSMutableArray *autocompleteUrls;

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring;

@end

视图控制器.m

@interface ViewController ()

@end

@implementation ViewController   

@synthesize pastUrls;
@synthesize autocompleteUrls;
@synthesize autocompleteTableView;


- (void)webViewDidStartLoad:(UIWebView *)webView {

if (![pastUrls containsObject:urlField.text]) {
    [pastUrls addObject:urlField.text];
    [[NSUserDefaults standardUserDefaults] setObject:pastUrls forKey:@"PastUrls"];
    }

- (void)loadView {
pastUrls = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"PastUrls"] mutableCopy];
pastUrls = [[NSMutableArray alloc] initWithObjects:@"PastUrls", nil];
[super loadView];
}

- (void)viewDidLoad
{
autocompleteUrls = [[NSMutableArray alloc] init];
}

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {

[autocompleteUrls removeAllObjects];
for(NSString *curString in pastUrls) {
    NSRange substringRange = [curString rangeOfString:substring];
    if (substringRange.location == 0) {
        [autocompleteUrls addObject:curString];  
    }
}
[autocompleteTableView reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section      {
return [autocompleteUrls count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyle)UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

}
@end
4

1 回答 1

0

首先,您的过去网址存在问题。在 loadView 中,您在其中设置了两个不同的内容:

- (void)loadView {
pastUrls = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"PastUrls"] mutableCopy];
pastUrls = [[NSMutableArray alloc] initWithObjects:@"PastUrls", nil];
}

第二个应该被删除。此外,您的实现中还有一些奇怪的东西,例如 loadView 和 viewDidLoad 函数。您更有可能拥有:

- (void)webViewDidStartLoad:(UIWebView *)webView {

if (![pastUrls containsObject:urlField.text]) {
    [pastUrls addObject:urlField.text];
    [[NSUserDefaults standardUserDefaults] setObject:pastUrls forKey:@"PastUrls"];
    // You might need to synchronize your NSUserDefaults content
    // if so, uncomment the following line
    // [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

// Used only if you do not use a XIB file to load the graphical interface
//- (void)loadView {
//}

- (void)viewDidLoad
{
    [super viewDidLoad]; // Was missing in your current implementation
    autocompleteUrls = [[NSMutableArray alloc] init];
    pastUrls = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"PastUrls"] mutableCopy];
}
于 2012-04-24T14:47:00.513 回答