只要文本处于iOS中textView的特定位置,就会触发事件(tableview reload)

时间:2015-08-10 10:52:22

标签: ios swift scrollview

我刚开始使用iOS编程,但我无法找到任何答案。

我有一个Textview,包含一个分段的大文本。 每当每个段落到TextView的中间时,我想触发Tableview的重新加载。

我怎么能这样做?

这是我到目前为止编写的代码:

@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

我在考虑向paragraphNumber添加一些其他属性,例如here,但我无法找到如何做到这一点。

提前致谢,

林牧复合

1 个答案:

答案 0 :(得分:1)

目前尚不清楚你的段落是如何划分的(每个段落都在它自己的UITextView中,或者它们都是一个大的文本视图),但基本原理是你需要重新加载一个表格视图滚动视图到达某个滚动位置。

UITableView有一个方法reloadData,所以当滚动视图位于该位置时请调用此方法。

滚动滚动视图只意味着您正在修改其contentOffset,每次内容偏移更改时滚动视图都会向其发送delegate消息。使控制器成为滚动视图的委托,并实现scrollViewDidScroll(_:)委托方法。在这里,检查当前内容偏移量,如果它在您要查找的值,请重新加载表格视图:

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