在prepareForSegue(swift 3)之后文本未完全通过

时间:2017-01-15 22:49:27

标签: ios json swift tableview

所以我构建了一个应用程序,将新闻加载到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!
            }
        }
    }
}
 }

这是我传递信息的单篇文章的目标视图控制器

   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
}
}

这就是发生的事情

http://imgur.com/2GQddeW

http://imgur.com/jos3VhE

请参阅?即使服务器返回全文,也不会显示全文。 我通过在另一个视图控制器中创建一个textView并从服务器获取文本来测试它,它运行正常。

问题似乎是在单元格中复制标签的布局并显示该标签中的内容。 还尝试将另一个标签放入单元格以加载文本init并且它正常工作,而不是在点击一个单元格后显示该标签中的内容。

我想要的是在发生这种情况时加载一个完整的文本。

1 个答案:

答案 0 :(得分:0)

nextScene.articleTitleString = selectedCells.title
nextScene.receiveFullText = selectedCells.title

您传递的标题是两次而不是全文......