RSS

Monthly Archives: April 2013

Optional Parameters in SQL Stored Procedures

CREATE PROCEDURE TestProc
(
@Param1 varchar(50) = NULL,
@Param2 varchar(50) = NULL,
@Param3 varchar(50) = NULL
)
AS
SELECT
*

FROM
TestTable

WHERE
((@Param1 is null) OR (col1 = @Param1)) AND 
((@Param2 is null) OR (col2 = @Param2)) AND 
((@Param3 is null) OR (col3 = @Param3))

 
Leave a comment

Posted by on April 26, 2013 in SQL Server

 

iOS Programming 101: Customize UITableView and UITableViewCell Background using Storyboard

January 6, 2013 by 

 

In the last tutorial of our iOS Programming 101 series, we showed you how to customize the navigation bar and buttons with your own background image. This time, we’ll look into the customization and styling of UITableView and UITableViewCell.

If you’ve followed our iOS tutorials from the very beginning, you know we’ve written a tutorial about customizing table view cell using Interface Builder. In this tutorial, we’ll do something different. Instead of using Interface Builder to tweak the UITableViewCell, we’ll show you how to use Storyboards to style the cell. Storyboards make customizing table cells much easier with the introduction of prototype cell. In brief, you’ll learn the following stuffs in this tutorial:

  1. Customizing UITableViewCell using Storyboard
  2. Style UITableViewCell and UITableView with your own background images

To illustrate the customization, we’ll tweak a simple table view, style it and make it more attractive. To help you focus on learning the customization, we’ve prepared the Xcode project for you to start with. Before proceeding, first download the Xcode project here (note: the project is created using Xcode 4.5).

Designing Prototype Cells in Storyboard

Since the release of iOS 5, Apple introduced Storyboarding that simplifies the way to design user interfaces for your app. To customize a table cell, you no longer need to create a separate cell in Interface Builder. You can design the cell right inside the Storyboard editor.

Open the Xcode project you’ve downloaded. Go to the Storyboard and you should see something like this:

Storyboard Empty TableViewController

Table View Controller with Empty Prototype Cell

When you add a UITableViewController in the Story, by default, you should see an empty prototype cell. You can now add other UI control elements (e.g. UILabel, UIImageView) right into the prototype cell. Let’s first change the height of the cell. Select the Prototype Cells, click the “Size” inspector and change the row height from 44 to 71.

CustomTableView - Change Row Height

Changing row height in Storyboard

Next, drag an Image View from object library to the prototype cell. Again, select the image view, click “Size” inspector and change the “X”, “Y”, “Width” and “Height” attributes as follows:

CustomTableView - Add Image View

Add image view to prototype cell

Switch to the “Attributes” inspector, set the “Tag” to 100. You can think of a tag as an unique identifier of the UI control. Later in our code, we’ll use this tag to identify the image view.

CustomTableView - Image Tag

Assigning a tag for the image view

Then drag a label to the cell and name it as “RecipeName”. This label is used to display the name of recipe. In the “Size” inspector, set “X” as 92, “Y” as 7, “Width” as 186 and “Height” as 21. In the “Attributes” inspector, change the font type to “Helvetica Neue Condensed Bold” and set the font size to 21 points. Finally, set the “Tag” value to 101.

CustomTableView - Add Labels

Add labels to the prototype cell

Add another label to the cell and name it as “Details”. In the “Size” inspector, set “X” as 92, “Y” as 32, “Width” as 186 and “Height” as 33. Switch to the “Attributes” inspector, set the “Tag” value as 102.

Coding the Table View Controller

With the user interface designed, let’s get back to the code. Open “RecipeViewController.m”, which is the corresponding class of the Table View Controller, change the “cellForRowAtIndexPath:” method to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @”Cell”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell…
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Display recipe in the table cell
Recipe *recipe = [recipes objectAtIndex:indexPath.row];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:recipe.imageFile];

UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
recipeNameLabel.text = recipe.name;

UILabel *recipeDetailLabel = (UILabel *)[cell viewWithTag:102];
recipeDetailLabel.text = recipe.detail;

return cell;
}

We’ve added the code (from line 12 to 20) for displaying the cell labels and image view. As you can see from the code, we invoke the “viewWithTag:” method with the tag value to identify the UI component.

Compile and run the app. Your app should look like this:

CustomTableView - Initial App

Recipe Book App with default cell style

Styling the UITableCell with Background Image

Now you’ve learnt how to create custom table cell using Storyboard, but the table view cell is just in plain style. We want to change the look & feel of the table cells and make them prettier. In the Xcode project, you should find three background images for table cell. The images are specially designed for different types of cells.

CustomTableView - Cell Background Image

Cell Background Image – Top and Bottom images are with rounded corners

The UITableViewCell class allows developers to assign a custom background view using the “backgroundView:” method. To assign your own custom background image, we can create an UIImageView object with the background image and set it as the background view of the cell. Add the following code to the “cellForRowAtIndexPath:” method:

1
2
3
4
5
6
    // Assign our own background image for the cell
UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];

UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image = background;
cell.backgroundView = cellBackgroundView;

There is one thing missing. We haven’t implemented the “cellBackgroundForRowAtIndexPath:” method. As explained before, we have three types of background image. The top and bottom cells are assigned with different background images that are differed from the rest of the cells. So we create the “cellBackgroundForRowAtIndexPath:” method to determine the background image to assign. Add the following code before the “cellForRowAtIndexPath:” method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
– (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
NSInteger rowIndex = indexPath.row;
UIImage *background = nil;

if (rowIndex == 0) {
background = [UIImage imageNamed:@”cell_top.png”];
} else if (rowIndex == rowCount – 1) {
background = [UIImage imageNamed:@”cell_bottom.png”];
} else {
background = [UIImage imageNamed:@”cell_middle.png”];
}

return background;
}

For the first row of the cell (i.e. rowIndex == 0), the method returns the top cell image. If it’s the last table cell (i.e. rowIndex == rowCount – 1), the method returns the bottom cell image. For the rest of table cell, it simply returns the middle cell image.

Lastly, add the following line of code in the “viewDidLoad” method of RecipeViewController.m. This tells the table cell not to show the separator as our own cell images already come with the separator.

1
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

Compile and run the app. You should end up with an app like this:

CustomTableView - Styled Table View Cell

Recipe book app with custom background cell image

Styling the Table View Background

The app looks much better, right? Let’s make it even better by assigning a background image to the view. Add the following code to “viewDidLoad” method of RecipeViewController.m:

1
2
    self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@”common_bg”]];
self.tableView.backgroundColor = [UIColor clearColor];

As the RecipeViewController is a subclass of UITableViewController, we use “self.parentViewController” to access its parent view, followed by assigning the background. By default, the table view is displayed in white. We set its color to transparent so as not to block out the background of the parent view.

Before you run the app, we’d like to make one more change. The table view is now very close to the navigation bar. You may want to add padding at the top of the table view. To add padding, you can use the contentInset property to specify a buffer area around the content of the view. The contentInset property is a UIEdgeInsets struct with the fields top, bottom, left, right. Let’s add the following code in “viewDidLoad”:

1
2
    UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0);
self.tableView.contentInset = inset;

The above code results in an additional buffer area that is 5 pixels at the top of the table view. Compile and run the app again. Your app should now be customized with padding and our own background image:

CustomTableView - Final app

Recipe book app in better style

What’s Coming Next

Again, I hope you enjoy the tutorial and find it useful. For your complete reference, you can download the final Xcode project here.

Earlier, we already showed you how to customize the tab bar and navigation bar. Combining with this tutorial, you should be able to make your table-based app more attractive. Later, we’ll see how to style other UI elements. Stay tuned.

 

Copy from: http://www.appcoda.com/ios-programming-customize-uitableview-storyboard/

 
Leave a comment

Posted by on April 19, 2013 in iOS, Mobile Development

 

How to setup Search API with Apache Solr

By: Ivan Zugec

The Search API is a Drupal 7 search framework module. It allows you to create custom search pages on any URL and integrates with a few search backends.

In this article I’ll show you how to setup Search API with Apache Solr on a mac (10.6).

For more details check out the Search API project page.

If you already have solr setup skip to the Search API section.

Setup Apache Solr

The first thing we’ll have to do is setup Solr locally. Make sure you have Java installed, if your using OSX 10.6 and keep it up to date you should be fine.

To double check run java -version in terminal to see which version is installed.

sapi_image1.png

Next go to the download page and download version 1.4.1.

sapi_image2.png

Extract the files anywhere open up terminal and navigate to the extracted directory.

sapi_image3.png

Solr ships with a built-in web server called Jetty. We’ll use this for our local version. To start up the server go into the example directory and execute start.jar.

cd example
java -jar start.jar

Go to http://127.0.0.1:8983/solr/admin to see if solr is working.

sapi_image4.png

Setup Search API

To get Search API working with solr there are a few thing we’ll have to configure with solr.
First go and download the following modules from drupal.org and place them into sites/all/modules:

  1. Entity
  2. Search API
  3. Search API Solr search

drush dl entity search_api search_api_solr

Configure solr

Go to your solr root directory and copy/paste the example directory, rename it to drupal (you can call the directory anything).

Copy the schema.xml and solrconfig.xml that ships in search_api_solr and paste it into /drupal/solr/conf. It’s fine to override the existing xml files just make sure you create a backup.

Now go ahead and test solr with the new xml files.

cd drupal
java -jar start.jar

Go to http://127.0.0.1:8983/solr/admin to see if solr is working with the new xml files.

Instead of “(Example)” you should see “(search-api-1.0)”.

sapi_image5.png

Download SolrPhpClient

Go to http://code.google.com/p/solr-php-client/downloads/list and download (Only works with r22 Solr PHP Client (Built 11/09/09 from r22) ) the php solr library and place it in the seach_api_solr directory.

sapi_image6.png

sapi_image7.png

Modules

Go to the modules page and enable the Search API, Search pages and Solr search.

sapi_image8.png

Configure Search API

Go to Configuration >> Search and metadata and click on Search API.

sapi_image9.png

There are three steps involved with setting up a solr search page. First we setup the server, then index and finally the search page.

Setup server

From the Search API configuration page click on the Add server link.

sapi_image10.png

Enter in the Server name and Server description then select Solr service from the Service class select box.

sapi_image11.png

Adjust the Solr service details below if you have any specific requirements.

Once you have finished filling out the form click on Save settings.

sapi_image12.png

Setup index

Once the server has been setup, the next thing we have to do is configure the index. Click on the Add index from the Search API configuration page.

sapi_image13.png

In the Add index form fill out the Index name and select which entity type you want to index and select Solr from the Server select box.

sapi_image14.png

Once you have created the index you’ll have to select which fields will get indexed. This could take some trail and error depending on your data. In the example that I’m using I have add the “main body text” and “Tags” from the Add related fields.

sapi_image15.png

After you have selected which fields get to be indexed, you’ll be directed to the Workflow page. On this page you can specify extra data alterations and processors to the index data. For this example we’ll keep that page empty.

The final item for the index is to actually add data into the solr index. Click on the Status tab and click on the Index now button.

sapi_image16.png

Search pages

Now that we have the server and the index all sorted we have to create a Search Page.

Search API module gives you the ability to create custom search pages on any URL. You are no longer stuck with a single search page on search/node or search/apachesolr_node.

Go to the Search API page and click on the Search pages tab, then click on the Add search page link.

sapi_image17.png

On the Add search page form enter in the Search name, Index and Path.

sapi_image18.png

On the next page you have a few more options, if you want leave the default options and click on the Create page button.

sapi_image19.png

Go to your newly created search page and test it out.

sapi_image20.png

If your having trouble you can start debugging by watching what appears in the terminal.

sapi_image21.png

Faceted search

Another great feature that the Search API gives you is faceted search. However this only seems to work with a solr server. UPDATE: Facets do work with database servers. Comment

To get started go and enable the Search facets module.

sapi_image22.png

Once the module has been enabled go back and edit the index that you created. Then select Facets from the dropdown.

sapi_image23.png

For this example I enabled the Content type and Tags > Name block. But you can choose any facet blocks.

sapi_image24.png

Once the blocks are enabled don’t forget to assign them to a region.

sapi_image25.png

Go back to your search page and test out the facets.

sapi_image26.png

If you have any problems or questions please leave a comment.

Copy from: http://zugec.com/73-how-setup-search-api-apache-solr#search-api

 
Leave a comment

Posted by on April 18, 2013 in Solr