indexPath调整iPhone

时间:2011-09-12 19:09:45

标签: iphone objective-c xcode

我有一个UITableView,在某些条件下,需要添加一些内容。所有数据(在某些条件下插入UITableView顶部的内容除外)都是从数组中引入的。

因为所有内容都是从数组中引入的,所以我需要修改indexPath,每次调用方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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;

等等

提前致谢。

3 个答案:

答案 0 :(得分:2)

THIS是一个很好的教程,可以解决您的问题。

使用

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

OR

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

根据您在决定何时显示时所使用的条件,将String / View作为某些内容返回或为空。

答案 1 :(得分:2)

为什么不在数组的开头添加新项目?

// 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];

然后你不必对索引路径做任何有趣的事情:)

答案 2 :(得分:1)

不要走那条路。只需使用tableHeaderView的{​​{1}}属性即可在表格顶部添加内容。它会像UITableView一样滚动。

UITableViewCell

要删除它,只需将其设置为self.tableView.tableHeaderView = aView;

如果你坚持使用这种方法,只需保持nil告诉你你处于哪种状态,如果你需要额外的行,只需从BOOL中减去1,就像

一样
indexPath.row