我想做一個簡單的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;
}
也許你應該正確的圖像設置爲按UIControlStateHighlighted狀態按下按鈕 – kjhkjhkjh 2014-08-27 20:06:03
@ 0Silencer我不明白UIControlStateHighlighted與UIControlStateSelected有什麼關係 – alvarolopez 2014-08-27 20:08:28
您的按鈕有一個標題?如果不檢查這個問題:http://stackoverflow.com/questions/19198858/uibuttons-selected-state-not-working-in-ios7 – 2014-08-27 20:09:19