【问题标题】:Text not fully passed after prepareForSegue (swift 3)prepareForSegue 后文本未完全传递(swift 3)
【发布时间】:2017-01-15 22:49:26
【问题描述】:

所以我构建了一个将新闻加载到 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 并且它工作正常,而不是在点击一个单元格后它显示该标签中的内容。

我想要的是在发生这种情况时加载全文。

【问题讨论】:

  • 据我了解,您的 tableViewCell 具有固定高度。但是你的文字太多了。见here & here
  • @Adin Ljudina,你的意思是你传递了错误的数据吗?
  • @Honey 表格视图单元格的高度并不重要,重要的是应该在新的 viewController 中显示的文本,请查看此屏幕截图,您会看到我的文本变短了。看看最后一个单词“SEDEF”它得到三个点,因为还有更多要显示,但由于某种原因它不是

标签: ios json swift tableview


【解决方案1】:
nextScene.articleTitleString = selectedCells.title
nextScene.receiveFullText = selectedCells.title

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

【讨论】:

  • 我将其更正为 nextScene.receiveFullText = selectedCells.text 但我的文本仍然没有完全显示。请检查我刚刚制作的这个截图imgur.com/Dxqp9qC
  • 这样的话,好像是第二个控制器上的UITextView不够大。转到界面生成器并确保它填充了所有黄色区域(您可能需要添加约束)。您还可以绘制 UITextView 背景以确认其大小是否正确。
  • 它是完全拉伸的,所以它占据了整个屏幕,这不是问题。
猜你喜欢
  • 1970-01-01
  • 2015-09-18
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 2017-02-06
相关资源
最近更新 更多