Showing posts with label Cocoa. Show all posts
Showing posts with label Cocoa. Show all posts

Monday, May 9, 2011

Objectice C: Change data of NSarray

NSMutableArray *tableContent = [[NSMutableArray alloc] initWithObjects:
                    [NSMutableArray arrayWithObjects:@"a",@"b",@"c",nil],
                    [NSMutableArray arrayWithObjects:@"d",@"e",@"f",nil],
                    [NSMutableArray arrayWithObjects:@"g",@"h",@"i",nil],
                     nil];

[[tableContent objectAtIndex:0] replaceObjectAtIndex:1 withObject:@"new object"];

Source:
http://stackoverflow.com/questions/2088679/objective-c-accessing-changing-array-elements-in-a-multidimensional-array-nsar

Tuesday, April 26, 2011

IPhone: Custom Table Cell

In order to make a custom table cell you have to handle the cell contentView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
   
    // Set up the cell...
    pinFreeNumberItem *cellValue = [tableData objectAtIndex:indexPath.row];
   
    UIView *myContentView = cell.contentView;
    UILabel* topLeftLabel = [[UILabel alloc] initWithFrame: CGRectMake(2, -6, 350, 30)];
    topLeftLabel.text = @"hi1";
   
    [myContentView addSubview:topLeftLabel];
   
    UILabel* bottomRightLabel = [[UILabel alloc] initWithFrame: CGRectMake(170, 24, 350, 15)];
    bottomRightLabel.text =@"hi2";
   
    [myContentView addSubview:bottomRightLabel];
   
    UILabel* bottomLeftLabel = [[UILabel alloc] initWithFrame: CGRectMake(2, 24, 150, 15)];
    bottomLeftLabel.text = @"hi3";
   
    [myContentView addSubview:bottomLeftLabel];
   
    return cell;
}

Resources:
http://stackoverflow.com/questions/5337340/custom-uitablecell
http://iphone.zcentric.com/2008/08/05/custom-uitableviewcell/

IPhone Table View Styles

Tuesday, April 19, 2011

Getting unique Id from ABPersonInstance

iterate a Dict in objective c


NSEnumerator *enumerator = [myDict keyEnumerator];
id key;
// extra parens to suppress warning about using = instead of ==
while((key = [enumerator nextObject]))
    NSLog(@"key=%@ value=%@", key, [myDict objectForKey:key]);
 
source:
http://stackoverflow.com/questions/1284429/is-there-a-way-to-iterate-over-an-dictionary 

request to send from iphone data and files to server using http post


-(NSMutableURLRequest *)requestWithURL:(NSURL *)url withBody:(NSMutableDictionary *)body  withUrlAndAttachedFile:(NSString *)fileAndUrl withFileName:(NSString *)fileName withFileFormat:(NSString *)fileFormat

{
    NSMutableURLRequest *request;
    NSString *requestBody;
       
    request = [[NSMutableURLRequest alloc] initWithURL:url
                                           cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                       timeoutInterval:60];
   
    request= [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *postbody = [NSMutableData data];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file_0\"; filename=\"%@.%@\"\r\n", fileName, fileFormat] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    NSData *postData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:fileAndUrl]];
    [postbody appendData:[NSData dataWithData:postData]];
    [postbody appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
   
   
    // iterate body dict
    NSEnumerator *enumerator = [body keyEnumerator];
    id key;
    // extra parens to suppress warning about using = instead of ==
    while((key = [enumerator nextObject])){
        //NSLog(@"key=%@ value=%@", key, [body objectForKey:key]);
        [postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
        [postbody appendData:[[body objectForKey:key] dataUsingEncoding:NSUTF8StringEncoding]];
        [postbody appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    }
   
   
    // close form
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:postbody];
   
   
    return request;
}
references:
http://stackoverflow.com/questions/4458112/mac-os-nsurlconnection-in-iteration

"UIApplication" undeclared (first use in this function)

you have to add this import line:

#import <UIKit/UIKit.h>


Friday, April 8, 2011

warning: passing argument 1 of 'localizedStringForKey:value:table:' from incompatible pointer type

 NSString *paramVia    = NSLocalizedString("share_on_twitter_url_via", nil);

there was missing something, so add @ to "share_on_twitter_url_via" : @"share_on_twitter_url_via"

 NSString *paramVia    = NSLocalizedString(@"share_on_twitter_url_via", nil);

Wednesday, April 6, 2011

Simple Custom loading alert view

UIActivityIndicatorView* spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinner setCenter:CGPointMake(140, 100)];

NSString* messageTitleToUser = @"Favorites list";
 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:messageTitleToUser message:@"loading..." delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];

  [alertView addSubview:spinner];
  [spinner startAnimating];
  [alertView show];

....
...
...

//if you programmatically want to close the alert view
 [alertView dismissWithClickedButtonIndex:0 animated:YES];
// stop spinner
[spinner stopAnimating];


References:
http://stackoverflow.com/questions/593234/how-to-use-activity-indicator-view-on-iphone