2014-08-27 98 views
0

我想做一個簡單的UIButton,每次單擊它時都會更改背景圖像。這是代碼:UIButton不會改變選定狀態

UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[likeButton addTarget:self 
       action:@selector(likeButtonPressed:) 
    forControlEvents:UIControlEventTouchUpInside]; 
[likeButton setBackgroundImage:[UIImage imageNamed:@"icon-like-grey.png"] forState:UIControlStateNormal]; 
[likeButton setBackgroundImage:[UIImage imageNamed:@"icon-dislike-color.png"] forState:UIControlStateSelected]; 

- (IBAction)likeButtonPressed:(UIButton *)sender { 
    if (sender.isSelected) { 
     [sender setSelected:NO]; 
     NSLog(@"not selected"); 
    } else { 
     [sender setSelected:YES]; 
     NSLog(@"selected"); 
    } 
} 

顯然它第一次被選中,並且按鈕從灰色變爲彩色。但是,它在第​​二次點擊後不會變回灰色。 NSLog顯示正確的代碼,所以問題必須在[sender setSelected: ]行。我究竟做錯了什麼?

編輯:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) 
{ 
    NSLog(@"in nil"); 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

cell.textLabel.text = @"Text"; 
cell.accessoryType = UITableViewCellAccessoryNone; 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:18]; 

     if (indexPath.row == 0) { 
      cell.textLabel.textAlignment = NSTextAlignmentCenter; 
      cell.textLabel.text = [NSString stringWithFormat:@"%@", [mainDelegate.imagesDescription objectAtIndex:indexPath.section]]; 

     } else if (indexPath.row == 1) { 
      NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", [mainDelegate.imagesURL objectAtIndex:indexPath.section]]]; 
      cell.accessoryType = UITableViewCellAccessoryNone; 

      SDWebImageManager *manager = [SDWebImageManager sharedManager]; 
      [manager downloadWithURL:url 
            options:0 
           progress:^(NSUInteger receivedSize, long long expectedSize) { 

           } 
           completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { 

            if (image && finished) { 
             cell.imageView.image = image; 
            } 
           } 
      ]; 

      UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
      [likeButton addTarget:self 
          action:@selector(likeButtonPressed:) 
       forControlEvents:UIControlEventTouchUpInside]; 

      [likeButton setBackgroundImage:[UIImage imageNamed:@"icon-like-grey.png"] forState:UIControlStateNormal]; 
      [likeButton setBackgroundImage:[UIImage imageNamed:@"icon-dislike-color.png"] forState:UIControlStateSelected]; 

      likeButton.frame = CGRectMake(215, 60, 40, 40); 
      likeButton.layer.borderWidth = 0; 
      cell.textLabel.text = @""; 

      [cell addSubview:likeButton]; 


     } else if (indexPath.row == 2) { 

      NSString *location = [mainDelegate.imagesLocation objectAtIndex:indexPath.section]; 
      cell.textLabel.text = location; 

      UILabel *labelRight = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 100, 100)]; 
      labelRight.textAlignment = NSTextAlignmentRight; 

      labelRight.text = [NSString stringWithFormat:@"%@ %@", [mainDelegate.imagesLikes objectAtIndex:indexPath.section], @"Likes"]; 
      cell.accessoryView = labelRight; 
     } 

return cell; 

} 
+2

也許你應該正確的圖像設置爲按UIControlStateHighlighted狀態按下按鈕 – kjhkjhkjh 2014-08-27 20:06:03

+0

@ 0Silencer我不明白UIControlStateHighlighted與UIControlStateSelected有什麼關係 – alvarolopez 2014-08-27 20:08:28

+0

您的按鈕有一個標題?如果不檢查這個問題:http://stackoverflow.com/questions/19198858/uibuttons-selected-state-not-working-in-ios7 – 2014-08-27 20:09:19

回答

2

你的每一個細胞被離隊時添加子視圖爲您喜歡按鈕,而不僅僅是更新按鈕的狀態。在這種情況下,一個好的做法是將UITableViewCell類繼承並初始化它,從tableView:cellForRowAtIndexPath:更新按鈕的狀態。 雖然這會要求您將按鈕狀態存儲爲數據源(並在您的likeButtonPressed:方法中更新它們)。

+0

這是一個不錯的方法。謝謝。 – alvarolopez 2014-08-28 20:23:48