退出我的应用并重新加载似乎清除了我的数组,或者我没有正确地重新加载它。我看不出我错过了什么。 webview加载并将urltextfield添加到对象pasturls到数组中。每次用户开始输入他们已从文本字段发送到webiew的内容时,它都会显示在tableview中。每次都很棒,直到应用程序被杀死。背景返回很好。请?
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
答案 0 :(得分:0)
首先,您的pastUrls存在问题。在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];
}