【问题标题】:Not able to identify the subview of TableView in Iphone无法识别 Iphone 中 TableView 的子视图
【发布时间】:2013-11-11 10:29:31
【问题描述】:

我添加了一个带有按钮和图像视图的标题视图,但我无法识别图像视图以更改图像。

我的代码如下:

我的 HaderView 类 .h 文件 (HeaderSection.h)

#import <UIKit/UIKit.h>

@interface HeaderViewSection : UIView
@property(nonatomic,retain)UIButton *btn;
@property(nonatomic,retain)UIImageView *arrow_img;
@end

我的 HaderView 类 .m 文件 (HeaderSection.m)

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor brownColor];

        _arrow_img=[[UIImageView alloc]init];
        _arrow_img.frame=CGRectMake(280, 15, 20, 20);
        _arrow_img.image=[UIImage imageNamed:@"Arrow_down_section.png"];
        _arrow_img.backgroundColor=[UIColor clearColor];
        [self addSubview:_arrow_img];

        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(20, 15, 35, 25);
        [btn setTitle:@"R" forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor blackColor];
        [self addSubview:btn];

    }

    return self;
}

.h 文件

#import "HeaderViewSection.h"
@property (nonatomic,retain)HeaderViewSection *header_view;

.m 文件

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    _header_view = [[HeaderViewSection alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    _header_view.tag = section+5000;
    _header_view.btn.tag = section+5000;
    [_header_view.btn addTarget:self action:@selector(expandTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
    _header_view.arrow_img.tag=section+2000;

    return _header_view;

}

-(void)expandTableHeaderView:(id)sender{

    [self.tblView beginUpdates];
    NSInteger section = [sender tag];
     UIView *view = (UIView *)[_header_view viewWithTag:[sender tag]]; //RETURNS nil
        NSArray *arr = [view subviews]; //RETURNS nil

}

我不知道为什么会这样?请帮我解决这个问题。

【问题讨论】:

  • @DivineDesert,你想要什么?
  • 好的,我明白你的问题了,@C_X 的回答解决了你的问题吗?
  • @DivineDesert,没有没有解决,只是看看我更新的问题。
  • 您在 [sender tag] 中得到的内容
  • -(void)expandTableHeaderView:(id)sender{} 只有在点击按钮时才会调用此方法。那么为什么你可以通过调用 [sender superView] 来获得 headerView;并且可以更改该 headerView 的大小

标签: ios iphone objective-c uitableview tags


【解决方案1】:

在此方法中,您将创建新的 headerView。现在这个时候headerView中已经没有subView了。 您正在设置 headerView.btn.tag 但您没有在 headerView 中添加按钮,图像也是如此。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    _header_view = [[HeaderViewSection alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    _header_view.tag = section+5000;
UIButton *button = [UIButton alloc] init];//set frame also
button.tag = section+5000;
 [button addTarget:self action:@selector(expandTableHeaderView:)  forControlEvents:UIControlEventTouchUpInside];
[header_view addSubView:button];

   //Do same like image view

}

编辑: 因为-(void)expandTableHeaderView:(id)sender{ } 方法只会在您点击按钮时调用。 Button 是 headerView 的子视图。您可以通过像这样调用其superView 方法来获得标题视图

UIView *view = (UIView*)[sender superView];
view.frame = // new frame

现在视图是你的 headerView,你可以改变它的框架来扩展或者你想要的任何东西。 你可以像这样得到它的子视图

NSArray *array = [view subViews];
for(UIView *v in array){
if([v isKindOfClass:[UIImage class]]){
//v is imageview change image if you want
} 

【讨论】:

  • 查看我更新的问题,因为我已经在单独的头类文件中添加了 btn 和 imageview
  • _header_view.tag = section+5000; _header_view.btn.tag = 部分+5000;您正在为视图和按钮设置相同的标签,这可能会导致冲突。更改为 headerView.tag 和 btn.tag 设置不同的标签
猜你喜欢
  • 2015-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多