我有一个带有国家/地区名称的tableview。当点击一个单元格时,我希望它移动到包含该国家/地区文本的详细视图。我为每个国家/地区都有一个单独的txt文件,其名称与单元格中的标题相同(例如USA.txt)。
如何在详细视图中显示这些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];
答案 0 :(得分:1)
您需要再多一个UILabel
(比如详情标签)来显示.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