具有多个原型单元的Swift TableView不显示所有行

时间:2015-10-19 21:36:26

标签: swift uitableview swift2

我有一个用2个原型单元创建的UITableView,每个单元都有单独的标识符和子类。

我的问题是当我显示细胞时,第二个原型的第一行被第一个原型细胞吸收。

例如,我有第一个原型单元格只显示1个项目。第二个原型单元格应该显示4个项目。但是,第二个原型单元格中的第一个项目没有显示,相反,四个项目中只有3个可见。

以下是我实施的代码:

  override func viewDidLoad() {
 super.viewDidLoad()


    self.staticObjects.addObject("Please...")
    self.objects.addObject("Help")
    self.objects.addObject("Me")
    self.objects.addObject("Thank")
    self.objects.addObject("You")

    self.tableView.reloadData()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.objects.count
}

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

   if(indexPath.row==0){

         let staticCell = self.tableView.dequeueReusableCellWithIdentifier("staticCell", forIndexPath: indexPath) as! StaticTableViewCell

         staticCell.staticTitleLabel.text = self.staticObjects.objectAtIndex(indexPath.row) as? String

        return staticCell

    }

    else{

        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PillarTableViewCell

        cell.titleLabel.text = self.objects.objectAtIndex(indexPath.row) as? String


        return cell

    }

感谢所有帮助。

1 个答案:

答案 0 :(得分:1)

对于tableView:numberOfRowsInSectiontableView:cellForRowAtIndexPath,您在表格中如何计算行数存在逻辑问题。

您的代码正在生成如下所示的显示输出:

  • 蓝色单元格代表您的staticCell原型表格单元格视图;这些是staticsObjects数组中的值。
  • 黄色单元格代表您的cell原型表格单元格视图;这些是objects数组中的值。

Bad Output of Table View

<强> 1。查看tableView:cellForRowAtIndexPath:

cellForRowAtIndexPath方法中,您返回objects数组的计数。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        
    return self.objects.count
}

这意味着您在表格中的行数将是 4 而不是5.相反,您希望返回表格中使用的两个数组的总和:{ {1}}。例如:

objects.count + staticObjects.count

<强> 2。查看tableView:cellForRowAtIndexPath:

这是你的原始代码和我的评论..

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return objects.count + staticObjects.count
}

现在,这是解决上述讨论中所述错误的一种方法:

// You are assuming that `staticObjects` will always have 
// exactly one row. It's better practice to make this
// calculation more dynamic in case the array size changes.
if (indexPath.row==0){

    let staticCell = self.tableView.dequeueReusableCellWithIdentifier("staticCell", forIndexPath: indexPath) as! StaticTableViewCell
    staticCell.staticTitleLabel.text = self.staticObjects.objectAtIndex(indexPath.row) as? String
    return staticCell            
} else {            
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PillarTableViewCell
    // Here's your problem! You need to calculate the row
    // because you want to put the objects from your other
    // array first. As a result, you need to account for them.
    cell.titleLabel.text = self.objects.objectAtIndex(indexPath.row) as? String
    return cell     
}

现在你应该看到这个:

Correct Display of Table View

相关问题