Skip to content

Instantly share code, notes, and snippets.

@mthongvanh
mthongvanh / Async Collection View Update
Last active August 29, 2015 14:10
via #iosdev on irc.freenode.net: With the Parse SDK, a user had trouble displaying images that downloaded successfully.
// Problem: Collection View data does not appear after calling [UICollectionView reloadData] in [PFQuery findObjectsInBackgroundBlock:]
// Symptoms: The correct number returns in numberOfItems: but cellForItem: is never called
// Solution: Try performing collection view updates manually via performBatchUpdates: or reloadItemsAtIndexPaths: or reloadSectionsAtIndexSet:
- (void)queryForPhoto {
NSLog(@"%s", __PRETTY_FUNCTION__);
PFQuery *query = [PFQuery queryWithClassName:@"Image"];
PFUser *user = [PFUser currentUser];
[query whereKey:@"user" equalTo:user];
@mthongvanh
mthongvanh / UICollectionView Cell Sizing
Last active August 29, 2015 14:10
via #iosdev on freenode.net: User wanted the first and last column of a 7-Column collection view to be wider than the middle columns
// Intent: Create a 7-Column collection view where the first and last column are wider than the middle columns
// Solution: Via the UICollectionViewDeleateFlowLayout calculate the correct size of the cells
const NSUInteger daysInWeek = 7;
const CGFloat cellHeight = 54.f;
const CGFloat sizeRatioForWeekStartEnd = 0.20;
const CGFloat sizeRatioForMiddleDays = (1 - (2 * sizeRatioForWeekStartEnd)) / (daysInWeek - 2);
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize cellSize = CGSizeZero;
@mthongvanh
mthongvanh / NSURL fileURLWithPathComponents fail
Last active August 29, 2015 14:10
via #iphonedev on irc.freenode.net: AVAudioRecorder failed to initialize by crashing on initialization method
// Problem: AVAudioRecorder failed to initialize by crashing on initialization method
// Diagnosis: NSURL *audioURL was nil at audioRecorder initiliazation point because audioURL's weak declaration
// Solution: Declare NSURL *audioURL as strong to ensure it does not get deallocated prematurelys.
// *Side note suggestion: Be sure to use [AVAudioRecorder record] instead of prepareToRecord since record will call it implicitly
// and handle errors appropriately!
// properties
// @property (weak, nonatomic) NSURL *audioURL; /* User's original declaration */
@property (strong, nonatomic) NSURL *audioURL;
@property (strong, nonatomic) AVAudioRecorder *audioRecorder;
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
NSLog(@"nav size %@", NSStringFromCGRect(self.navigationController.navigationBar.bounds));
}
enum PersonDescription {
case Singular, Dual, Plural
struct Portrayal {
var literal: String, numeral: Int
}
var portrayal: Portrayal {
switch self {
case .Singular:
@mthongvanh
mthongvanh / DynamicScrollviewResizingViewController.h
Created March 11, 2015 10:53
DynamicScrollviewResizingViewController.h
#import "DynamicScrollviewResizingViewController.h"
@interface DynamicScrollviewResizingViewController ()
@property (weak, nonatomic) IBOutlet UILabel *dynamicLabel;
@end
@implementation DynamicScrollviewResizingViewController
- (void)viewDidLoad {
[super viewDidLoad];
@mthongvanh
mthongvanh / gist:3156dc54982cb3f445f3
Last active August 29, 2015 14:20
It's important NOT to assume that NSString paths will always work if you construct them yourself. This bit me today. As it has many other times in the past.
// In both versions, we're feeding a string to the NSFileManager method, but only the second one succeeds. In the debugger console, they both appeared with the file:// so it was a bit difficult to debug. Of course, I'd momentarily forgot about every. other. time. it has happened.
// Version 1
- (BOOL)fileExistsInCaches:(NSString *)filename
{
NSString *path = [NSString stringWithFormat:@"%@%@",[self cacheDirectory],filename];
// path = file://Users/mthongvanh/etc/etc/etc
return [[NSFileManager defaultManager] fileExistsAtPath:path];
}
@mthongvanh
mthongvanh / Debugging.txt
Last active December 23, 2015 15:47
Xcode Snippets
Debug Log Statement
----------------------
DLog(@"\n<#debugging message#> <#%@#>",<#args#>);
NSString Convenience Method
----------------------------
[NSString stringWithFormat:@"<#%@#>",<#args#>]
Dispatch Asynchronously on Main Queue
-------------------------------------
{
"agendaFilters": [{
"id": 1,
"display": "Conferences",
"value": "conferences",
"valueType": "decisionTree",
"filterType": "",
"options": [{
"id": 1,
"display": "Ellucian Live",