退出我的应用程序并重新加载似乎清除了我的数组,或者我没有正确地重新加载它。我看不到我错过了什么。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