0

So I built an app which loads the news into the tableView cells. Now I want the user to be able to open individual article. To do that I passed the selected cells using prepareForSegue method and it works but partially. It passes the title and image properly but the full text is shown partially, to be precise it is shown as in the cells.

Here is my table of news class:

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

And here is my destination view controller for the single article in which I am passing the information

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

And this is what happens

http://imgur.com/2GQddeW

https://i.stack.imgur.com/dEr0e.jpg

See? The full text is not shown even though the server is returning full text. I did test this by creating a textView in another view controller and get the text from the server and it worked fine.

The issue looks like it's copying a layout of the label in the cell and displaying what is in that label. Also a tried putting another label in to the cell to load the text init and it worked properly, than after tapping a cell it displayed what was in that label.

What I want is to load a full text when this segue happens.

Adin Ljudina
  • 185
  • 2
  • 5
  • 15
  • As far as I understand your tableViewCell has a fixed height. But your text is too much for it. See [here](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) – mfaani Jan 15 '17 at 22:59
  • @Adin Ljudina, do you mean you passed the wrong data? – aircraft Jan 16 '17 at 00:34
  • @Honey The height of the table view cell is not important, the important thing is the text which should be displayed in a new viewController, please check this screenshot and you will see that my text gets shorter. Look at the last word "SEDEF" it gets three dots because there is more to be displayed, and fore some reason it is not – Adin Ljudina Jan 16 '17 at 10:54
  • Screenshot: http://imgur.com/Dxqp9qC – Adin Ljudina Jan 16 '17 at 10:55

1 Answers1

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

You are passing the title twice instead of the full text...

Tiago Lira
  • 2,543
  • 1
  • 18
  • 17
  • I corrected it to nextScene.receiveFullText = selectedCells.text but still my text is not fully displayed. Please check this screenshot that I have just made http://imgur.com/Dxqp9qC – Adin Ljudina Jan 16 '17 at 10:50
  • In that case, it seems that the UITextView on the second controller is not big enough. Go to interface builder and make sure it fills all the yellow area (you may need to add constraints). You can also paint the UITextView background to confirm if its size is correct. – Tiago Lira Jan 16 '17 at 11:36
  • It is fully stretched, so it takes the whole portion of the screen, it is not the issue. – Adin Ljudina Jan 16 '17 at 15:38