首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xib中来自自定义TableCell的TableCell

xib中来自自定义TableCell的TableCell
EN

Stack Overflow用户
提问于 2014-04-07 05:21:01
回答 7查看 17.6K关注 0票数 12

我已经创建了自定义的tableView控制器,在单元格中我放置了一个按钮来打开设备照片库。我的问题是,我无法从CustomCell.m打开imagePickerController,它的错误显示在下面。

请给我点主意来解决我的问题。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2014-04-07 05:31:46

TableViewCell是一个视图,您不能present上的视图,而是UIViewController可以处理它。您应该将控件从单元格转移到包含表视图并为其创建自定义单元格的控制器。

就像这样:

自定义单元格.h类:

代码语言:javascript
复制
@protocol changePictureProtocol <NSObject>
-(void)loadNewScreen:(UIViewController *)controller;
@end

@property (nonatomic, retain) id<changePictureProtocol> delegate;

然后是Synthesize it in.m。

将其添加到m文件中:

代码语言:javascript
复制
-(IBAction)changePicture:(id)sender
{
    // ..... blah blah
    [self.delegate loadNewScreen:picker];
}

加载此单元格的视图控制器:

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // create cell here

   cell.delegate = self;
}

-(void)loadNewScreen:(UIViewController *)controller;
{
  [self presentViewController:controller animated:YES completion:nil];
}

这是给你一个想法的密码。

编辑:

Swift等值:

CustomTableViewCell.swift代码:

代码语言:javascript
复制
protocol ChangePictureProtocol : NSObjectProtocol { 
    func loadNewScreen(controller: UIViewController) -> Void;  
}

class CustomTableViewCell: UITableViewCell {

    // Rest of the class stuff

    weak var delegate: ChangePictureProtocol?

    @IBAction func changePicture(sender: AnyObject)->Void
    {
        var pickerVC = UIImagePickerController();
        if((delegate?.respondsToSelector("loadNewScreen:")) != nil)
        {
           delegate?.loadNewScreen(pickerVC);
        }  
    }
}

ViewController.swift代码:

代码语言:javascript
复制
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as CustomTableViewCell!

    cell.delegate = self;

    return cell;
}

func loadNewScreen(controller: UIViewController) {
    self.presentViewController(controller, animated: true) { () -> Void in

    };
}
票数 32
EN

Stack Overflow用户

发布于 2015-01-02 11:40:51

提出委托或实例变量的答案是正确的,但是,在大多数情况下,使用特殊视图控制器表示新控制器并不重要。在这些情况下,以下解决方案要简单得多:只需使用应用程序根视图控制器:

代码语言:javascript
复制
UIViewController* activeVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[activeVC presentViewController:'new view controller'
                       animated:YES
                     completion:NULL];
票数 11
EN

Stack Overflow用户

发布于 2014-04-07 05:24:52

presentViewController:消息存在于视图控制器中。请将控件从单元格委托给viewController,并使用同一行将解决问题。UITableViewCell不响应presentViewController:消息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22904164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档