qrコードをスキャンするプロジェクトを行っています。このプロジェクトには、ユーザーのスキャンの履歴を表示する必要がある履歴ページがあります。URLのみが含まれています。そこで、彼が以前にスキャンしたURLのリストをテーブルビューに表示することを計画しています。
履歴リストを保存するにはどうすればよいですか?助けてください。NSMutable配列などを使用して、スキャンしたURLを次のように保存できますか[myArray writeToURL:aURL atomically:NO];
qrコードをスキャンするプロジェクトを行っています。このプロジェクトには、ユーザーのスキャンの履歴を表示する必要がある履歴ページがあります。URLのみが含まれています。そこで、彼が以前にスキャンしたURLのリストをテーブルビューに表示することを計画しています。
履歴リストを保存するにはどうすればよいですか?助けてください。NSMutable配列などを使用して、スキャンしたURLを次のように保存できますか[myArray writeToURL:aURL atomically:NO];
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingString:@"/"];
NSString *fullPathToFile = [filePath stringByAppendingPathComponent:@"myfile.plist"];
[myArray writeToFile:fullPathToFile atomically:YES];
はい、できます。
テーブルビューは、テーブルビューのソースとしてNSMutableArrayを使用するためにUITableViewDataSourceを実装する必要があります:(ここでは、このサンプルコードの配列としてmyArrayを使用しています)
-(void)saveToHistoryArray:(NSString *)urlAsString {
[myArray addObject:urlAsString];
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
/* insert error checking here */
return [myArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
return cell;
}
はい、これにはsqliteまたはcoredataを使用して内部データベースを作成する必要があります。ユーザーがqrcodeストアをデータベースにスキャンし、ユーザーが履歴を確認したい場合は、データベースからフェッチして、表示に使用するメソッドを表示します。