是否有可能从UIView上的UITableViewCell转到另一个视图 [英] is it possible to segue from a UITableViewCell on a UIView to another view

查看:106
本文介绍了是否有可能从UIView上的UITableViewCell转到另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xcode 4.6.1 iOS 6使用故事板

Xcode 4.6.1 iOS 6 using storyboards

我的问题是这个

我有一个UITableView UIViewController中的UIView上的动态原型单元(它本身嵌入在导航控制器中),我想从一个特定的单元格转到另一个视图

I have a UITableView with dynamic prototype cells on a UIView in a UIViewController (that is itself embedded in a navigation controller) and I want to segue from one specific cell to another view

(在任何人建议之前)我应该只是使用UITableViewController,我在UIView上有其他东西,所以我出于某种原因设置这种方式。)

(Before anyone suggests I should just be using a UITableViewController , I do have other things on the UIView, so i'm set up this way for a reason.)

现在我不是确定如何创建segue

Now i'm not sure how to go about creating the segue

如果我从原型UITableViewCell拖动创建一个segue,所有生成的单元格会自动调用segue - 当我只需要一个这样做。这是正常的行为,如果我通过从UITableViewController拖动创建segue并调用[self performSegueWithIdentifier:....来自我的didSelectRowAtIndexPathMethod,因此只有我想要执行此segue的特定单元格触发它,我会解决这个问题。

If I drag from the prototype UITableViewCell to create a segue , all the generated cells automatically call the the segue - when i need only one to do so. This is normal behaviour and I would get around this if i was using a UITableViewController by creating the segue by dragging from UITableViewController and calling [self performSegueWithIdentifier:.... From my didSelectRowAtIndexPathMethod so only the specific cell I want to perform this segue triggers it.

在这种情况下我没有UITableViewController - 只是UIView上的UITableView,它是UIViewController子类的一部分

I don't have a UITableViewController in this case - just my UITableView on a UIView that is part of a UIViewController subclass

我一直在玩,我刚刚发现我无法从UITableView拖动 - 不允许你这样做,所以这是一个deadend。

I've been playing around and I have just discovered that i cannot drag from the UITableView - doesn't let you do that, so that was a deadend.

我唯一的选择似乎是从UIViewController中拖出

My only choice that seemed left to me was to drag from the UIViewController

所以我试过了,当然XCode在执行segue行告诉错误我有...没有'LocationTV'的可见界面声明选择器performSegueWithIdentifier。 LocationTv是我的tableview子类。

So i tried that and of course XCode throws up an error on the perform segue line telling me i have ... No visible interface for 'LocationTV' declares the selector performSegueWithIdentifier. LocationTv being my tableview subclass.

在这种情况下尝试调用新视图的正确方法是什么

What is the correct way to attempt to call the new view in this situation

感谢

Simon

推荐答案

我的解决方案

我最终使用委托模式

我从我的UIViewController中拖出了一个segue - 具体来说从viewController图标拖动(带有白色方块的橙色圆圈 - 从故事板中视图下方的名称栏 - 尽管您也可以从侧边栏拖动)到我想要转到的视图。

I made a segue dragging from the my UIViewController - specifically dragging from the viewController icon (the orange circle with a white square in it - from the name bar thats under the view in the storyboard - although you could also drag from the sidebar ) to the view that i wanted to segue to.

我需要从表视图上的表格视图单元格中触发此segue。

I needed to trigger this segue from a table view cell on a table view.

TableView位

所以我在tableview头文件中声明了一个协议 - 名为LocationTV.h - 如下所示

So i declared a protocol in my tableview header file - which is called LocationTV.h - as follows

@protocol LocationTVSegueProtocol <NSObject>

-(void) makeItSegue:(id)sender;

@end

下面我声明一个属性来保存我的代表

Below that I declare a property to hold my delegate

@property (nonatomic, strong) id<LocationTVSegueProtocol> makeSegueDelegate;

要实际触发segue,我在didSelectRowAtIndexPath方法中的makeSequeDelegate上调用了makeItSegueMethod

To actually trigger the segue i called the makeItSegueMethod on my makeSequeDelegate in my didSelectRowAtIndexPath method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

switch (indexPath.section) {
        DLog(@"selected row %d",indexPath.row);
    case dLocation:
    {
        if(indexPath.row == 2){

            [_makeSegueDelegate makeItSegue:self];

        } else if (indexPath.row == 7){

UIViewController位

并将我的UIViewController(名为MultiTableHoldingVC)设置为实现该协议

and set up my UIViewController (named MultiTableHoldingVC) as implementing that protocol

@interface MultiTableHoldingView : UIViewController    
                                  <EnviroTVProtocol,LocationTVSegueProtocol> {

}

下面我在列表中声明了协议方法我的类方法(虽然我不确定是否有必要,因为编译器应该知道该方法,因为实现协议的decalration本质上是实现此方法的承诺)

Below that i declared the protocol method in the list of my classes methods (although i'm not sure that is necessary as the compiler should know about the method as the decalration of implementing a protocol is essentially a promise to implement this method)

-(void) makeItSegue:(id)sender;

然后在我的UIViewController的实现文件中,我写了一个基本上只调用preformSegueWithIdentifier的方法

And then over in the implementation file of my UIViewController i wrote the method which essentially just calls preformSegueWithIdentifier

-(void) makeItSegue:(id)sender{ 
    [self performSegueWithIdentifier:@"ChooseCountryNow" 
                              sender:sender];   
}

并将它们全部链接在一起,如我在头文件中声明的那样tableView的实例如下:

And to link it all together,as in the header file I had declared my instance of the tableView as follows

@property (strong, nonatomic) IBOutlet LocationTV *dsLocationTV;

我必须将表视图委托属性设置为自我 - 这是我在UIViewControllers中所做的 - ( void)ViewDidLoad方法

I had to set that tables views delegate property to be self - which I did in my UIViewControllers -(void)ViewDidLoad method

_dsLocationTV.makeSegueDelegate = self;

这看起来有点像调用方法来调用方法并且allprog建议更简单(我不能为我的生活弄清楚为什么它为我抛出错误)但这很好用。感谢allprog和danypata的建议。

It all seems a bit of a kludge calling a method to call a method and allprog suggestion is simpler (I cant for the life of me work out why it threw up errors for me) but this works just fine . Thanks to both allprog and danypata for their suggestions.

希望这对那里的人有帮助

Hope this is helpful to someone out there

这篇关于是否有可能从UIView上的UITableViewCell转到另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆