Swift:cellForRowAtIndexPath不按indexPaths的顺序调用

首先,indexpath按照0,1,2 …的顺序调用,但是在点击table view之后,func tablview被调用,其索引path似乎是完全随机的,因此我无法重现相同的数据。 我正在使用数组关联表的行。

代码在这里:

// 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 } 

你有一个devise问题。 您需要能够按照随机访问的要求提供数据。

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

API调用cellForRowAtIndexPath,因为它需要数据,它只是请求它需要显示的数据,而不是每个数据。 它只创build正在显示的单元格。 如果你有1000行的数据,并通过说903显示行900,它只需要这些数据。 如果视图滚动以再显示一行,则只需要来自行904的更多数据。

索引pathcellForRowAtIndexPath被调用的顺序绝对不能保证。 它在UIKit框架上。

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