如何设置在viewForHeaderInSection中添加的UIImageView图像并在另一个Method中设置

时间:2016-06-21 13:24:23

标签: objective-c uitableview uiimageview

如何设置UIImageView中添加的viewForHeaderInSection图像并在另一种方法中设置。

- (NSInteger)numberOfSectionsInTableView:(TQMultistageTableView *)mTableView
{
     return 4;
}

- (UIView *)mTableView:(TQMultistageTableView *)mTableView viewForHeaderInSection:(NSInteger)section;
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 90)];
    header.layer.backgroundColor    = [UIColor whiteColor].CGColor;
    header.layer.masksToBounds      = YES;

    UILabel *sectionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 110, 70)];
    sectionTitleLabel.text = [sectionTitleArray objectAtIndex:section];
    sectionTitleLabel.textAlignment = NSTextAlignmentCenter;
    sectionTitleLabel.font = [UIFont fontWithName:@"Helvetica-Neue" size:15];
    sectionTitleLabel.numberOfLines = 0;
    sectionTitleLabel.contentMode = NSLineBreakByWordWrapping;
    [header addSubview:sectionTitleLabel];

    UIImageView *locationImageView  = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 90, 0, 90, 90)];
    locationImageView.image = [locationImageArray objectAtIndex:section];
    locationImageView.backgroundColor = [UIColor brownColor];
    [header addSubview:locationImageView];

    UILabel *sectionLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, header.frame.size.height - 1, self.view.frame.size.width, 1)];
    sectionLineLabel.backgroundColor = [UIColor blackColor];
    [header addSubview:sectionLineLabel];

    discloserImageView  = [[UIImageView alloc] initWithFrame:CGRectMake(locationImageView.frame.size.width - 50, 0, 40, 40)];
    discloserImageView.image = [UIImage imageNamed:@"down-arrow.png"];
    discloserImageView.backgroundColor = [UIColor brownColor];
    discloserImageView.tag = section;
    [locationImageView addSubview:discloserImageView];


    return header;
}

- (void)mTableView:(TQMultistageTableView *)mTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didSelectRow ----%ld",(long)indexPath.row);
}

#pragma mark - Header Open Or Close

- (void)mTableView:(TQMultistageTableView *)mTableView willOpenHeaderAtSection:(NSInteger)section
{
    NSLog(@"Open Header ----%d",section);

//    UITableViewHeaderFooterView *header = [self mTableView:mTableView viewForHeaderInSection:section];

    UIView *headerView = [self mTableView:mTableView viewForHeaderInSection:section];
    [[[headerView subviews] objectAtIndex:1] subviews];
    NSLog(@"HeaderView :%@",headerView);

    for (UIImageView *imageView in [[[headerView subviews] objectAtIndex:1] subviews]) {
    if ([ imageView isKindOfClass:[UIImageView class]])
    {
        imageView.image = [UIImage imageNamed:@"up-arrow.png"];
    }
    }
}

1 个答案:

答案 0 :(得分:0)

在您的代码中,您的标头不是UIImageView,因此您的条件if ([header isKindOfClass:[UIImageView class]])永远不会成立。实际上,您将UIImageView添加为UIView的子视图。

你能做的是:

if ([[header subviews][0] isKindOfClass:[UIImageView class]])
{
    UIImageView *imageViewChange =(UIImageView *) imageView;
    imageViewChange.image = [UIImage imageName@"downArrow.png"];
} 

修改:

根据您的编辑,我认为您最好的选择是将UIImageView属性设为如下:

@interface YourViewController () {
    UIImageView *imageViewChange;
}

并使用它:

imageViewChange.image = [UIImage imageName@"downArrow.png"];