2011-09-16 63 views
1

我有一張桌子。當我點擊表格行時,我想推新視圖。我知道,在navigationController以及如何推動它,但我想用這樣的代碼:點擊表格行推新視圖

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
// Navigation logic -- create and push a new view controller 

    TasksViewController *tasks = [[TasksViewController alloc] 
        initWithNibName:@"Tasks" 
        bundle:nil]; 

    LoginAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 
    [delegate.window addSubview:[tasks view]]; 
    [self.view removeFromSuperview];   
} 

的TaskViewCOntroller一種觀點我想要把這樣的。當我嘗試執行此操作時,我有這樣一個異常:

*** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:5678 
2011-09-16 12:26:41.118 LeoAction[1149:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x00df55a9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x00f49313 objc_exception_throw + 44 
    2 CoreFoundation      0x00dadef8 +[NSException raise:format:arguments:] + 136 
    3 Foundation       0x000e03bb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116 
    4 UIKit        0x0035ac91 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 883 
    5 UIKit        0x003504cc -[UITableView(UITableView 

我不知道如何處理此異常?

+0

請粘貼您的cellForRowAtIndexPath函數的代碼。 – MathieuF

+0

代碼從'TasksViewController' – Nekto

+0

該函數的 - (的UITableViewCell *)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath { \t靜態的NSString * CELLID = @ 「customCell」; CustomCell * cell =(CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellID]; \t \t 如果(細胞==無) \t { \t \t的NSArray * nibObjects = [[一個NSBundle mainBundle] loadNibNamed:@ 「CustomCell」 所有者:無選項:無]; \t \t爲(在nibObjects ID currentObject) \t \t { \t \t \t如果([currentObject isKindOfClass:[CustomCell類]]) \t \t \t { \t \t \t \t細胞=(CustomCell *)currentObject; \t \t \t} \t \t} \t} \t cell.textLabel.numberOfLines = 0; \t [cell.textLabel sizeToFit]; \t return cell; } – Petko

回答

-1

你應該推動這樣的:

[self.navigationController pushViewController:taskViewController animated:YES];

+0

Francisco但是我根本沒有導航控制器? – Petko

+0

我實際上應該閱讀這些問題......對不起 –

-1

U可以推像婁

TasksViewController *tasks = [[TasksViewController alloc] 
       initWithNibName:@"Tasks" 
       bundle:nil]; 

[self.navigationController pushViewController:tasks animated:YES]; 
[tasks release]; 
+0

你能提供cellForRowAt indexpath代碼 – Srinivas

+0

見上面我已經提供 – Petko

2

例外是告訴你,你在tableView:cellForRowAtIndexPath:方法的問題。這聽起來像你沒有爲被調用的方法的每個實例提供正確的返回路徑。

檢查該方法的結尾是否有類似return cell;的東西。

至於你的導航,你應該這樣做:

[self.navigationController pushViewController:tasks animated:YES]; 

UIViewController爲您定義導航控制器,所以推新是實例,並調用上面的方法一樣容易。

編輯

至於導航控制器 - 當你設置你的頂級表視圖控制器,你需要它由一個UINavigationController所擁有。爲了簡單起見,我將假定您正在使用Interface Builder。

在您的MainWindow.xib中,拖動導航控制器對象。展開其公開箭頭,您會看到它包含導航欄和根視圖控制器。選擇此根視圖控制器,並在身份檢查器中,將其類更改爲您的自定義表視圖控制器,並在屬性檢查器中將其NIB名稱字段更改爲適當的筆尖。根據需要將導航控制器連接到App Delegate對象。

然後您應該會發現,您可以使用pushViewController:animated:方法self.navigationControllertableView:didSelectRowAtIndexPath:內。

我希望這會有所幫助。

+0

我這樣做了,就像你提議的那樣,但是沒有發生什麼事?我沒有navigationController我做我的導航像window.pushView就像我上面的問題。 – Petko

+0

異常消失了,但是這樣的事情沒有發生? – Petko

+0

@Petko:當您添加頂級表格視圖控制器(包含您發佈的代碼的控制器)時,您需要確保它是導航控制器的根視圖控制器。我會再次編輯我的問題並提供更多詳細信息。 – Stuart