UIButton不会改变选定状态
问题描述:
我想做一个简单的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;
}
答
你的每一个细胞被离队时添加子视图为您喜欢按钮,而不仅仅是更新按钮的状态。在这种情况下,一个好的做法是将UITableViewCell类继承并初始化它,从tableView:cellForRowAtIndexPath:
更新按钮的状态。 虽然这会要求您将按钮状态存储为数据源(并在您的likeButtonPressed:
方法中更新它们)。
+0
这是一个不错的方法。谢谢。 – alvarolopez 2014-08-28 20:23:48
也许你应该正确的图像设置为按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