2009-08-04 74 views
1

我相信这是一件容易的事情。我对客观C非常陌生(找到了一份老板打算外包的工作),并且可以使用我可以获得的所有帮助。请在这里指出我正确的方向。基本上我想要建立的是这样的:将UITableView行移动到列表顶部时

我有一个电话号码列表,根据需要扩展到多行。我填充没有问题,并且列表工作正常,但我希望列表中最近选定的所有项目都能够在顶部更容易地访问。所以,无论何时用户“didSelectRowAtIndexPath”,我希望该行移动到顶部。

我只是知道这是超级简单...但我还没有找到解决方案。不要着急。我只是想自己解决问题。提前致谢!

回答

3

元素的顺序是数据源(您的代码)的责任,而不是表视图。如果你想重新排序,那么你需要重新排序你用来驱动-cellForRowAtIndexPath:的数据结构。如果你想要动画,我不相信还有一个好的动画。你可以做的最好的(容易)是执行删除和插入。有关如何为其设置动画的详细信息,请参见Batch Insertion and Deletion of Rows and Sections。几个月前,我向UITableView开发者询问OS2.x中这些动画的一般性问题和脆弱性,他们向我保证,在OS3中有很多工作要改进。所以希望你可以按照文档,它不应该崩溃了。

0

UITableView中有一个名为方法

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath

我相信你确实有,虽然写的代码,我不知道,因为我从来没有带桌子的工作(我是新和) 。

2

没有能力从表中的一个位置移除一行到另一个位置。它可以通过删除的行,然后将其放回在新的位置进行仿真:

// Remove the row from your datasource here 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:sourcePath] withRowAnimation:UITableViewRowAnimationLeft]; 
// Add the row back into the datasource here 
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:destPath] withRowAnimation:UITableViewRowAnimationLeft]; 

替换正确的命令的意见更新您的数据源

0

试试这个(工作代码):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

if(indexPath.row == 0) return; 

NSString * selectedNum = [numbers objectAtIndex:indexPath.row]; // NSMutableArray* numbers = dataStore 
[selectedNum retain]; 

[numbers removeObjectAtIndex:indexPath.row]; 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
      withRowAnimation:UITableViewRowAnimationLeft]; 
[numbers insertObject:selectedNum atIndex:0]; 
[tableView insertRowsAtIndexPaths:[NSArray 
      arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] 
      withRowAnimation:UITableViewRowAnimationLeft]; 

[selectedNum release]; 

}

+0

嗨@Tomas,你的代码的工作,但我们可以有这样明确的应用动画效果 – Ranjit 2012-08-02 11:04:25