Swift:cellForRowAtIndexPath 未按 indexPath 的顺序调用

Swift: cellForRowAtIndexPath not called in the order of indexPaths

提问人:psijic 提问时间:9/8/2014 最后编辑:Nirav Bhattpsijic 更新时间:9/8/2014 访问量:1967

问:

首先,索引路径按 0、1、2...但是在单击表视图后,使用似乎完全随机的 indexpath 调用了 func tablview,因此我无法重现相同的数据。我正在使用数组来关联表的行。

代码在这里:

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}


override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return  myList.count
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
// Configure the cell...

    println("table view --> ")
    let CellID : NSString = "Cell"
    var cell : UITableViewCell = tableView?.dequeueReusableCellWithIdentifier(CellID)as UITableViewCell
     var VehicleName = cell.viewWithTag(1) as UILabel
     var VehicleNumber = cell.viewWithTag(2) as UILabel

     if let ip = indexPath
     {

        VehicleName.text = arrayData[ip.row*2];
        VehicleNumber.text = arrayData[ip.row*2+1];
    }
 }
     return cell

 }
iOS Cocoa-Touch Swift

评论

1赞 Nirav Bhatt 9/8/2014
为什么你期望indexPaths的顺序?另外,您在这里提到了很多错误的函数名称。请明确你想在这里实现什么,到目前为止你尝试了什么,什么不起作用。
0赞 psijic 9/8/2014
我正在从 arrayData 中获取数据并使用 indexPath 串行获取数据。因此,当 cellForRowAtIndexPath 没有像之前那样在序列中调用时,我得到的是表上显示的随机数据。

答:

0赞 zaph 9/8/2014 #1

您有一个设计问题。您需要能够以随机访问方式按请求提供数据。

研究有关 UITableView 和“UITableViewDataSource”的文档。

API 调用 cellForRowAtIndexPath 是因为它需要数据,它只是请求需要显示的数据,而不是每次都请求所有数据。它仅创建正在显示的单元格。如果您有 1000 行数据,并且显示第 900 行到第 903 行,则它只需要该数据。如果视图滚动以显示另一行,则只需要第 904 行中的更多数据。

评论

0赞 psijic 9/12/2014
谢谢。我可能会尝试更改我的数组上的依赖关系。
0赞 Nirav Bhatt 9/8/2014 #2

绝对不能保证 indexPaths 的调用顺序。它基于 UIKit 框架。cellForRowAtIndexPath

您需要并且能够完全控制数据源数组保存数据的方式。在这种情况下,它是 - 我也有一种感觉,这在某种程度上与 有关,但从您的代码片段中看不到它。研究元素在 arrayData 中的排列方式,然后您将在所有单元格中都有预期的元素。arrayDatamyList

评论

0赞 psijic 9/12/2014
谢谢,将尝试使其独立于我的数组。