所以我构建了一个应用程序,将新闻加载到 tableView 单元格中。 现在我希望用户能够打开单独的文章。 为此,我使用prepareForSegue方法传递了选定的单元格,它可以部分工作。 它正确地传递了标题和图像,但全文显示部分,准确地说,它显示在单元格中。
这是我的新闻课表:
import Alamofire //Framework for handling http requests
import UIKit
import AlamofireImage
class NewsTableViewController: UITableViewController {
//Custom struct for the data
struct News {
let title : String
let text : String
let link : String
let imgUrl : String
init(dictionary: [String:String]) {
self.title = dictionary["title"] ?? ""
self.text = dictionary["text"] ?? ""
self.link = dictionary["link"] ?? ""
self.imgUrl = dictionary["imgUri"] ?? ""
}
}
//Array which holds the news
var newsData = [News]()
// Download the news
func downloadData() {
Alamofire.request("https://api.sis.kemoke.net/news").responseJSON { response in
print(response.request as Any) // original URL request
print(response.response as Any) // HTTP URL response
print(response.data as Any) // server data
print(response.result) // result of response serialization
//Optional binding to handle exceptions
self.newsData.removeAll() // clean the data source array
if let json = response.result.value as? [[String:String]] {
for news in json {
self.newsData.append(News(dictionary: news))
}
self.tableView.reloadData()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
downloadData()
tableView.rowHeight = 100
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return newsData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? newsCellTableViewCell
let news = newsData[indexPath.row]
cell?.headline.text = news.title
Alamofire.request(news.imgUrl).responseImage { response in
debugPrint(response)
print(response.request as Any)
print(response.response as Any)
debugPrint(response.result)
let cellImage = response.result.value
if let image = response.result.value {
print("image downloaded: \(image)")
}
cell?.thumbnailImage.image = cellImage
}
print(news.imgUrl)
return cell!
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showArticle" {
let nextScene = segue.destination as! ArticleViewController
// Pass the selected object to the new view controller.
if let indexPath = self.tableView.indexPathForSelectedRow {
let selectedCells = newsData[indexPath.row]
nextScene.articleTitleString = selectedCells.title
nextScene.receiveFullText = selectedCells.title
//Downloading an image to be displayed in a single article
Alamofire.request(selectedCells.imgUrl).responseImage { response in
debugPrint(response)
print(response.request as Any)
print(response.response as Any)
debugPrint(response.result)
let cellImage = response.result.value
nextScene.articleImage.image = cellImage!
}
}
}
}
}
这是我在其中传递信息的单篇文章的目标 View Controller
class ArticleViewController: UIViewController {
@IBOutlet weak var articleTitle: UILabel!
var articleTitleString = ""
@IBOutlet weak var articleImage: UIImageView!
@IBOutlet weak var fullText: UITextView!
var receiveFullText = ""
override func viewWillAppear(_ animated: Bool) {
articleTitle.text = articleTitleString
fullText.text = receiveFullText
}
}
这就是发生的事情
看到了吗?即使服务器返回全文,也不会显示全文。 我通过在另一个 View Controller 中创建一个 textView 并从服务器获取文本来测试这一点,它工作得很好。
问题看起来像是复制单元格中标签的布局并显示该标签中的内容。 还尝试将另一个标签放入单元格中以加载文本初始化,它工作正常,而不是点击单元格后显示该标签中的内容。
我想要的是在发生此转场时加载全文。
最佳答案
nextScene.articleTitleString = selectedCells.title
nextScene.receiveFullText = selectedCells.title
您传递了两次标题而不是全文...
关于ios - 在prepareForSegue之后文本未完全通过(swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41667206/