繁体   English   中英

UITableViewCell子类-在IBAction上添加子视图

[英]UITableViewCell Subclass - Adding a subview on IBAction

当试图触发视图内的UITableViewCell的按钮时,我试图向UIViewController的视图添加子视图。 我面临的问题是从表视图单元格的子类调用父视图。

您可以在视图层次结构中查找父tableView

-(UITableView*) parentTableView
{
    UIView* v = [self superview];
    while (v && ![v isKindOfClass:[UITableView class]]) {
        v = [v superview];
    }
    return v;
}

创建tableviewcell子类时,通过制作自己的init方法,将其传递给父视图实例的指针。 然后将该指针作为实例变量保存在子类中。 这样,当子类中发生某些事情时,您可以对父级执行任何操作。 让我知道您是否需要更多帮助。

我建议为UITableViewCells设置UIViewController的委托。 您必须创建一个协议并使UIViewController实现它。 此外,您需要UITableViewCell以便添加委托属性。

#import <Foundation/Foundation.h>

@protocol YourTableViewCellDelegate

  - (void)didTouchButton:(UIButton *)theButton;

@end

UIButton的action方法中,检查委托的存在:

if (self.delegate != nil && [self.delegate respondsToSelector:@selector(didTouchButton:)]) {
    [self.delegate performSelector:@selector(didTouchButton:) withObject:_theButton];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM