Change background of a UITableViewCell

I was asked this question by a co-worker on how to do this. My first answer was when you init the cell just set the backgroundColor but that did not work at all.

If you followed my other tutorials and have a custom UITableViewCell open that file and in the initWithFrame method you want the following

MyContentView.backgroundColor = [UIColor redColor];

Post JSON to a webservice

Make sure you have JSON Framework installed first.

This is a continuation of UIImageView in a custom cell

Here is a simple little code snippet on how to post some JSON to a webservice.

NSString *requestString = [NSString stringWithFormat:@"json=%@", [loginDict JSONFragment], nil];
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://domain.com/api/login/"]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: requestData];

NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];

// decoding the json
NSDictionary *loginArray = [NSDictionary alloc];
loginArray = [returnString JSONValue];

So below is what each line does.

  • First we format the request string. What I am doing there is taking a whole dictionary and making it a JSON object. I then assign that to the json variable to post in. So in PHP land that would come in as $_POST['json']  and I could do like json_decode($_POST['json']) and turn it into an array I can work with in PHP
  • The next line turns the request into a data object so we know how big the string is.
  • Now we setup the URL to post the request to
  • We are setting the method to a post in html land it is like <form method=”POST”>
  • Now are are setting up the body of the post with our data
  • We now make the connection to the server and then get a data string back
  • We decode the data stream into a NSString object
  • Since you send in JSON I expect you get JSON back so we take the contents of the NSString and turn it into a NSDictionary

Pretty simple. That is all there is to it.

UIBarButtonItem with a custom view

The main project I am doing for Photoblog is an application to show all the latest entries and such. So I call a web service using JSON and it gives me the data I need. Now there might be a few second delay in sending the request and getting the request back. Once I get the request back I open my new view. So one way to show the user that activity is going on is creating a activity indicator.

Apple has UIActivityIndicatorView which we will use. Now I want to put this in the top navigation bar but by default you cannot. So we have to set a UIBarButtonItem then use a custom view option. So here is the function I created to do this

-(void)showLoading {
	// initing the loading view
	CGRect frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
	UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithFrame:frame];
	[loading startAnimating];
	[loading sizeToFit];
	loading.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
								UIViewAutoresizingFlexibleRightMargin |
								UIViewAutoresizingFlexibleTopMargin |
								UIViewAutoresizingFlexibleBottomMargin);

	// initing the bar button
	UIBarButtonItem *loadingView = [[UIBarButtonItem alloc] initWithCustomView:loading];

	[loading release];
	loadingView.target = self;

	self.navigationItem.rightBarButtonItem = loadingView;

}

This isn’t anything major. It will create the UIActivityIndicatorView with a width and height of 25. It will start is animating and make it fit into the frame we created.

Then I init a UIBarButtonItem and use a initWithCustomView method to set it to use our activity indicator view. I then release the indicator and set the click target to self. I then place that button item on the right side of the bar.

Now I do this right before I begin the process of sending a request and getting the response. The problem is that I never see the animation due to the request and responsive locking the main thread.

So when I have threads figured out we will do the JSON request in a thread so the main thread isn’t locked.

Append to a NSString

I was working on appending to some strings and there is a way to do it with NSString but you are better off working with NSMutableString instead. I have a feeling doing it with NSString, if not done correctly, will leak memory.

NSMutableString *hello = [[NSMutableString alloc] initWithString:@"Hello"];
[hello appendString:@" World"];

Using NSUserDefaults

This will go over a quick run through of using NSUserDefaults. This is the class to use to save user preferences for example. It is pretty straight forward.

Saving Data

This is how you save data.

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving a string
[prefs setObject:@"test" forKey:@"stringVal"];
// saving a int
[prefs setObject::23 forKey:@"intVal"];
// saving it all
[prefs synchronize];

Loading Data

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting the string
stringVal = [prefs stringForKey:@"stringVal"];
// getting the int
intVal = [prefs integerForKey:@"intVal"];

That is it.. pretty simple.

Next Page →