简体   繁体   中英

Button selection on BOOL UIButton

I am attempting to show a button in the selected state when based on a boolean off of JSON. Here is my code for my custom UITableViewCell:

TwitterCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TwitterCell"];

    if (cell == nil) {
        cell = [[TwitterCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TwitterCell"];
    }

    NSDictionary *data = tweets[indexPath.row];

    id favorited = data[@"favorited"];
    NSLog(@"%@",favorited);
    id retweeted = data[@"retweeted"];
    NSLog(@"%@",retweeted);

    if ((int)favorited == 1) {
        [cell.favoriteButton select];
    } else {
        [cell.favoriteButton deselect];
    }

    if ((int)retweeted == 1) {
        [cell.retweetButton select];
    } else {
        [cell.retweetButton deselect];
    }

Here is a possible point of issue in my custom TableViewCell class. Where I prepare for reuse:

- (void)prepareForReuse
{
    [super prepareForReuse];
    self.favoriteButton.selected = NO;
    self.tweetLabel.text = nil;
    self.profilePicture.image = nil;
    self.nameLabel.text = nil;
    self.retweetButton.selected = NO;
    self.usernameLabel.text = nil;
}

There are a couple of things wrong with the code you have provided.

1 - Casting NSNumber to int

Retrieving favorited , which is presumably a number, from the dictionary will return an NSNumber which is a wrapper class for numbers. When you NSLog this it will print the result of it's description method, essentially printing out the underlying number.

When you cast an NSNumber to int you don't get the underlying number but the memory address in integer format.

See this code and some example output:

NSNumber *favorited = @(1);
NSLog(@"%@", favorited);
NSLog(@"%d", (int)favorited);

favorited = @(0);

NSLog(@"%@", favorited);
NSLog(@"%d", (int)favorited);

Outputs

2015-08-19 07:04:36.235 xctest[589:7545] 1
2015-08-19 07:04:36.236 xctest[589:7545] 18
2015-08-19 07:04:36.236 xctest[589:7545] 0
2015-08-19 07:04:50.720 xctest[589:7545] 2

To retrieve the underlying number it is necessary to use one of the appropriate *value methods such as integerValue or longValue .

2 - Calling select and deselect

Neither of these methods exist on UIButton unless you have created a custom subclass. To properly adjust the selected state of a button you need to use the selected property as you have done in the prepareForReuse method.

button.selected = YES; // or NO

Putting it all together

Taking the above information we can change part of the cell creation method as follows:

if ([favorited integerValue] == 1) {
    cell.favoriteButton.selected = YES;
} else {
    cell.favoriteButton.selected = NO;
}

if ([retweeted integerValue] == 1) {
    cell.retweetButton.selected = YES;
} else {
    cell.retweetButton.selected = NO;
}

I think the issue is with

(int)favorited == 1

Just use [favorited intvalue] things will be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM