【问题标题】:UIButtons on tableFooterView not responding to eventstableFooterView 上的 UIButtons 没有响应事件
【发布时间】:2010-11-10 11:19:55
【问题描述】:

我已成功将一些 UIButtons 添加到自定义 tableFooterView。问题是按钮事件没有被调用。此外,相对于 UIControlStateHighlighted 的图像不会出现。有任何想法吗? 我已经尝试了所有我能想到的,但我找不到诀窍。我在这里报告 UITableViewController 的有趣部分:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection: (NSInteger)section 
{
    CGRect footerFrame = CGRectMake(0.0, 0.0, 480, 190);
    _footerView = [[UIImageView alloc] initWithFrame: footerFrame];
    _footerView.image = [UIImage imageNamed:@"tableviewfooter.png"];
    _footerView.contentMode = UIViewContentModeTopLeft;

    UIButton *addExam = [self createButtonWithFrame: CGRectMake(10, 30, 460, 58) Target: self Selector: @selector(addNewItem:) Image: @"additem.png" ImagePressed: @"additem_pressed.png"];
    [_footerView addSubview:addExam];

    return _footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger) section
{
    return 190;
}


- (void) addNewItem:(id)sender
{
    // This get's never called
    NSLog(@"Pressed");
}


- (UIButton*) createButtonWithFrame: (CGRect) frame Target:(id)target Selector:(SEL)selector Image:(NSString *)image ImagePressed:(NSString *)imagePressed
{
    UIButton *button = [[UIButton alloc] initWithFrame:frame];

    UIImage *newImage = [UIImage imageNamed: image];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];

    UIImage *newPressedImage = [UIImage imageNamed: imagePressed];
    [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    return button;
}

感谢您的帮助!

【问题讨论】:

    标签: iphone cocoa-touch uitableview


    【解决方案1】:

    我不认为UIImageView 被设计为可以很好地用作其他视图的容器,尤其是UIButtons。其一,UIImageViews 在创建时,其userInteractionsEnabled 属性默认设置为false

    我将创建一个简单的UIView 作为您的页脚视图,然后将UIButtonUIImageView 直接添加到它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 2013-08-18
      相关资源
      最近更新 更多