繁体   English   中英

iOS 8 Swift:如何在表格视图的详细信息视图中显示项目的.txt文件?

[英]iOS 8 Swift: How I show a .txt File from the project on a Detail View from Table View?

朋友们! 我之前曾问过一个问题,在IOS项目中何处放置.txt文件并从中读取文件,并得到了一个很好的工作答案,我现在想将其扩展到我的一个小项目。

我创建了一个简单的TableView Controller,该控制器向我显示了几个名为“ Story1”,“ Story2”等的单元格。

故事本身应该从我的项目“ README1.txt”,“ README2.txt”中的.txt文件中读取。

我无法解决的问题是:“如何在DetailVC显示已分配的.txt文件?也许是因为我的“路径”字符串?

对于故事类型,我创建了一个仅包含名称和文件路径作为字符串的结构。

import Foundation
struct Story {
var name: String
var path: String? //tested as optional or not, no changes
}



 //Code of the Main `Table View Controller`:
 import UIKit

class MainTVC: UITableViewController {

var data = [Story]()

override func viewDidLoad() {
    // append Stories to Table
    let story1 = Story(name: "TestStory1", path: NSBundle.mainBundle().pathForResource("README1", ofType: "txt")!)
    let story2 = Story(name: "TestStory2", path: NSBundle.mainBundle().pathForResource("README2", ofType: "txt")!)

    data.append(story1)
    data.append(story2)

}
//Tells the data source to return the number of rows in a given section of a table view. 
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return data.count
}

//Asks the data source for a cell to insert in a particular location of the table view.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell

    cell.textLabel.text = "\(data[indexPath.row].name)"
    return cell
}
//Asks the delegate for the height to use for a row in a specified location.
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 75
}

//Notifies the view controller that a segue is about to be performed.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "detail" {
    let row = tableView.indexPathForCell(sender as UITableViewCell)?.row
    (segue.destinationViewController as DetailVC).data = data[row!]
    }
}
}

以及来自Detail View Controller的代码:

import UIKit

class DetailVC: UIViewController {

var data: Story?

@IBOutlet weak var storyTV: UITextView!

override func viewDidLoad() {

    //Show the name of the Story as title
    if let name = data?.name {
        title = name
    }
    else {
        title = "No Story"
    }

    //tried the following lines to get the Story shown
    let pathx = data?.path
    storyTV.text = String(contentsOfFile: data?.path!, encoding: NSUTF8StringEncoding, error: nil) //doesn't work and error occuring because of "data?.path!" just says add "!" to "path" and if i do, it says remove "!". Don't know why?

    storyTV.text = String(contentsOfFile: pathx!, encoding: NSUTF8StringEncoding, error: nil) // doesn't work to show, but don't know why
}
}

这是我通过几个println()自己发现的问题的答案。 我从类DetailVC中打印出了路径,该路径始终为空,或者开头的字符串为Optinal(“ ...)”。 因此,最终这只是对Optional的误解。

对于具有打开带有变量的文本文件的问题的读者,这里是DetailVC类的最后3行的更正代码:

if let path = data?.path {
        data?.path = path
        storyTV.text = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)
    }
    else {
        data?.path = "empty"
    } 

祝你好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM