2012-01-29 30 views
0

我有一个tableVIew,其中每个单元格的高度为90.0f(高度)。我的代码;TableView单元格外观问题

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 90.0; 
} 

现在,当我删除表中的所有记录。单元格的高度缩小到它的默认值44(我认为)。

1.)我的问题是,即使在用户删除所有记录后,我仍然需要单元格的高度保持为90.0f。我如何以编程方式执行此操作?

2.)我给了我的细胞2种颜色。

cell.contentView.backgroundColor = indexPath.row % 2 ? [UIColor whiteColor] : [UIColor blackColor] ; 

当我删除第二个单元格,它是黑色的,那么第一个和第三个单元格都是白色的。我怎么能重新安排这是一个订单黑色和白色。

编辑:

好这个工作,但是当我尝试删除表中的最后一个记录和[tableview indexPathsForVisibleRows]我最终得到断言失败

-[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:961

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. Attempt to delete more rows than exist in section.'

这是为什么?

CODE:

[self.tableView beginUpdates]; 

     [self.myArray removeObjectsInArray:discardedItems ]; 

     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];  

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES]; 

     [self.tableView endUpdates]; 
+0

你知道你可以使用blackColor而不是黑色写出的RGB值。 – 2012-01-29 16:18:56

+0

还有一些其他的颜色,我不得不改变它发布到SO – sharon 2012-01-29 16:20:35

回答

1

1)我不认为你可以做到这一点。其中一种方法是显示x个空白单元格以填充页面。

2)删除单元格后调用[tableView reloadData],或在删除单元格前后的行上调用[tableView reloadCellAtIndex ...]。

+0

我编辑了我的问题。 – sharon 2012-01-30 16:19:11

1

如果行的高度确实是静态的(如您所示),请考虑不实施tableView:heightForRowAtIndexPath:,而是使用表视图的rowHeight属性。
这可以在代码或界面生成器中设置,应该是“如果没有问题,行缩小”的解决方案。

您的第二个问题:
您是否知道-[UITableView indexPathsForVisibleRows]

您可以使用它来只重新加载需要更新的表视图部分。它会是这样的:

- tableView:(UITableView *)table commitEditingStyle:(UITableViewCellEditingStyle)style forRowAtIndexPath:(NSIndexPath *)path 
{ 
    NSArray *visibleCellPaths = [table indexPathsForVisibleRows]; 

    // replace this comment with whatever else needs to be done in any case... 

    if (style == UITableViewCellEditingStyleDelete) { 
     // replace this comment with the deletions of the model object 

     NSUInteger affectedPathIndex = [visibleCellPaths indexOfObject:path]; 
     NSMakeRange updateRange = NSMakeRange(affectedPathIndex, [visibleCellPaths count] - affectedPathIndex); 
     NSArray *cellPathsToReload = [visibleCellPaths subarrayWithRange:updateRange]; 

     // replace this comment with the bounds-checking. 
     // e.g. if you only had one section and the backing store was an array, it may look like this: 
     // while ([self.backingStore count] <= [[cellPathsToReload lastObject] row]) { 
     // NSUInteger remainingPathCount = [cellPathsToReload count]; 
     // if (remainingPathCount) break; 
     // cellPathsToReload = [cellPathsToReload subarrayWithRange:NSMakeRange(0, remainingPathCount - 1)]; 
     // } 

     [table reloadRowsAtIndexPaths:cellPathsToReload withRowAnimation:UITableViewRowAnimationFade]; 
    } 

} 
+0

确定这个工程,但当我尝试删除表中的最后一条记录和'[tableview indexPathsForVisibleRows]'我最终得到'断言失败 - [UITableView _endCellAnimationsWithContext:],/SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m :961'和'终止应用程序,由于未捕获的异常'NSInternalInconsistencyException',原因:'无效的更新:无效的行数在第0节。试图删除更多的行比节中存在'.'为什么? – sharon 2012-01-30 16:17:32

+0

我已经更新了我的答案,并用我实际测试过的东西替换了代码。这在我的设置中效果很好,但是你的里程可能会有所不同...... – danyowdee 2012-01-31 03:38:26

0
  1. 设置UITableViewrowHeight财产。

文档状态:

在接收机的每一行(表单元格)的高度。

  1. 请提供您的数据源代码
+0

我已将我的代码添加为编辑。请看看问题的编辑点 – sharon 2012-01-30 16:43:21