objective-c - UIButton 在 UItableViewCell 内不工作

标签 objective-c ios uiview uitableview uibutton

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

将此 View 变为 indexPath.section = 3 的 View

这是我的问题:

我可以看到牢房内的景色。但我无法点击按钮,因为单元格被选中,而不是按钮(位于单元格内部)。

如何解决这个问题? 我的按钮甚至无法选择!

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

    }

}

以及页脚 View (以防有人需要看到它):

 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 之外来重现您的问题。确保单元格足够大以容纳您的标签并且它应该正常工作。要使单元格更大以适合您的内容,您可能必须实现:

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

关于objective-c - UIButton 在 UItableViewCell 内不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6669027/

相关文章:

ios - 从聊天室中过滤掉不合适的词

ios - 从另一个 ViewController Swift 4 访问对象(SQLite 表)

iphone - 如何使用父 View Controller 启动 uiactionsheet 使其不被剪切?

iphone - UIImageView 单元格 contentView 的 View 之一未与其父 View 一起扩展

ios - 无法在 Swift 中的自定义 UIView 上布局 subview

iphone - 如何在 UIView 中删除 NSNotification 的观察者?

objective-c - PFUser 在 fetchAllIfNeeded 之后调用 objectForKey 时抛出 NSInternalInconsistencyException

ios - 如何在 Objective-C 中将 __NSCFString 转换为 NSData

iphone - 整个 View 中的 UIGestureRecognizer

ios - UIView 角半径在小屏幕上很锐利