2013-10-24 57 views
1

在我cellForRowAtIndexPath:方法,我有以下代码:为什么我的UIButton没有被添加到我的单元格中?

 UIButton *signOutButton = [[UIButton alloc] init]; 

     [signOutButton addTarget:self action:@selector(logoutButtonTapped) forControlEvents:UIControlEventTouchUpInside]; 
     signOutButton.translatesAutoresizingMaskIntoConstraints = NO; 
     signOutButton.titleLabel.textColor = [UIColor colorWithRed:0/255.0 green:180/255.0 blue:35/255.0 alpha:1.0]; 
     signOutButton.titleLabel.text = @"Sign Out"; 

     [cell.contentView addSubview:signOutButton]; 

     [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:signOutButton 
                    attribute:NSLayoutAttributeLeading 
                    relatedBy:NSLayoutRelationEqual 
                     toItem:cell.contentView 
                    attribute:NSLayoutAttributeLeft 
                    multiplier:1.0 
                     constant:50.0]]; 

     [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:signOutButton 
                    attribute:NSLayoutAttributeTrailing 
                    relatedBy:NSLayoutRelationEqual 
                     toItem:cell.contentView 
                    attribute:NSLayoutAttributeRight 
                    multiplier:1.0 
                     constant:50.0]]; 

     [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:signOutButton 
                    attribute:NSLayoutAttributeBottom 
                    relatedBy:NSLayoutRelationEqual 
                     toItem:cell.contentView 
                    attribute:NSLayoutAttributeBottom 
                    multiplier:1.0 
                     constant:-6.0]]; 

但它从来没有显示出来,当我加载细胞。我对自动布局有点新鲜,所以任何人都可以弄清楚我做错了什么?

奇怪的是,如果我点击按钮应该执行的区域。

+0

添加例外断点所有异常并运行你的应用程序,如果在你的自动布局规则的一些冲突,应停止执行,你应该能够理解什么是错 – Manu

回答

3

由于该方法在您按下时执行,因此您的按钮似乎实际上已添加到单元格中。我认为标题没有设置。试着用 [signOutButton setTitle:@"Sign Out" forState:UIControlStateNormal]

+1

您还需要使用' - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state'来更改文本颜色 – slecorne

1

你的按钮替换 signOutButton.titleLabel.text = @"Sign Out" 是越来越增加,因为你的标签标题是没有得到设置,因此你没有看到它。你可以改变背景颜色,看看是不是在小区:

[signOutButton setBackgroundColor:[UIColor blueColor]]; 

,然后更改标题和颜色如下:

[signOutButton setTitle:@"Sign Out" forState:UIControlStateNormal]; 
[signOutButton setTitleColor:[UIColor colorWithRed:0/255.0 green:180/255.0 blue:35/255.0 alpha:1.0] forState:UIControlStateNormal]; 

这是你最后的代码是怎么回事的样子:

UIButton *signOutButton = [[UIButton alloc] init]; 

[signOutButton addTarget:self action:@selector(logoutButtonTapped) forControlEvents:UIControlEventTouchUpInside]; 
signOutButton.translatesAutoresizingMaskIntoConstraints = NO; 
[signOutButton setTitle:@"Sign Out" forState:UIControlStateNormal]; 
[signOutButton setTitleColor:[UIColor colorWithRed:0/255.0 green:180/255.0 blue:35/255.0 alpha:1.0] forState:UIControlStateNormal]; 
[signOutButton setBackgroundColor:[UIColor blueColor]]; //just for testing, you can get rid of this line 

[cell.contentView addSubview:signOutButton]; 

[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:signOutButton 
                  attribute:NSLayoutAttributeLeading 
                  relatedBy:NSLayoutRelationEqual 
                   toItem:cell.contentView 
                  attribute:NSLayoutAttributeLeft 
                  multiplier:1.0 
                   constant:50.0]]; 

[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:signOutButton 
                  attribute:NSLayoutAttributeTrailing 
                  relatedBy:NSLayoutRelationEqual 
                   toItem:cell.contentView 
                  attribute:NSLayoutAttributeRight 
                  multiplier:1.0 
                   constant:50.0]]; 

[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:signOutButton 
                  attribute:NSLayoutAttributeBottom 
                  relatedBy:NSLayoutRelationEqual 
                   toItem:cell.contentView 
                  attribute:NSLayoutAttributeBottom 
                  multiplier:1.0 
                   constant:-6.0]]; 
相关问题