我有一个UITableView,在某些情况下,需要在它的顶部添加一些东西。所有数据(除了在特定条件下插入在UITableView顶部的数据)都是从一个数组中传入的。
因为所有内容都是从数组中获取的,所以我需要修改每次调用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法时获取这些数组对象的indexPath。如果我尝试创建一个本地变量并更新indexPath.row的本地版本,它会告诉我它是只读的。
实现这一点的最佳方式是什么?
下面的绘图(这不是代码,而是表视图的绘图):
(REGULAR SITUATION) (3 lines)
array objectAtIndex:0;
-----
array objectAtIndex:1;
-----
array objectAtIndex:2;等
(MODIFIED SITUATION) (4 lines)
blah blah modified insertion text here
-----
array objectAtIndex:0;
-----
array objectAtIndex:1;
-----
array objectAtIndex:2;等等等等
提前谢谢。
发布于 2011-09-13 04:38:45
为什么不直接在数组的开头添加新项呢?
// Create a mutable copy and add the item at index 0
NSMutableArray *mutable = [myData mutableCopy];
[mutable insertObject:newItem atIndex:0];
// Then store the new array and reload the table
[myData autorelease];
myData = mutable;
[self.tableView reloadData];这样你就不需要对索引路径做任何有趣的事情了:)
发布于 2011-09-13 03:21:20
不要走那条路。只需使用UITableView的tableHeaderView属性在表的顶部添加一些内容。它会像UITableViewCell一样滚动。
self.tableView.tableHeaderView = aView;要删除它,只需将其设置为nil即可。
如果您坚持使用这种方法,只需保留一个BOOL来告诉您所处的状态,如果需要额外的行,只需从indexPath.row中减去1,例如
[myArray objectAtIndex:indexPath.row-1];发布于 2011-09-13 03:26:51
THIS是一个很好的教程,它解决了你的问题。
使用
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section或
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section根据您在决定何时显示或不显示时所使用的条件,将String/View返回为某个内容或为空。
https://stackoverflow.com/questions/7392718
复制相似问题