2016-06-29 34 views
0

我有一個UITableViewCell子類,其中每個表格單元格包含一個UICollectionView多個UIcollectionViewCell s。如何使用Segue選擇UICollectionViewCell時

我需要點擊任何collectionViewCell時必須打開一個新的viewController。

我已重寫以下方法,

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 

正如我的UITableViewCell子類已經實現了整體的CollectionView所以我無法呼叫[self performSegueWithIdentifier:@"NewViewController" sender:self];

我已經在同一個班級嘗試prepareForSegue但它加載新的viewController之前didSelectItemAtIndexPath方法。

回答

0

在你的UITableViewCell子類

@protocol MyUITableViewCellDelegate <NSObject> 
-(void)selectedCell; 
@end 


@interface MyUITableViewCell : UITableViewCell 
.... 
.... 
@property(nonatomic, weak) id<MyUITableViewCellDelegate>delegate; 
.. 
.. 
@end 

在您的視圖控制器,每個UITableViewCell的設置

cell.delegate = self; 

爲了您SomeViewController.m文件

@interface SomeViewController : UIViewController<MyUITableViewCellDelegate>{ 
..... 
} 

@end 

@implementation SomeViewController 
..... 
...... 
-(void)selectedCell{ 
    [self performSegueWithIdentifier:@"NewViewController" sender:self];// The segue with identifier 'NewViewController' should be from SomeViewController to the desired view controller. 

} 
..... 
@end 

並調用selectedCell這樣

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
    [self.delegate selectedCell]; 
} 

我想這會解決你的麻煩。

+0

工作是啊這解決了我的問題。謝謝@Zartha –

+0

@AhmadNauman快樂編碼! – Zartha

0

您可以使用此代碼

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
    YourViewController *newView = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"NewViewController"]; 
    [self.navigationController pushViewController:newView animated:YES]; 
} 
+0

'navigationController'屬性不是在'UITableViewCell'子類 –

+0

可用在AppDelegate中 // AppDelegate.h @屬性(非原子,強)的UINavigationController * navController; 在didFinishLaunching [window setRootViewController:navController]; 並使用這個 UINavigationController * navController = window。RootViewController的; 並在您的tableview單元格中使用navController。 – Pushkraj

0

不能從裏面UITableViewCell調用[self performSegueWithIdentifier:@"NewViewController" sender:self];這似乎是你的情況下,performSegueWithIdentifierUIViewController方法,因此在邏輯上就需要調用的UIViewController此功能。

您需要安裝協議,並從內撥打火警的方法UITableViewCellUIViewController,像這樣

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
    [self.delegate cellDidSelect:indexPath]; 
} 

而在UIViewController,你必須UITableView只是處理

-(void)cellDidSelect:(NSIndexPath*)indexPath{ 
    [self performSegueWithIdentifier:@"NewViewController" sender:self]; 
} 

或者,在UITableViewCell中使用UIViewController引用作爲屬性,並直接調用,如

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
     [self.parentContoller performSegueWithIdentifier:@"NewViewController" sender:self.parentContoller]; 
    } 

希望它可以幫助你。乾杯。

+0

我得到這個錯誤 終止應用程序,由於未捕獲異常'NSInvalidArgumentException',原因:'當我從UIViewController調用它時,'Receiver()沒有標識符爲'NewViewController''的segue。由於標識符是正確添加和在其他viewControllers –

相關問題