【问题标题】:How to jump to the next row in detailview如何在详细视图中跳转到下一行
【发布时间】:2011-10-24 10:53:09
【问题描述】:

我有一个 TableView,它在 didSelectRow 中加载一个详细视图。我对数据没有任何问题。 我正在使用 coreData 并使用 NSFetchedResultsController 填充 TableView。

现在,我想在 detailView 中创建下一个和上一个函数跳转到下一个或上一个 Table(row) 元素。下一个 上一个,就像在邮件应用程序中一样。
我知道我必须使用带有 IBAction 的按钮。

但是我怎样才能实现这个功能呢?

谢谢,
刷子51

编辑 1: 我已经这样做了:

- (IBAction) previousCard:(id) sender {  
    NSLog(@"previousBtn");

DashboardViewController *parent = (DashboardViewController *)self.parentViewController;

NSIndexPath *actualIndexPath = selectedRow2;
NSIndexPath * newIndexPath = [NSIndexPath  indexPathForRow:actualIndexPath.row-1 inSection:actualIndexPath.section];
NSLog(@"newIndexPath: %@", newIndexPath);
[parent.tableViews selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; //HERE I GET SIGABRT

}

但我得到一个错误:

-[UIViewController tableViews]: unrecognized selector sent to instance 0xd758ae0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableViews]: unrecognized selector sent to instance 0xd758ae0'  

如何解决?

【问题讨论】:

    标签: ios uitableview core-data row nsfetchedresultscontroller


    【解决方案1】:

    使用您的 UITableView,您可以通过以下方式获取所选行:

    - (NSIndexPath *)indexPathForSelectedRow
    

    并通过增加 indexPath.row 来设置选择:

    - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
    

    那么你的行动可能是:

    -(IBAction)nextCell:(id)sender {
      NSIndexPath * actualIndexPath = myTableView.indexPathForSelectedRow;
      NSIndexPath * newIndexPath = [NSIndexPath  indexPathForRow:actualIndexPath.row+1 inSection:actualIndexPath.section];
      [myTableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    }
    

    【讨论】:

    • 谢谢。以及如何在详细视图中设置我的 tableView?还是我在这里遗漏了一部分?
    • 您可以在详细视图控制器中添加对父控制器的引用,在推送详细控制器时将父控制器分配到 didSelectRowAtIndexPath 上
    • 那该怎么做呢?你能解释一下吗?我很抱歉我在 ios 方面的经验不足。
    • 好的,引用完成了:(MyParentViewController *)self.parentViewController。我现在可以使用 parentView 中的表,但在 indexPathForSelectedRow 中出现错误:-[UIViewController tblViewC]:无法识别的选择器已发送到实例 0x4eaa7b0 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[UIViewController tblViewC]:无法识别...我到底要做的是 didSelectRowAtIndexPath?如何在那里分配partentController?再次感谢。
    【解决方案2】:

    为此,您可以使用 DataSource 数组进行管理。

    当您加载详细视图时,您会传递数组索引(indexPath.row)。通过该索引,您可以在详细视图中获取数据。并在该索引中使用增量和减量,您可以获得下一个和上一个单元格的数据。

    步骤

    1) 设置您的 tableView。

    2) 当重定向到 DetailView 时,为存储当前 indexPath.row 设置一个 int 变量。所以定义一个 int 详细视图。

    3) 使用下一个和上一个按钮管理 int 变量中的 indexpath.row。 并表示来自 Source 数组的适当数据。

    【讨论】:

    • 好的,我已将 indexPath 传递给 detailview。以及如何使用 indexpath 获取详细视图中的数据?
    【解决方案3】:

    你可以使用:

    (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

    获取您想要的单元格(下一个或上一个)。

    然后将返回的单元格“Selected”属性设置为YES。

    cell.selected = YES

    【讨论】:

      猜你喜欢
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 2014-05-05
      • 2018-07-14
      • 2015-07-23
      • 1970-01-01
      相关资源
      最近更新 更多