-2

I just started with iOS programming and I could not find anywhere an answer about this.

I have a Textview, containing a large text divided in paragraphs. I would like to trigger the reload of a Tableview whenever each paragraph reaches the middle of the TextView.

How could I do it?

Here is the code I wrote so far:

@IBOutlet weak var testo: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()

    if databaseDB.open() {

        // Here I definethe string attributes
        var style = NSMutableParagraphStyle()
        style.lineSpacing = 15
        let font = UIFont(name: "Georgia", size: 18.0) ?? UIFont.systemFontOfSize(18.0)
        let textFont = [NSFontAttributeName:font, NSParagraphStyleAttributeName : style]
        let fontSuper = UIFont(name: "Georgia-Italic", size: 13.0) ?? UIFont.systemFontOfSize(13.0)
        let superFont = [NSFontAttributeName:fontSuper, NSBaselineOffsetAttributeName:6]

        // and here the variables
        let mainText = NSMutableAttributedString()
        var paragraphId : String = ""

        // I load the text from a table in the database
        let querySQL = "SELECT id, versetext FROM table1"
        let results:FMResultSet? = databaseDB.executeQuery(querySQL, withArgumentsInArray: nil)

        while results!.next(){

            paragraphId = results!.stringForColumn("id")

            // I add some other information from another table (here goes also other code, but I have skipped it for simplicity)
            let queryDescrSQL = "SELECT paragraph FROM table2 WHERE id = \(paragraphId)"
             let paragraphResults:FMResultSet? = databaseDB.executeQuery(queryDescrSQL, withArgumentsInArray: nil)

            if paragraphResults?.next() == true {
            // here I add the attributes to the text to be shown
                let numberParagraph = paragraphResults!.stringForColumn("paragraph")

                let paragraphNumber = NSAttributedString(string: numberParagraph, attributes:superFont)
                mainText.appendAttributedString(paragraphNumber)

                let textParagraph = results!.stringForColumn("versetext")+" "
                let paragraphText = NSAttributedString(string: textParagraph, attributes:textFont)
                mainText.appendAttributedString(paragraphText)
            }
        }
        databaseDB.close()
        // and finally everything is loaded on the TextView
        testo.attributedText = mainText
    }

}
// I'm letting out the code for the TableView, as it is probably not relevant for the matter

I was thinking of adding some other attribute to paragraphNumber, something like here, but I haven't been able to find how to do it.

Thanks in advance,

Silvo

Community
  • 1
  • 1
silvoc
  • 15
  • 1
  • 7

1 Answers1

1

It's not clear how your paragraphs are divided up (is each in its own UITextView, or do they all form one large text view), but the basics of this are that you need to reload a table view when the scroll view reaches a certain scrolled position.

UITableView has a method reloadData, so call this when your scroll view is at the position.

Scrolling a scroll view just means you are modifying its contentOffset, and the scroll view sends its delegate a message every time the content offset changes. Make your controller the delegate of the scroll view, and implement the scrollViewDidScroll(_:) delegate method. In here, check the current content offset, and if it's at the value you're looking for, reload the table view:

class MyViewController: UIViewController, UIScrollViewDelegate {

    // var scrollView = ...
    // var tableView = ...

    override viewDidLoad() {
        super.viewDidLoad()
        scrollView.delegate = self
    }

    func scrollViewDidScroll(scrollView: UIScrollView) {
        // var scrollThreshold = (calculate some scroll threshold based on your paragraph text)
        if scrollView.contentOffset.y > scrollThreshold {
            tableView.reloadData()
        }
    }
}
Stuart
  • 36,683
  • 19
  • 101
  • 139
  • In order to do this, shouldn't I know in advance the size of each paragraph (which could be quite difficult with a large text)? What if I wanted to reload the tableview each time a new paragraph moves to a certain position? I wasn't thinking of loading each paragraph into its own UITextView, should I calculate the size/position of each paragraph in advance? – silvoc Aug 10 '15 at 13:42
  • What have you tried? The way you decide to calculate your paragraph size is up to you, and depends on how you're app/views are structured and what you're trying to achieve. String drawing (and hence bounds checking) _can_ be expensive depending how much/frequently you're changing them, but don't optimise prematurely. – Stuart Aug 10 '15 at 14:39
  • I'll start from the beginning (as a beginner...). I start with an SQLite database, and I want to have a pane in which there is the context Actually, I have first loaded my text on a Tableview (from an SQLite database), with each cell linked to – silvoc Aug 10 '15 at 14:58
  • Sorry, I keep hitting the return key to move to a new line, and the comment is inserted.... – silvoc Aug 10 '15 at 15:04
  • Can I suggest you append such details to your question - extended chats in comments are discouraged, as they are hard to follow and don't help future readers. – Stuart Aug 10 '15 at 15:07
  • I'll start from the beginning (as a beginner...). I start with an SQLite database, and I want to have a pane in which there is the content of one table and another pane showing the content from a different table referenced by a specific row in the first table. I initially did everything using two flanking Tableviews, in which each cell in the left pane would trigger the refresh of the right pane. But, I thought that loading the main text from Table1 in a ScrollView would make it look it better (basically, I could present in a book-like manner). – silvoc Aug 10 '15 at 15:13
  • Thanks for the advice, I will add the code and everything. How can I append it to the original question now that it is already posted?? – silvoc Aug 10 '15 at 15:15