UIButton在UItableViewCell中不起作用

Leg*_*las 2 objective-c uibutton uitableview uiview ios

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

这是我的问题:

我可以在单元格内看到视图.但我无法点击按钮,因为单元格被选中而不是按钮(在单元格内).

我该如何克服这个问题?我的按钮甚至不可选!

这是代码... In 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];

    }

}
Run Code Online (Sandbox Code Playgroud)

页脚视图(如果有人需要看到这个):

 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];
Run Code Online (Sandbox Code Playgroud)

Pau*_*l.s 6

您需要实现与此类似的功能

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

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return aHeightThatIsBigEnoughToContainMySubViews;
}
Run Code Online (Sandbox Code Playgroud)