Treatment of onycholysis depends on the cause of the problem. Eliminating or correcting the predisposing cause is the best treatment. For example, treatment of hyperthyroidism will allow the nails to regrow normally and nail infections can be treated with antimicrobials.
The portion of nail that has separated will not reattach to the nail bed and you will have to wait until the nail is fully regrown for the condition to be completely gone. Fingernails take 4-6 months to fully regrow whilst toenails may take twice as long.
To prevent deterioration or recurrence of onycholysis after it has cleared up: