如何在json中显示在tableview中加载更多按钮

时间:2016-01-07 21:41:44

标签: swift uitableview

如何在json

的tableview中显示更多内容

我希望在向下滚动后获取20行,然后再获取另外20行,但是当你滚动并且没有获取新行时,我的三元组重复了这段代码的错误

   var currentPage = 0
var nextPage = 0

var allData: NSMutableArray = []
var data: NSMutableArray = []

private var loading = false {
    didSet {
        tableViewFooter.hidden = !loading
    }
}

override func scrollViewDidScroll(scrollView: UIScrollView) {
    let currentOffset = scrollView.contentOffset.y
    let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height

    if (maximumOffset - currentOffset) <= 40 {
    }
}


override func viewDidLoad() {
    super.viewDidLoad()

    _ = NSTimer.scheduledTimerWithTimeInterval(0.0001, target: self, selector: Selector("sayHello"), userInfo: nil, repeats: false)

    tableViewFooter.hidden = true
}

  func dataJsonFromURL(url:String)->NSArray
{
    if let data = NSData(contentsOfURL: NSURL(string: url)!) {
        return ((try! NSJSONSerialization.JSONObjectWithData(data, options: [])) as! NSArray)
    }
    else{
        return data
    }

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count //items.count  //data.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)


    let mindata = (data[indexPath.row] as! NSDictionary)


   //let item = items[indexPath.row]

   // cell.textLabel?.text = mindata["title_topic"] as? String

// cell.textLabel?.text = item.name         cell.textLabel?.text = String(mindata [“title_topic”]!)

    return cell
}

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    nextPage = data.count - 5
    if indexPath.row == nextPage {
        currentPage += 1
        nextPage = data.count - 5
        data.addObjectsFromArray(allData.subarrayWithRange(NSMakeRange(currentPage, 20)))
        tableView.reloadData()
    }
}

func sayHello()
{
    if let tempData = dataJsonFromURL("http://www.arabfono.com/show22.php") as? NSMutableArray {

        allData = tempData
    }

    data.addObjectsFromArray(allData.subarrayWithRange(NSMakeRange(0, 20)))

    self.tableView.reloadData()
}

}

1 个答案:

答案 0 :(得分:1)

我在项目早期遇到的同样问题。 试试这个,

http://toilatester.com/category/?xv=

在tableview的末尾,您可以重新加载tableView