我正在做一个扫描二维码的项目。在这个项目中,存在一个历史页面,我必须在其中显示用户扫描的历史记录。它仅包含 URL。所以我打算在表格视图中显示他之前扫描的 url 列表。
如何保存历史列表?请帮忙。我可以使用 NSMutable 数组或其他东西来保存扫描的 url 像这样[myArray writeToURL:aURL atomically:NO];
我正在做一个扫描二维码的项目。在这个项目中,存在一个历史页面,我必须在其中显示用户扫描的历史记录。它仅包含 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];
是的,您必须使用 sqlite 或 coredata 创建一个内部数据库,无论用户扫描 qrcode 存储到数据库中还是用户想要查看历史记录的任何位置,然后从数据库中获取并显示您用来显示的任何方法
是的你可以。
您的表视图需要实现 UITableViewDataSource 以使用 NSMutableArray 作为表视图的源:(此处使用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;
}