2011-08-15 6 views

答えて

9

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

} 
+0

didSelectRowAtIndexPathは( –

+0

が次にあなたが開始インデックスを選択する必要があります(まだ呼び出されていませんあなたのボタンアクションでまだ選択が行われていない場合は、私は答えを確認するためにコードを更新しました。 – mjisrawi

1

最も簡単な方法は、を選択したインデックスのトラックにすることです。

  • 選択したインデックスを記録します。
  • オンボタンでインデックスを変更します。
  • テーブルをリロードします。 [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"] 
0

あなたはscrollToRowAtIndexPathメソッドという名前のメソッドであることを行うことができます。また、セルが選択されるたびにindexPathを格納することができます。したがって、ユーザーの再起動時に復元することができます。

0

...のような何かを行うことができますしてください

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フラグ「ユーザー対話が有効」をオフに切り替えることをお勧めします。

2

次の/前の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 
関連する問題