アプリを終了してリロードすると、アレイがクリアされるか、正しくリロードされません。欠けているものが見えません。webview は urltextfield を読み込んで、オブジェクト pasturls を配列に追加します。ユーザーがテキストフィールドから既に webview に送信したものを入力し始めるたびに、テーブルビューに表示されます。アプリが強制終了されるまで、毎回うまく機能します。バックグラウンドリターンは問題ありません。お願いします?
ViewController.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
ViewController.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