2014-08-28 108 views
0

我想在表格视图中显示128个单元格。但是,由于某种原因,表格视图最多显示五个单元格。我检查了代码返回的行数,它大于5.所以我相信那部分是正确的。另外,我编写了自定义单元的代码。这是否有助于这种行为?如果是,我该怎么办?如果不是,我做错了什么?为表视图表视图显示有限数量的单元格?

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

} 

/*码*/

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

    } 

谢谢!

+1

'tableView(numberOfRowsInSection)'中'println'的结果是什么? – 2014-08-28 00:13:20

回答

0

UITableView方法dequeueReusableCellWithIdentifier是为什么一次只实例化5个单元格的原因。 UITableView只创建足够的对象来填充屏幕。当一个人滚动并且不再在视野中时,它就排队等待再次使用。如果您快速滚动,它可能会创建比屏幕更多的单元格,但通常它会用来覆盖屏幕。

您可以为显示的每个索引路径创建一个新的UITableViewCell但是,除非您自己保留对象引用,否则它将在屏幕外滚动时超出范围。你可以通过将它添加到你的班级管理的数组来完成。

相关问题