【问题标题】:UIButton Not Working inside UItableViewCellUIButton 在 UItableViewCell 内不起作用
【发布时间】:2011-07-12 18:16:18
【问题描述】:

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

这是我的问题:

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

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

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

    }

}

以及页脚视图(以防有人需要查看):

 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];

【问题讨论】:

    标签: objective-c ios uiview uitableview uibutton


    【解决方案1】:

    你需要实现类似的东西

    - (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;
    }
    

    【讨论】:

    • @paul:我也已经解决了身高问题。它比我对该部分的子视图大。问题仍未解决。
    • footerView 的框架设置为?
    • FML。谢啦。其实我用过... self.footerView = [[UIView alloc] init.. 把它改成... self.footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)]; 成功了。
    • @Paul 和@Deepak:我现在将发布一个新问题,这是我面临的一个非常严重的问题,与这个问题有关。如果可能,请帮助我!我将在接下来的 10 分钟内发布。
    • 如果你没有使用ARC并且你合成了你的footerview属性会导致内存泄漏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多