0

I want to display 128 cells in the table view. However, due to some reason the table view displays a maximum of five cells. I checked the number of rows returned by the code, it is greater than 5. So I am sure that part is correct. Also, I have written code for custom cell. Does this contribute to this behavior? If yes, what should I do ? If no, what am I doing wrong ?

/* Custom cell code */

 class myCustomCell: UITableViewCell{
    @IBOutlet var  myTitle: UILabel!
    @IBOutlet var  mySubtitle: UILabel!

    convenience required init(reuseIdentifier: String!){
        self.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier )
    }

}

/* code for table view */

import Foundation
import UIKit
import CoreLocation



    class TableViewController: UITableViewController{

        var rowNumber: String!

        override func viewDidLoad() {
            super.viewDidLoad()
            //println("Count is : \(dataArray.count)")
            // Do any additional setup after loading the view, typically from a nib.
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
            return 1;
        }

        override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
            //println("Here Count is : \(dataArray.count)")
            return dataArray.count
        }

        override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

            let cellId = "cell"
            var cell = tableView.dequeueReusableCellWithIdentifier(cellId) as? myCustomCell
            //UITableViewCell

            if nil==cell {
                cell = myCustomCell(reuseIdentifier: cellId)

            }

            if let ip = indexPath{
                var dict: NSDictionary! = dataArray.objectAtIndex(indexPath.row) as NSDictionary
                cell!.myTitle.text = dict.objectForKey("name") as String

            }
            return cell

        }

        override func tableView(tableView: UITableView!, didSelectRowAtIndexPath: NSIndexPath){
            //println("Clicked \(didSelectRowAtIndexPath.row)")
        }

        override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {

            if(segue.identifier == "centerDetails"){
                var svc = segue!.destinationViewController as CellClickController
                var selectIndex = self.tableView.indexPathForCell(sender as UITableViewCell)
                svc.cellIdx = selectIndex.row
            }
        }

     }

Thanks!

ranafde
  • 157
  • 2
  • 7

1 Answers1

0

The UITableView method dequeueReusableCellWithIdentifier is why only 5-ish cells are instantiated at one time. The UITableView only creates enough UITableViewCell objects are to fill the screen. When one scrolls off and is no longer in view, it is queued for reuse. If you are scrolling quickly, it may create more cells than the screen needs, but generally it will use just enough to cover the screen.

You can create a new UITableViewCell for each index path you display. However it will go out of scope when it scrolls offscreen unless you retain the object reference yourself. You can do this by adding it to an array that your class manages.

Fruity Geek
  • 7,351
  • 1
  • 32
  • 41