之后文本没有完全传递所以我构建了一个应用程序,它将消息加载到tableView单元格中。 现在我希望用户能够打开个别文章。 为了做到这一点,我使用prepareForSegue方法传递了选定的单元格,它可以工作,但部分工作。 它正确地传递了标题和图像,但部分显示了全文,准确地说它显示在单元格中。prepareForSegue(swift 3)
这里是我的新闻类的表:
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!
}
}
}
}
}
,这里是我的单篇文章的目的地视图控制器中,我传递的信息
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
}
}
这是什么情况
请参阅?即使服务器正在返回全文,也不会显示全文。 我做了测试,通过在另一个视图控制器中创建一个textView并从服务器获取文本,它工作正常。
问题看起来像它的复制单元格中的标签的布局和显示的是在该标签。 也尝试将另一个标签放入单元格以加载文本init,并且它正常工作,比单击单元格后显示的是该标签中的内容。
我想要的是在发生这种情况时加载全文。
据我了解您tableViewCell有一个固定的高度。但是你的文字太多了。见[这里](http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/)&[here](http://stackoverflow.com/questions/19211083/how-to-resize-uitableviewcell-to-fit-its-content) – Honey
@Adin Ljudina,你的意思是你传递了错误的数据吗? – aircraft
@Honey表格视图单元格的高度并不重要,重要的是应该在新的viewController中显示的文本,请检查此截图,您将看到我的文本变短。看看硬道理“SEDEF”它得到三个点,因为要显示更多的,和前某些原因,它是不 –