UIButton 在 UItableViewCell 中不起作用 [英] UIButton Not Working inside UItableViewCell

查看:89
本文介绍了UIButton 在 UItableViewCell 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由一个标签和两个按钮组成的视图,我将其命名为 UIView *footerView,因为我在表视图的最后一部分中实现了它.我使用 [cell.contentView addSubview: footerView]

I have a view consisting of a label and two buttons, which I named UIView *footerView, as I am implementing this in the last section of my table view. I have this view become the view for indexPath.section = 3 by using [cell.contentView addSubview: footerView]

这是我的问题:

我可以看到单元格内部的视图.但我无法点击按钮,因为选择的是单元格而不是按钮(位于单元格内).

I can see the view inside the cell. But I cannot tap the buttons, as the cell gets selected instead of the buttons (which are inside the cells).

我该如何克服这个问题?我的按钮甚至无法选择!

How do I get over this problem? My buttons are not even selectable!

这是代码...在 cellForRowAtIndexPath:

if (section == 3){

    cell = nil;
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    if (indexPath.row ==0){

        cell.contentMode = UIViewContentModeScaleToFill;
        [cell.contentView addSubview:self.footerView];

    }

}

和页脚视图(以防有人需要看到这个):

And the Footer view (in case anyone needs to see this):

 UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 3, 300, 44)];
textView.text = @"Terms and Conditions: Lorem ipsum dolor sit amet, con- sectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut";

textView.backgroundColor = [UIColor clearColor];
textView.font = [UIFont systemFontOfSize:10];

[self.footerView addSubview:textView];

//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[button setFrame:CGRectMake(0, 60, 300, 44)];

//set title, font size and font color
[button setTitle:@"Create Account" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
// [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

//set action of the button
[button addTarget:self action:@selector(createAccount:)
 forControlEvents:UIControlEventTouchUpInside];

[button setEnabled:YES];
//add the button to the view
[self.footerView addSubview:button];


UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[cancelButton setFrame:CGRectMake(0, 107, 300, 44)];

//set title, font size and font color
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
[cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
// [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

//set action of the button
[cancelButton addTarget:self action:@selector(cancelPressed:)
       forControlEvents:UIControlEventTouchUpInside];


[cancelButton setEnabled:YES];
//add the button to the view
[self.footerView addSubview:cancelButton];

推荐答案

你需要实现类似的东西

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (3 == indexPath.section){
      return nil;
    }
    return indexPath;
}

好的,如果我执行上述操作,它就可以正常工作.我相信我可以通过使 UIButton 框架落在单元格的 contentView 之外来重现您的问题.确保单元格足够大以包含您的标签并且它应该可以正常工作.要使单元格更大以适合您的内容,您可能需要实施:

Ok so if I implement the above it works fine. I believe I can reproduce your issue by making the UIButton frame fall outside the cell's contentView. Make sure the cell is big enough to contain your label and it should function correctly. To make the cell larger to fit your content you may have to implement:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return aHeightThatIsBigEnoughToContainMySubViews;
}

这篇关于UIButton 在 UItableViewCell 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆