想要了解在Tableview中添加字幕以获取txt文件列表的最佳方法吗?

时间:2018-05-02 04:45:26

标签: swift

我有一个可重排的txt文件列表,我在表视图中列出了他们的标题,我正在试图找出添加字幕的最佳方法。我想将标题作为其外语文本的名称,副标题是翻译后的名称。

目前,我列出了这样的标题:

stories = Bundle.main.urls(forResourcesWithExtension: "txt", subdirectory: nil)!

然后我像这样添加文件名和条带扩展名:

cell.textLabel?.text = stories[indexPath.row].deletingPathExtension().lastPathComponent

我想到的一种方法是将所有txt文件行放入字符串数组中,然后将字幕放在第1行的txt文件中,然后将其作为tableview中的副标题。但是我不知道如何在不读取txt的整个文件然后读取第1行的情况下如何做到这一点。有没有办法只读取所有文本块的第一行并将它们放在与之对应的字幕数组中标题?

我想的第二种方法是将原始语言和翻译标题添加到txt文件的文件名中,用“ - ”或符号分隔。有没有办法在连字符之前读取文本作为一个字符串,将文本作为第二个字符串读取?

还有另一种更好的方法吗?

已添加:使用此方法打开故事。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController {
            vc.selectedStory = stories[indexPath.row]
            navigationController?.pushViewController(vc, animated: true)
        }
    }

更新:一时兴起,我决定创建一个这样的词典:

var storyTitles:[String:String] = [
   "foreign lang title A":"english translation A",
   "foreign lang title B":"english translation B",
]

并添加了我所有的外语标题:翻译标题。

然后我将它插入到cell.detailText中,就这样工作了!

 let storyForeignTitle = stories[indexPath.row].deletingPathExtension().lastPathComponent
        cell.textLabel?.text = storyForeignTitle
        let storyEnglishTitle = storyTitles[storyForeignTitle]
        cell.detailTextLabel?.text = storyEnglishTitle

所以现在最后一个问题:我总共会讲50个故事。我想出的解决方案是否有任何缺点?我担心它不会起作用,因为外语不是拉丁文本,但在我的模拟器中似乎没问题。

1 个答案:

答案 0 :(得分:0)

使用每个故事的一个文本文件的方法,您可以用逗号分隔标题和翻译:

Title1,Translation1

创建故事结构:

struct Story {
    var title: String
    var translation: String
}

初始化空ArrayStory个对象

var stories = [Story]()

阅读文字文件:

if let textfileURLs = Bundle.main.urls(forResourcesWithExtension: "txt", subdirectory: nil) {

    for url in textfileURLs {

        do {
            let textFile = try String(contentsOf: url, encoding: .utf8)
            let data = textFile.components(separatedBy: ",")
            if data.count == 2 {
                let title = data[0], translation = data[1]
                stories.append(Story(title: title, translation: translation))
            }
        } catch {
            print("Error reading text file")
        }
    }
}

numberOfRowsInSection

return stories.count

然后在cellForRowAt indexPath中,您可以从每个故事对象中获取标题和副标题:

    cell.textLabel?.text = stories[indexPath.row].title
    cell.detailTextLabel?.text = stories[indexPath.row].translation

使用单个文本文件的替代方法

根据您的使用情况,将所有故事存储在一个文本文件中可能更有效:

Title1,Translation1
Title2,Translation2
Title3,Translation3

然后您可以解析这样的数据:

    if let textFileURL = Bundle.main.url(forResource: "StoryTitles", withExtension: "txt") {

        do {
            let textFile = try String(contentsOf: textFileURL, encoding: .utf8)
            let data = textFile.components(separatedBy: "\n")

            for row in data {
                let storyData = row.components(separatedBy: ",")
                if storyData.count == 2 {
                    let title = storyData[0], translation = storyData[1]
                    stories.append(Story(title: title, translation: translation))
                }
            }
        } catch {
            print("Error reading text file")
        }
    }