我有一个UITableView
,每行必须带我到一个包含UIWebView的详细页面。Tableview
的每个项目(行)都有一个本地HTML文件。
使用此代码,我支持相同的HTML文件:
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let localfilePath = NSBundle.mainBundle().URLForResource("yyy", withExtension: "html")
let myRequest = NSURLRequest(URL: localfilePath!)
webView.loadRequest(myRequest)
}
如何为每一行分配与之匹配的HTML文件?
答案 0 :(得分:0)
在此加载webview的viewcontroller中创建一个属性selectedHTMLFileName
。从具有tableview的先前控制器,处理didSelectRowAtIndexPath
并将此属性设置为您的文件名。
let localfilePath = NSBundle.mainBundle().URLForResource(self.selectedHTMLFileName, withExtension: "html")
修改强>
这样的事情可以奏效。
class IntroTableViewController: UITableViewController {
var labelintro = [String]()
var webAddresses = [String]()
int selectedIndex = 0;
override func viewDidLoad() {
super.viewDidLoad()
labelintro = ["Avant-propos", "Zone Sakalava", "Pronontiation", "Conjugaison", "Verbes indispensables","Grammaire & synthaxe","Mots indispensables","Les comptes","Le temps", "Termes de politesse", "La famille", "Se déplacer", "Le commerces", "Les vêtements", "Se loger", "Nourritures, épices & condiments", "Nature & paysages", "Ville, village", "L'école", "La flore", "La faune", "Santé & corps", "Artisanat, tradition & sacré", "Expressions courantes"]
webAddresses = ["xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html"]
}
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?) {
if segue.identifier == "gotodetail" {
var detailIntroVC = segue!.destinationViewController as DetailIntroViewController;
detailIntroVC.selectedHTMLFileName = webAddresses[selectedIndex];
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return labelintro.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("texteintrocell", forIndexPath: indexPath) as! IntroTableViewCell
let row = indexPath.row
cell.labelIntro.text = labelintro[row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedIndex = indexPath.row;
self.performSegueWithIdentifier("gotodetail", sender: self)
}
}
并在DetailViewController中:
class DetailIntroViewController: UIViewController {
string selectedHTMLFileName;
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Use filename without extension
var fileNameComponents = split(selectedHTMLFileName) {$0 == "."}
selectedHTMLFileName = fileNameComponents[0]
let localfilePath = NSBundle.mainBundle().URLForResource(selectedHTMLFileName, withExtension: "html")
}
}
此代码未经过测试,但您可以检查流程的流程。
所做的更改是:
prepareForSegue
已更新代码selectedIndex
didSelectRowAtIndexPath
活动selectedHTMLFileName
。答案 1 :(得分:0)
在我的TableViewController中:
class IntroTableViewController: UITableViewController {
var labelintro = [String]()
var webAddresses = [String]()
override func viewDidLoad() {
super.viewDidLoad()
labelintro = ["Avant-propos", "Zone Sakalava", "Pronontiation", "Conjugaison", "Verbes indispensables","Grammaire & synthaxe","Mots indispensables","Les comptes","Le temps", "Termes de politesse", "La famille", "Se déplacer", "Le commerces", "Les vêtements", "Se loger", "Nourritures, épices & condiments", "Nature & paysages", "Ville, village", "L'école", "La flore", "La faune", "Santé & corps", "Artisanat, tradition & sacré", "Expressions courantes"]
webAddresses = ["xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html","xxx.html", "yyy.html"]
}
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?) {
if segue.identifier == "gotodetail" {
_ = segue.destinationViewController
as! DetailIntroViewController
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return labelintro.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("texteintrocell", forIndexPath: indexPath) as! IntroTableViewCell
let row = indexPath.row
cell.labelIntro.text = labelintro[row]
return cell
}
}
在我的DetailViewController中:
class DetailIntroViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let localfilePath = NSBundle.mainBundle().URLForResource(self.selectedHTMLFileName, withExtension: "html")
}
}