2011-10-24 67 views
3

我有一個TableView,它在didSelectRow中加載detailview。我對這些數據沒有任何問題。 我正在使用coreData和填充TableView我使用NSFetchedResultsController。如何跳轉到詳細視圖中的下一行

現在,我想使detailView 中的下一個和上一個函數跳轉到下一個或上一個表(行)元素。下一個上一個喜歡在郵件應用程序。
我知道我必須使用帶有IBAction的按鈕。

但是我怎樣才能使這個功能?

感謝,
brush51

編輯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' 

如何解決這個?

回答

10

有了您的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]; 
} 
+0

謝謝。我怎樣才能在detailview中設置我的tableView?還是我在這裏錯過了一部分? – brush51

+0

您可以將詳細視圖控制器中的引用添加到父控制器,當您推送詳細控制器時,在didSelectRowAtIndexPath上分配父控制器 –

+0

以及如何操作?你能解釋一下嗎? Iam對我在ios上的低級體驗感到抱歉。 – brush51

0

您可以使用:

(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

爲了得到你想要的單元格(一個或下一個)。

然後將返回的單元格「Selected」屬性設置爲YES。

cell.selected = YES

1

您可以使用DataSource數組進行管理。

當你加載你的詳細視圖,那時你傳遞你的數組索引(indexPath.row)。通過該索引,您可以在詳細視圖中獲取數據。並使用該索引中的遞增和遞減數據獲取下一個和前一個單元格的數據。

步驟

1)設置您的tableView。

2)此時重定向到DetailView時,爲存儲當前indexPath.row設置一個int變量。所以在詳細視圖中定義一個int。

3)使用下一個和上一個按鈕管理int變量中的indexpath.row。 並表示來自Source數組的適當數據。

+0

好吧,我已將indexPath傳遞給detailview。以及如何使用indexpath在detailview中獲取數據? – brush51

相關問題