如何以编程方式转到UITableView中的下一个/上一个单元格?

时间:2011-08-15 08:04:18

标签: iphone objective-c uitableview

我有UItableview和两个按钮Next和Previous点击,我应该去选择上一个/下一个单元格?可能吗?此外,如何记住用户最后点击的单元格,以便在启动时选择它?

5 个答案:

答案 0 :(得分: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];

}

答案 1 :(得分: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

答案 2 :(得分: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"]

答案 3 :(得分:0)

您可以使用名为scrollToRowAtIndexPath方法的方法执行此操作。并且您可以在选择单元格时存储indexPath。因此,您可以在用户重新启动时恢复它。

答案 4 :(得分: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”。