如何将uiviewcontroller的视图作为内容视图添加到uitableviewcell? [英] How to add uiviewcontroller's view as content view to uitableviewcell?

查看:905
本文介绍了如何将uiviewcontroller的视图作为内容视图添加到uitableviewcell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个uiviewcontroller的视图,它有一个按钮和几个标签作为uitableviewcell的内容视图。

I want to add a uiviewcontroller's view which has a button and few labels as a content view of a uitableviewcell.

虽然我可以添加按钮动作导致崩溃。

Though i am able to add but the button action causes a crash.

MyViewController *controller = [[MyViewController alloc] initwithnibname:@"MyView" bundle:nil];
[cell.contentview addsubview:controller.view];
[controller release]; // if i comment out this part the button action is received by my view controller.

但是从视图中删除后会出现内存泄漏。我没有调用myviewcontroller的dealloc。

However there are memory leaks when its removed from view. The dealloc of myviewcontroller is not called.

这样做的正确方法是什么?

What is the correct way to do this?


  1. 将视图添加到uitableview单元格
    ,其中包含一个按钮,由
    视图控制器处理

  1. Add a view to a uitableview cell which has a button and is handled by the viewcontroller

当视图超出范围时,如何确保释放内存

How to assure memory is released when the view goes out of scope?

TIA,
Praveen

TIA, Praveen

推荐答案

我认为问题在于,您正在发布控制器并只使用其超级视图保留的子视图。动作模式需要一个我认为是已释放控制器的目标。如果你只需要它的视图,为什么要释放你的viewController?保留它并通过属性保持对它的引用。

I think the problem is, that you are releasing the controller and just using the subview which is retained by its superview. The action pattern needs a target which I assume is the released controller. And why should you release your viewController if you only need the view of it? Retain it and keep a reference through a property to it.

我将子视图添加到tableview单元格的方法是在UITableViewCell的子类中。假设你有一个 UITableViewCell 的子类,比如说 ButtonTableViewCell 。单元格的init创建并向您的单元格添加一个UIButton,并将其很好地放入其contentView中。 Decalre引用该按钮的属性。喜欢 UIButton * myButton 。在cellForRowAtIndexPath中应该做的是这样的:

My way of adding subviews to a tableview cell would be in a subclass of UITableViewCell. Let's assume you are having a subclass of UITableViewCell, say ButtonTableViewCell. The init of the of cell creates and adds a UIButton to your cell and puts it nicely in its contentView. Decalre a property which references to the button. Like UIButton *myButton. What should be done in the cellForRowAtIndexPath is something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyButtonCell"];           
    if (cell == nil) {
        cell = [[ButtonTableViewCell alloc] initWithReuseIdentifier:@"MyButtonCell"];
    }
    [cell.myButton addTarget:self action:@selector(onDoSomething) forControlEvents:UIControlEventTouchUpInside];

    // Do more cell configuration...
    return cell;
}

我已经组成了初始化器 initWithReuseIdentifier 可以轻松实现。

I've made up the initializer initWithReuseIdentifier which can be easily implemented.

您可以通过 viewDidUnload 方法://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html\"rel =nofollow> UIViewController 和dealloc方法。

You assure release of memory with the viewDidUnload method of the UIViewController and the dealloc method.

这篇关于如何将uiviewcontroller的视图作为内容视图添加到uitableviewcell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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