gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:08:28 25 4
gpt4 key购买 nike

如何显示从 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()
}

}

最佳答案

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

 func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
println("end of the table")
}

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

关于swift - 如何从json在tableview中显示加载更多按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34665690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com