Toddlers are so changeable and mercurial.
What you need to do is figure out what's going on with your child at that particular moment and respond to him accordingly. Even kids have days when all they want to do is crawl under a rock and stay there. While the issues may be less complex than what grownups have to deal with, a disappointment at daycare or preschool or frustration during a play date can bring out the loner in a toddler. Maybe his favorite friend didn't want to play with him at the park, or perhaps his usual peer group at preschool made fun of him for crying during circle time. Either way, he's upset about it and doesn't want your hugs or cuddles to make him feel better.