0

アプリを終了してリロードすると、アレイがクリアされるか、正しくリロードされません。欠けているものが見えません。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
4

1 に答える 1

0

まず、過去の URL に問題があります。loadView では、次の 2 つの異なるコンテンツを設定します。

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

2 つ目は削除する必要があります。さらに、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 に答える