【问题标题】:UITableView footerView with button, button doesn't work带有按钮的UITableView footerView,按钮不起作用
【发布时间】:2011-04-28 17:34:42
【问题描述】:

我正在尝试为我的 UITableView footerView 创建一个自定义视图,其中包含一个按钮。我可以在那里看到它,但是当我触摸它时,什么也没有发生……你能看到这里有什么问题吗:

- (void)viewDidLoad {
    [super viewDidLoad];

    if(self.tableView.tableFooterView == nil) {
        //allocate the view if it doesn't exist yet
        UIView *footerView  = [[UIView alloc] init];

        //create the button
        UIButton *addNewBtn = [UIButton buttonWithType:UIButtonTypeCustom];     
        //the button should be as big as a table view cell
        [addNewBtn setFrame:CGRectMake(0, 0, 320, [AddMerchantTVC cellHeight])];

        //set title, font size and font color
        [addNewBtn setTitle:@"Add New" forState:UIControlStateNormal];
        [addNewBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
        [addNewBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [addNewBtn addTarget:self action:@selector(addNew:) forControlEvents:UIControlEventTouchUpInside];

        UIImageView *cellGradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellGradient.png"]];
        cellGradient.frame = CGRectMake(0, 54, 320, 16);
        [footerView addSubview:cellGradient];

        //add the button to the view
        [footerView addSubview:addNewBtn];
        footerView.userInteractionEnabled = YES;
        self.tableView.tableFooterView = footerView;
        self.tableView.tableFooterView.userInteractionEnabled = YES;

        [footerView release];
        [cellGradient release];
}

- (void)addNew:(id)sender {
    // This is never called
    NSLog(@"Touched.");
}

【问题讨论】:

    标签: iphone uitableview footer


    【解决方案1】:

    创建 UIView 的子类并包含以下方法:

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        return [super hitTest:point withEvent:event];
    }
    
    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
        return [super pointInside:point withEvent:event];
    }
    

    【讨论】:

    • 我真的需要 UIView 的子类来完成这个吗?如果是这样,那就很烦人了。
    【解决方案2】:

    您没有设置 footerView 的框架。将 footerView 的框架设置为至少与按钮一样高,否则触摸不会传递到按钮。

    【讨论】:

    • 当我实现- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; 来指定页脚高度时,这对我有用。
    【解决方案3】:

    您需要在调用 viewForFooterInSection 之前设置 tableView 的 sectionFooterHeight 属性(不能在 viewForFooterInSection 方法中进行)。

    【讨论】:

    • 特别是如果你使用XIB构建你的UIView,你需要实现这个功能
    【解决方案4】:
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
        return 50.0f;
    }
    

    把这个方法放在你的课堂上,你的问题就会解决.....

    【讨论】:

      【解决方案5】:

      它对我有用,我只是添加

      footerView.frame =CGRectMake(0, 300, 100, 100);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-08
        相关资源
        最近更新 更多