2013-10-24 58 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]]; 
相關問題