【问题标题】:Load more options with UITableView使用 UITableView 加载更多选项
【发布时间】:2016-06-22 14:01:30
【问题描述】:

尝试使用分页将我的数据加载到页面中,我已经看到了很多示例,但都是在 Objective-C 中,有些问题没有答案。

我的代码:

class testTableViewController: UITableViewController {

    //MARK: - Properties -

    var allObjectArray: NSMutableArray = []
    var elements: NSMutableArray = []

    var currentPage = 0 //number of current page
    var nextpage = 0

    var selectedRow = Int()

    //MARK: - View Life Cycle -

    override func viewDidLoad() {
        super.viewDidLoad()

        for var i = 1; i < 500; i++ {
            allObjectArray.addObject(i)
        }
        elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(0, 30)))
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    // MARK: - Table view data source -

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return elements.count + 1
    }
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        selectedRow = (tableView.indexPathForSelectedRow?.row)!
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var customCell = tableView.dequeueReusableCellWithIdentifier("cell")
        customCell = UITableViewCell(style: .Default, reuseIdentifier: "cell")

        customCell!.textLabel!.text = "cell - \(allObjectArray[indexPath.row])"

        if indexPath.row == elements.count  {
            customCell?.textLabel?.textColor = UIColor.blueColor()
            customCell?.textLabel?.text = "Load more..."
        }
        return customCell!
    }
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        nextpage = elements.count

        if indexPath.row == nextpage {
            if indexPath.row == selectedRow {
                currentPage++
                nextpage = elements.count - 5
                elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(currentPage, 30)))
                tableView.reloadData()
            }
        }
    }
}

我想要这样的输出:

试图获取选定的索引,但它会返回 nil。

【问题讨论】:

标签: ios uitableview pagination swift2


【解决方案1】:

在 Interface Builder 中为 tableview 创建 Outlets 并制作两个动态原型单元格给它们标识符 制作一个带有按钮的单元格(您的加载更多单元格按钮) 然后使用该按钮创建包含加载更多单元格的逻辑的操作!!!

现在请参阅下面的片段以供参考...

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var tblDemo: UITableView!
var arForCells:NSMutableArray = NSMutableArray()
let simpleCellID = "SimpleCell"
let loadMoreCell = "LoadMoreCell"

override func viewDidLoad() {
    super.viewDidLoad()
    tblDemo.delegate = self
    tblDemo.dataSource = self
    arForCells  = NSMutableArray(objects: "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arForCells.count + 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if (indexPath.row == arForCells.count){
        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(loadMoreCell, forIndexPath: indexPath)
        return cell
    }else {
        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(simpleCellID, forIndexPath: indexPath)

        let lblCounter = cell.viewWithTag(111) as! UILabel
        lblCounter.text = arForCells.objectAtIndex(indexPath.row) as! String
        return cell
    }
}

@IBAction func loadMoreCells(sender: AnyObject) {
    let newAr:NSArray = NSArray(objects:  "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")

    arForCells.addObjectsFromArray(newAr as [AnyObject])
    tblDemo.reloadData()
}}

我已经检查过了,它应该会给你想要的结果。

您也可以在 TableViewFooter 中执行相同的操作。

【讨论】:

    【解决方案2】:

    要设置“加载更多..”文本,请在与 tableViewCell 大小相同的视图中添加带有此文本的标签。然后将其添加到您的 tableView 页脚中。在页脚视图中添加一个(自定义/透明)按钮,这样当触摸它时,它将加载您的主数组并包含更多数据,然后重新加载您的 tableView。

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      首先,你不必使用这个:

      selectedRow = (tableView.indexPathForSelectedRow?.row)!
      

      在 didSelectRowAtIndexPath 中。你可以简单地使用

      selectedRow = indexPath.row
      

      接下来,如果你想让单元格点击它,你的 willDisplayCellAtIndexPath 逻辑似乎是多余的。您可以简单地将以下内容放入enter code here:

      nextpage = elements.count
      if indexPath.row == nextpage {
           currentPage++
           nextpage = elements.count - 5
           elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(currentPage, 30)))
           tableView.reloadData()
      }
      

      另外,我不确定你为什么需要nextpage = elements.count - 5,但我认为你有这背后的原因。

      【讨论】:

        【解决方案4】:

        我自己做的

        import UIKit
        
        class LoadMoreTableVC: UITableViewController {
        
            //MARK: - Properties -
        
            var allObjectArray: NSMutableArray = []
            var elements: NSMutableArray = []
        
            var currentPage = 0 //number of current page
            var nextpage = 0
        
            var totalElements = 500 //total elements
            var elementAtOnePage = 30 //at one page
        
            //MARK: - View Life Cycle -
        
            override func viewDidLoad() {
                super.viewDidLoad()
        
                for i in 0 ..< totalElements {
                    allObjectArray.addObject(i+1)
                }
                elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(0, elementAtOnePage)))
            }
            override func didReceiveMemoryWarning() {
                super.didReceiveMemoryWarning()
            }
        
            // MARK: - Table view data source -
        
            override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
                return 1
            }
            override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
                let count = elements.count + 1
        
                if count < allObjectArray.count
                {
                    return count
                }
                else
                {
                    return allObjectArray.count
                }
            }
            override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
                if indexPath.row == elements.count {
                    loadDataDelayed()
                }
            }
            override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
                cell.textLabel!.text = "\(allObjectArray[indexPath.row])"
                if indexPath.row == elements.count  {
                    cell.textLabel?.text = "Load more..."
                }
                return cell
            }
            func loadDataDelayed(){
                currentPage += 1
                elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(currentPage, elementAtOnePage)))
                tableView.reloadData()
            }
        }
        

        【讨论】:

        • 所以,基本上我建议你这样做:)
        【解决方案5】:

        我已经在swift中完成了。

           func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
              if loading == true {
        
                var endScrolling:CGFloat = scrollView.contentOffset.y +   scrollView.frame.size.height
        
                if(endScrolling >= scrollView.contentSize.height){
        
                    NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "loadDataDelayed", userInfo: nil, repeats: false)
        
        
                }
        
            }
        
        }
        

        请试试这个代码。

        并分享您的回复。

        编码愉快:)

        【讨论】:

        • 请在 swift 中提供此代码.....我尝试在 swift 中进行转换,但第一行 self.loading 不起作用
        • 现在试着分享你的话:)
        • 这将在滚动时加载更多结果(当您到达底部时),并且 OP 希望为此设置单元格:)
        • @Shubham Narang 你的'loadDataDelayed'方法代码包含加载数据????但在我的代码中,我在 willDisplayCell 方法中重新加载
        • @GunjaPatel 您可以在 'loadDataDelayed' 方法中添加数组对象并重新加载表格。我认为这会对你有所帮助
        猜你喜欢
        • 2016-04-02
        • 1970-01-01
        • 2011-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-18
        • 2021-03-19
        相关资源
        最近更新 更多