我有一个带有国家名称的表格 View 。当点击一个单元格时,我希望它移动到包含有关国家/地区的文本的详细 View 。我为每个国家/地区创建了一个单独的 txt 文件,其名称与单元格中的标题相同(例如 USA.txt)。
如何在详细 View 中显示这些 txt 文件?
到目前为止,我的代码如下所示:
TableViewController.h
@property (nonatomic, strong) NSArray *Title;
TableViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
_Title = @[@”Afganistan”,
@”Albania”,
@”Algeria”,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
int row = [indexPath row];
cell.TitleLabel.text = _Title[row];
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
DetailViewController *detailviewcontroller = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
int row = [myIndexPath row];
detailviewcontroller.DetailModal = @[_Title[row]];
}
}
DetailViewController.h
@property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
@property (strong, nonatomic) NSArray *DetailModal;
DetailViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_TitleLabel.text = _DetailModal[0];
self.navigationItem.title = _DetailModal[0];
}
我在 StackOverflow 上找到了下面的代码,但我不知道如何实现它。
NSString *path = [[NSBundle mainBundle]pathForResource:@”Afganistan” ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
最佳答案
你需要再取一个UILabel
(比如detailLabel)来显示.txt
文件的内容。
在 DetailViewController
的 ViewDidLoad 中,添加以下行
NSString *path = [[NSBundle mainBundle]pathForResource:_DetailModal[0] ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
self.detailLabel.text = content
self.detailLabel.numberOflines = 0
关于ios - 使用(正确的)txt 文件填充详细 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40359156/