Swift:cellForRowAtIndexPath没有按indexPaths的顺序调用

时间:2014-09-08 12:47:06

标签: ios cocoa-touch swift

首先按照0,1,2的顺序调用索引路径...但是在单击表视图后,使用似乎完全随机的索引路径调用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

 }

2 个答案:

答案 0 :(得分:0)

您遇到了设计问题。您需要能够以随机访问方式按要求提供数据。

研究UITableView和`UITableViewDataSource'上的文档。

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

答案 1 :(得分:0)

绝对不能保证indexPaths cellForRowAtIndexPath的调用顺序。它是在UIKit框架上。

您需要并且可以完全控制数据源阵列保存数据的方式。在这种情况下,它是arrayData - 我也感觉这在某种程度上与myList相关,但是在您的代码段中无法看到它。研究如何在arrayData中排列元素,然后在所有单元格中都有预期的元素。