【问题标题】:Header image not resizable in expandable table可扩展表格中的标题图像无法调整大小
【发布时间】:2013-01-31 16:05:15
【问题描述】:

我是 Xcode 的新手,这个简单的问题让我发疯了!我创建了一个可以正常工作的可扩展表。这是 UIView 子类中的一些代码,用于在您点击单元格时展开的部分:

- (id)initWithFrame:(CGRect)frame WithTitle: (NSString *) title Section:(NSInteger)sectionNumber delegate: (id <SectionView>) Delegate
{
self = [super initWithFrame:frame];
if (self) {

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(discButtonPressed:)];
    [self addGestureRecognizer:tapGesture];

    self.userInteractionEnabled = YES;

    self.section = sectionNumber;
    self.delegate = Delegate;

    CGRect LabelFrame = CGRectMake(100, 100, 100, 100);
    LabelFrame.size.width -= 100;
    CGRectInset(LabelFrame, 1.0, 1.0);

    //BUTTON 
    CGRect buttonFrame = CGRectMake(LabelFrame.size.width, 0, 100, LabelFrame.size.height);
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = buttonFrame;
    //[button setImage:[UIImage imageNamed:@"carat.png"] forState:UIControlStateNormal];
    //[button setImage:[UIImage imageNamed:@"carat-open.png"] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(discButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    self.discButton = button;

    //My IMAGE
    NSString *imageName = @"gradient1.png";
    UIImage *myImage = [UIImage imageNamed:imageName];
    UIImageView *sectionHeaderView = [[UIImageView alloc] initWithImage:myImage];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
    imageView.frame = CGRectMake(20,50,100,100);
    [self addSubview:sectionHeaderView];
    self.headerBG = sectionHeaderView;

    //HEADER LABEL
    UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(22, 12, sectionHeaderView.frame.size.width, 35.0)];
    label.textAlignment = NSTextAlignmentLeft;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.shadowColor = [UIColor darkGrayColor];
    label.shadowOffset = CGSizeMake(0.0, -1.0);
    label.text = title;
    label.font = [UIFont fontWithName:@"AvenirNext-Bold" size:20.0];
    sectionHeaderView.backgroundColor = [UIColor clearColor];
    //label.textAlignment = UITextAlignmentLeft;
    [self addSubview:label];
    self.sectionTitle = label;
}

return self;
}

我在单元格 @"gradient1.png" 上有一个自定义图像,但我似乎无法调整它的大小?这是 UITableViewController 中的头代码:

    - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:   (NSInteger)section
  {
SectionInfo *array  = [self.sectionInfoArray objectAtIndex:section];
if (!array.sectionView)
{
    NSString *title = array.category.name;
    array.sectionView = [[SectionView alloc] initWithFrame:CGRectMake(0, 10, self.tableView.bounds.size.width, 0) WithTitle:title Section:section delegate:self];
}
return array.sectionView;
}

对不起,如果这是一个微不足道的问题,非常感谢您的帮助!

【问题讨论】:

    标签: ios iphone xcode uitableview expandable


    【解决方案1】:

    我没有看到您尝试调整图像大小的位置,因此我无法提供任何帮助来说明为什么它没有调整大小,但我发现这令人困惑:

    //My IMAGE
        NSString *imageName = @"gradient1.png";
        UIImage *myImage = [UIImage imageNamed:imageName];
        UIImageView *sectionHeaderView = [[UIImageView alloc] initWithImage:myImage];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
        imageView.frame = CGRectMake(20,50,100,100);
        [self addSubview:sectionHeaderView];
        self.headerBG = sectionHeaderView;
    

    在这部分代码中,您创建了两个图像视图,然后调整一个的大小并将另一个添加到单元格(我假设是单元格)子视图中。您是否可能正在尝试调整从未添加为单元格子视图的视图的大小?

    此外,调整大小应该在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 内部进行。

    确保您调整了正确的视图大小,十确保您在正确的位置调整了它的大小。

    【讨论】:

    • 非常感谢!我试图调整不在单元格中的 imageView 框架的大小,非常愚蠢
    • 你能帮我解决另一个问题吗,还是很简单的......我怎样才能为不同的标题提供不同的图片?
    • 你有你的数组,你正在填充你的表格视图。只需根据该数组中的某些内容执行条件语句。说如果我的数据说什么UIImage *myImage = [UIImage imageNamed:@"someImage];,否则如果我的数据说什么UIImage *myImage = [UIImage imageNamed:@"somethingElse];
    • 再次感谢,找了个变量,然后看到sectionNumber,很明显!
    猜你喜欢
    • 2020-05-10
    • 2019-05-08
    • 1970-01-01
    • 2021-05-09
    • 2011-07-30
    • 2020-02-01
    • 2015-03-05
    • 2012-10-17
    相关资源
    最近更新 更多