RSS

Monthly Archives: February 2011

Add chevrons and images from URL to Table View

The following code adds images from URLs and chevrons to Table View like on the image below:

table view with images

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    cell.textLabel.text = [self.propertyList objectAtIndex: [indexPath row]];
	
    // show chevron on the right
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
    NSString *path = @"http://assets.survivalinternational.org/static/lib/img/content/video_icon.png";
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *icon = [[UIImage alloc] initWithData:data cache:NO];
    cell.image = icon;
    
    return cell;
}
 
Leave a comment

Posted by on February 15, 2011 in Apple

 

Stay logged in between application launches

In the old version of Facebook iOS SDK to stay logged in next time your application launches you would call [session resume].

You will notice that new Facebook SDK for iPhone is lacking this functionality.

Here is an example of how you achieve same results in the new version of SDK.

Save access token and expiration date on successful login event:

- (void)fbDidLogin {
	NSLog(@"logged in");
	...
        [[NSUserDefaults standardUserDefaults] setObject:_facebook.accessToken forKey:@"AccessToken"];
	[[NSUserDefaults standardUserDefaults] setObject:_facebook.expirationDate forKey:@"ExpirationDate"];
}

When the view is loaded set access token and expiration date to their saved values. Is session is still valid, Facebook will not ask the user to login again.

- (void)viewDidLoad {
        _facebook = [[Facebook alloc] initWithAppId:kAppId];
	
	_facebook.accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"AccessToken"];
	_facebook.expirationDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"ExpirationDate"];
	
	[_facebook authorize:_permissions delegate:self];

        // or do not show dialog at all
        if ([_facebook isSessionValid]) {
		// your code
	}
	else {
		[_facebook authorize:_permissions delegate:self];
	}
}
 
4 Comments

Posted by on February 12, 2011 in Apple, Facebook

 

Regular Expression for URL / IP with Port Number

Regular expression for checking URL for protocol, subdomain, domain, IP address, port number, path or/and filename.

Matches:

Non-Matches:

var regexp = /^((([hH][tT][tT][pP][sS]?|[fF][tT][pP])\:\/\/)?([\w\.\-]+(\:[\w\.\&%\$\-]+)*@)?((([^\s\(\)\<\>\\\"\.\[\]\,@;:]+)(\.[^\s\(\)\<\>\\\"\.\[\]\,@;:]+)*(\.[a-zA-Z]{2,4}))|((([01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}([01]?\d{1,2}|2[0-4]\d|25[0-5])))(\b\:(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}|0)\b)?((\/[^\/][\w\.\,\?\'\\\/\+&%\$#\=~_\-@]*)*[^\.\,\?\"\'\(\)\[\]!;<>{}\s\x7F-\xFF])?)$/;

Source http://regexlib.com.

 
Leave a comment

Posted by on February 10, 2011 in misc

 

Tags: , , , ,

NimbleKit build error on new project

If you as me stumbled across one of the following errors when trying to build you new NimbleKit iPhone application, try a few possible solutions below.

Error messages you might get:

  • There is no SDK with the name or path ‘iphoneos’
  • “_ADBannerContentSizeIdentifierPortrait”, referenced from: …
  • “_ADBannerContentSizeIdentifierLandscape”, referenced from: …

1. IPhone SDK version

First possible solution is to check that you are using the correct version of iPhone SDK. Go to /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs and make sure the folder for the desired version exists.

For example, NimbleKit 1.9.5 requires iOS SDK 4.2. Check NimbleKit website for more information on system requirements.

2. Project build SDK

If correct version of SDK is installed another place to check is your project settings. Go to Project -> Edit Project Settings menu. On the Build tab look for Base SDK option. It should be set to the version required by NimbleKit (4.2 for NimbleKit 1.9.5).

iOS Deployment Target doesn’t matter in this case as it’s a minimum iOS version you want your app to run on.

 
Leave a comment

Posted by on February 10, 2011 in Apple

 

Tags: , , ,

Show tab with invalid controls when validating the form

ExtJs Tab Panel

Wouldn’t it be nice if in addition to highlighting invalid controls, FormPanel makes the tab that contains invalid control active? If you think it’s handy too then the following code will help you to achieve it.

What we need to do is overrite isValid() method of the BasicForm which is the parent of all other form components, such as FormPanel for example.

Ext.custom.FormPanel = Ext.extend(Ext.form.FormPanel, 
{

Check if form has a Tab Panel as it’s child. For this filter form’s controls by their xtype. See ExtJs documentation function for more details on filterBy function.

	
findChildTabPanel: function()
{
	var tabPanels = this.items.filterBy(function(c){ return c.getXType()=='tabpanel'; });
	if (tabPanels.items[0]) return true;
	return false;
},
overriteIsValid: function()
{
	var self = this;
	Ext.override(Ext.form.BasicForm, 
	{
		isValid: function(e) {
			var valid = true;
			var firstInvalidField = null;
					
			self.getForm().items.each(function(f)
			{
				if(!f.validate())
				{
					valid = false;
					f.markInvalid();
					if (firstInvalidField == null) firstInvalidField = f;
				}
			});
				
			if (firstInvalidField) 
			{
				var tab; 
				var tabbedPanel = firstInvalidField.findParentBy(
					function(p)
   			         {
			             if (p.isXType('tabpanel'))
			             {
			               	return true; 
			             }
			             tab = p;
			        });
			       if ((!Ext.isEmpty(tab,false)) && 
                   (!Ext.isEmpty(tabbedPanel,false)
                   { 
		              tabbedPanel.activate(tab.getId());  
		           } 
			}
			return valid;
		}
	});
},

Check if the form contains Tab Grid then override IsValid() method during component initialisation.

initComponent: function()
{
	Ext.taopix.FormPanel.superclass.initComponent.apply(this,arguments);
	
        this.on('afterlayout', function() { 
                if (this.findChildTabPanel()) this.overriteIsValid(); 
        }, this);
}
	
});

And register xtype type for new component:

Ext.reg('taopixFormPanel', Ext.taopix.FormPanel );

You can see working example here.

 
Leave a comment

Posted by on February 7, 2011 in Sencha

 

Tags: , , ,

ExtJS: GroupTabs hide expand/collapse icon on items with no child elements

By default in ExtJS (version 3.0.3) GroupTabs shows an expand/collapse icon even if group doesn’t have any subitems. Like on this screenshot:

grouptabs

If you want to change this behavior and hide an icon you will have to make a change or override onRender method of GroupTabPanel (GroupTabPanel.js).

This is a piece of modified onRender method:


/*if (!this.groupTpl) { */
      	var tt;
       	for (i = 0; i < this.items.length; i++)
       	{
       		if (this.items.items[i].items.length < 2)
       		{
       			tt = new Ext.Template(
      				'<li class="{cls}" id="{id}">',
       				'<a class="x-grouptabs-text {iconCls}" href="#" onclick="return false;">',
       				'<span>{text}</span></a>',
       				'</li>'
       			);
       		}
       		else
       		{
       			tt = new Ext.Template(
       				'<li class="{cls}" id="{id}">',
       				'<a class="x-grouptabs-expand" onclick="return false;"></a>',
       				'<a class="x-grouptabs-text {iconCls}" href="#" onclick="return false;">',
       				'<span>{text}</span></a>',
       				'</li>'
       			);
       		} 
       		tt.disableFormats = true;
           	tt.compile();
            	Ext.ux.GroupTabPanel.prototype.groupTpl = tt;

            	this.initGroup( this.items.items[i], i);
       	}
        	
/*
        var tt = new Ext.Template(
                '<li class="{cls}" id="{id}">', 
                '<a class="x-grouptabs-expand" onclick="return false;"></a>', 
                '<a class="x-grouptabs-text {iconCls}" href="#" onclick="return false;">',
                '<span>{text}</span></a>', 
                '</li>'
        );
        tt.disableFormats = true;
        tt.compile();
        Ext.ux.GroupTabPanel.prototype.groupTpl = tt;
}
this.items.each(this.initGroup, this);
*/

As a result you should get something like this:

grouptabs

 
1 Comment

Posted by on February 4, 2011 in Sencha

 

Tags: , ,

ExtJS: DatePicker width issue

The issue with DatePicker control width appears in Safari starting version 4.0.4 if Doctype declaration for the page is missing.

Here is a quick fix for it that should be included in the style section after ExtJs stylesheet:

.ext-safari .x-date-picker 
{
    width: 185px;
}

As reported on ExtJs forum this problem is fixed in 3.1.

 
2 Comments

Posted by on February 2, 2011 in Sencha

 

Tags: , ,

 
%d bloggers like this: