私はUItableviewを持っていますまた、前と次のセルの選択に行く必要がある次と前のタップの2つのボタン?出来ますか?さらに、ユーザーが最後にタップしたセルを覚えて、起動時に選択する方法は?UITableViewの次/前のセルにプログラムで移動するにはどうすればいいですか?
答えて
UITableViewDelegateを実装しdidSelectRowAtIndexPathで現在選択されたインデックスを保存します、そして、あなたのボタンアクションにあなたがのtableViewでいくつかのセルを選択するための
- (IBAction)didTapNextButton:(id)sender{
//Remember to check boundaries before just setting an indexpath or your app will crash!
if(currentSelection){
currentSelection = [NSIndexPath indexPathForRow:currentSelection.row+1 inSection:currentSelection.section];
}else{
currentSelection = [NSIndexPath indexPathForRow:0 inSection:0];
}
[self.tableView selectRowAtIndexPath:currentSelection animated:YES scrollPosition: UITableViewScrollPositionTop];
}
最も簡単な方法は、を選択したインデックスのトラックにすることです。
- 選択したインデックスを記録します。
- オンボタンでインデックスを変更します。
- テーブルをリロードします。 [tableview reloadData];
あなたがcellForRowAtIndexPath関数を介して選択したインデックスを公開するだけです:
if (indexPath.row == 0) {
cell.selected = true;
}
をに関しては、私はあなたの状態の管理を参照考える第二部へ。そうNSUserDefaultsが、私は通常、それを行う方法であれば:)
// get
[[NSUserDefaults standardUserDefaults] objectForKey:@"myLastSelectedIndex"]
// set to 5
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:5] forKey:@"myLastSelectedIndex"]
あなたはscrollToRowAtIndexPathメソッドという名前のメソッドであることを行うことができます。また、セルが選択されるたびにindexPathを格納することができます。したがって、ユーザーの再起動時に復元することができます。
...のような何かを行うことができますしてください
NSIndexPath *currentSelection;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
currentSelection = indexPath;
}
を、使用:- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
(http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html)。
NSUserDefaults* def = [NSUserDefaults standardUserDefaults];
[def setObject:selectNum forKay:@"someKay"];
[def synchronize];
、私は思う:索引の が最も有用である、あなたは多くの方法を使用して格納し、選択しますが、。
さらに、あなたの場合、tableViewフラグ「ユーザー対話が有効」をオフに切り替えることをお勧めします。
次の/前のNSIndexPath
を特定のNSIndexPath
から見つけることができる、いくつかのUITableView
カテゴリメソッドがあります。
これらのメソッドは、セクションと空のセクションに対応します。
@interface(.H)
@interface UITableView(Extensions)
- (NSIndexPath *)nextIndexPathFromIndexPath:(NSIndexPath *)indexPath;
- (NSIndexPath *)prevIndexPathFromIndexPath:(NSIndexPath *)indexPath;
@end
@implementation(.M)
@implementation UITableView(Extensions)
-(NSIndexPath *)nextIndexPathFromIndexPath:(NSIndexPath *)indexPath {
return [self adjacentIndexPathFromIndexPath:indexPath goingForward:YES];
}
-(NSIndexPath *)prevIndexPathFromIndexPath:(NSIndexPath *)indexPath {
return [self adjacentIndexPathFromIndexPath:indexPath goingForward:NO];
}
-(NSIndexPath *)adjacentIndexPathFromIndexPath:(NSIndexPath *)indexPath goingForward:(BOOL)goingForward {
NSInteger delta = goingForward ? 1 : -1;
NSInteger adjRow = indexPath.row + delta;
if (adjRow >= 0 && adjRow < [self numberOfRowsInSection:indexPath.section]) {
//Adjacent row in same section...
return [NSIndexPath indexPathForRow:adjRow inSection:indexPath.section];
}
NSInteger adjSection = indexPath.section + delta;
if (adjSection < 0) {
adjSection = [self numberOfSections] - 1;
} else if (adjSection > [self numberOfSections] - 1) {
adjSection = 0;
}
NSUInteger adjSectionRows = [self numberOfRowsInSection:adjSection];
if (!adjSectionRows) {
//Adjacent section is empty...
//Adjacent row in adjacent-adjacent section...
return [self adjacentIndexPathFromIndexPath:[NSIndexPath indexPathForRow:0 inSection:adjSection] goingForward:goingForward];
}
adjRow = goingForward ? 0 : adjSectionRows - 1;
//Adjacent row in adjacent section...
return [NSIndexPath indexPathForRow:adjRow inSection:adjSection];
}
@end
- 1. Angular.jsの次のステップまたは前のステップに移動するにはどうすればよいですか?
- 2. iPhoneで次と前のボタンをプログラムで追加するにはどうすればいいですか?
- 3. プログラムでAndroidのデフォルトホーム画面に移動するにはどうすればいいですか?
- 4. 元のページに移動するにはどうすればいいですか?
- 5. UITableViewの行移動後にCoreDataを更新するにはどうすればよいですか?
- 6. UITableViewセルにUITableViewを追加するにはどうすればよいですか?
- 7. ndbカーソルで前のページに移動するにはどうすればいいですか?
- 8. NSViewをすべてのNSViewの前面に移動するにはどうすればいいですか?
- 9. dequeueReusableCellWithIdentifier中にuitableviewセルのreuseidentifierにnil値を設定するにはどうすればいいですか?
- 10. iOSでGoogleマップの移動に伴いマーカーを移動するにはどうすればよいですか?
- 11. ボタンで2つのセクション間でセルを移動するにはどうすればよいですか?
- 12. divを他のdivの前に移動するにはどうすればいいですか?
- 13. ビジュアルコードエディタの前のカーソル位置に移動するにはどうすればいいですか?
- 14. 次のdivにdivを移動するにはどうすればいいですか?
- 15. GridPaneのボタンを移動するにはどうすればいいですか?
- 16. UITableViewはセルに移動します
- 17. 実行せずにRコマンドプロンプトの次の行に移動するにはどうすればよいですか?
- 18. javascriptでkeyPressedを使って次の要素に移動するにはどうすればよいですか?
- 19. ページヘッダーブロックをフッターに移動するにはどうすればいいですか?
- 20. ジオジソンポリゴンをリーフレットに移動するにはどうすればいいですか?
- 21. ギャラクシーダッシュボードに移動するにはどうすればいいですか?
- 22. ライトボックスに移動するにはどうすればいいですか?
- 23. 他のアプリケーションからプログラムでウィンドウを移動/サイズ変更するにはどうすればよいですか?
- 24. UIButtonをプログラム的に動かすにはどうすればいいですか?
- 25. Vim Ultisnips - 次のプレースホルダまたはタブトップに移動するにはどうすればよいですか?
- 26. アンドロイドアプリケーションで画像を次々と移動するにはどうすればいいですか?
- 27. UITableViewの特定のセルのテキストの色を変更するにはどうすればよいですか?
- 28. 次の大文字に移動するにはどうすればよいですか?
- 29. Android GUI - ボタンを次の行に移動するにはどうすればよいですか?
- 30. Excel次のセルに移動する前にセルを最大化する機能
didSelectRowAtIndexPathは( –
が次にあなたが開始インデックスを選択する必要があります(まだ呼び出されていませんあなたのボタンアクションでまだ選択が行われていない場合は、私は答えを確認するためにコードを更新しました。 – mjisrawi