2011-08-15 15 views

回答

9

實施的UITableViewDelegate並保存在didSelectRowAtIndexPath方法當前選定的指標:

NSIndexPath *currentSelection; 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    currentSelection = indexPath; 
} 

然後在您的按鈕操作,你可以這樣做......

- (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

對於選擇的tableView一些細胞,請,請使用:- (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標誌「User Interaction Enabled」。

2

這裏有幾個UITableView類別的方法,可以從指定的NSIndexPath找到下一個/前一個NSIndexPath

這些方法容納部分和空白部分。

@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 
相關問題