ios - 使用(正确的)txt 文件填充详细 View

标签 ios objective-c tableview

我有一个带有国家名称的表格 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/

相关文章:

ios - UIStoryboard popViewController

ios - 在数组中每第 n 个数字添加一组数字 - iPhone

ios - 两个标签相邻,截断一个

ios - 在 CABasicAnimation 中控制速度

ios - com.alamofireJsonToObjects.error 代码 1

ios - NSUrl 在 NSString 中带有超链接?

ios - NSTextStorage 子类在某些情况下无法处理表情符号字符和更改字体

ios - 如何在 iOS 的 didselectrowatindexpath 中触发按钮操作方法, objective-c

css - Javafx TableView tabelmenubutton CSS 样式表

ios - 快速折叠一行上的 tableView 行选择