UIButton在选定状态下不会改变

时间:2014-08-27 20:02:02

标签: ios objective-c 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;

}

1 个答案:

答案 0 :(得分:2)

每次单元格出列时,您都会为喜欢按钮添加子视图,而不仅仅是更新按钮的状态。在这种情况下,一个好的做法是继承UITableViewCell类并在那里初始化它,从tableView:cellForRowAtIndexPath:更新按钮的状态。 虽然这需要您将按钮状态存储在其他地方作为数据源(并在likeButtonPressed:方法中更新它们)。