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

问题描述:

我有一个用2个原型单元格创建的UITableView,每个原型单元格都有独立的标识符和子类。具有多个原型单元格的Swift TableView不显示所有行

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

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

这里是我的实现代码:

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 

    } 

感谢所有帮助。

你有逻辑问题与你如何两个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 
} 

这意味着,行数,你会在你的桌子会,而不是5.相反,你想回到你正在使用你的表格中两个数组的总和:objects.count + staticObjects.count。例如:

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

2.看的tableView:的cellForRowAtIndexPath:

这里是你原来的代码与我的意见..

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

现在,这里有一个方法来解决上述讨论指出你的错误:

// If the indexPath.row is within the size range of staticObjects 
// then display the cell as a "staticCell". 
// Notice the use of "staticObjects.count" in the calculation. 
if indexPath.row < staticObjects.count { 
    let staticCell = self.tableView.dequeueReusableCellWithIdentifier("staticCell", forIndexPath: indexPath) as! StaticTableViewCell 
    staticCell.staticTitleLabel.text = self.staticObjects.objectAtIndex(indexPath.row) as? String 
    return staticCell 
} else { 
    // If you get here then you know that your indexPath.row is 
    // greater than the size of staticObjects. Now you want to 
    // display your objects values. 
    // You must calculate your row value. You CANNOT use the 
    // indexPath.row value because it does not directly translate 
    // to the objects array since you put the staticObjects ahead 
    // of them. As a result, subtract the size of the staticObjects 
    // from the indexPath.row.   
    let row = indexPath.row - staticObjects.count 
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PillarTableViewCell 
    cell.titleLabel.text = self.objects.objectAtIndex(row) as? String     
    return cell 
} 

现在你应该看到这一点:

Correct Display of Table View

+0

感谢这个帮助。它是完全有道理的,我感谢你的努力。我在离开一段时间后发现问题。再次感谢。 – Rico

+0

@Rico不客气。很高兴听到你明白了;逐步远离问题总是有帮助的。 :) – whyceewhite

+0

它是怎么回事我实现了上面的代码,并通过打印出迄今为止的5(这是正确的)的计数做了一些检查。一切工作,直到我实现计数逻辑,它将两个细胞计数加在一起。我收到一个NSException和一个SIGABRT信号。不知道为什么会这样。对我来说,似乎计数是超载或溢出。你有什么想法吗?感谢 – Rico