Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

ios - Swift: cellForRowAtIndexPath not called in the order of indexPaths

At first the indexpath are called in sequence of 0,1,2... but after clicking on the table view , the func tablview is called with indexpath that seems completely random , and hence i am unable to reproduce the same data . I am using array to correlate the rows of the table .

The code is here:

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

 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a design problem. You need to be able to supply the data as requested in a random access fashion.

Study the documentation on UITableView and `UITableViewDataSource'.

The API calls cellForRowAtIndexPath as it needs the data, it just requests the data that it needs to display, not all the data each time. It only creates the cells that are being displayed. If you had 1000 rows of data and were displaying rows 900 through say 903 it only needs that data. If the view scrolls to display one more row it only needs more data from row 904.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...