Archive for January 2011

Compound comparison in a LINQ join statement

I’ve been doing pretty much iPhone only postings recently, so this might change it up a bit.

So I am trying to go into our web application’s C# code to make some changes to the administration area of the web site.  (This is usually the only place I feel comfortable making changes, as this is not an area that customers actually use.)  We have a page that uses a store procedure to pull data from our SQL Server database and presents it on the page.

I needed to get more information out of the database than the store procedure was giving me, and I didn’t feel like modifying the procedure and then trying to rebuild the DBML, so I decided to convert it to a LINQ statement and bind to that instead of binding to the results of the stored procedure.

These things never go as planned.  I took a similar LINQ statement that I found in the application, but it did not do exactly what I wanted to do.  Basically, the LINQ statement I found used a simple comparison.  I needed to check for two different things in my comparison, so after a bit of research and trial and error, here is what I came up with:

var p = (from ord in dc.orders
    join ordSt in dc.orderStatus on 
        new { ord.orderID, b = true } equals new { ordSt.orderID, b = ordSt.isDeleted }
    where ord.customerID == custID
    select
        new { ord.orderID, ordSt.deletionDate } );

I had to add the little “b = true” and “b = ordSt.isDeleted” parts because it would not let me use just the “true” in the comparison.  Ah, isn’t it great that LINQ is so simple?

Double tap a table view cell on a UITableViewController

So I have one of those nifty UITableViewController views in my application, and since the app is now starting to get a bit more complex, I wanted to have a quick way to be able to do multiple things with a cell from that view.  Unfortunately, if you use the UITableViewCellStyleDefault style to create your cell, there is not much you can do with it, other than to have an accessory type.  This is how I started using the cells, allowing the user to set a cell to be selected or not selected (setting the cell accessoryType to UITableViewCellAccessoryCheckmark or UITableViewCellAccessoryNone).

In order to perform a different function on the cell, I figured that it couldn’t be too hard to allow the user to double tap the cell to do something other than turn the check mark off and on again or vice versa.  As per usual with the iPhone platform, it took a bit of digging.

At first, I thought I could use a UIGestureRecognizer to detect the double tap.  Unfortunately, our apps are still trying to support 3.1.3 OS users, and the UIGestureRecognizer requires 3.2.

So with a bit more digging, I found a solution on Stack Overflow that I “borrowed”.  Unfortunately, that code was a bit rough and required some minor tweaks and fixes, so here below is how to do this.

First, you need to make sure to add the necessary ivars to your class header file:

int tapCount, tappedRow, doubleTapRow;

Then, here is the code that you will need in your implementation file.  Those of you familiar with the UITableViewDelegate will see something familiar and something to extend it.

#pragma mark -
#pragma mark Table view delegate
 
- (void)tapTimerFired:(NSTimer *)aTimer
{
    // timer fired, there was a single tap on indexPath.row = tappedRow
 
    // do something here with tappedRow
 
    [cell setSelected:NO animated:YES];  // maybe, maybe not
 
    if (tapTimer != nil)
    {
        tapCount = 0;
        tappedRow = -1;
    }
}
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
 
    // checking for double taps here
    if (tapCount == 1 && tapTimer != nil && tappedRow == indexPath.row)
    {
        // double tap - Put your double tap code here
        [tapTimer invalidate];
        tapTimer = nil;
 
        //NSLog(@"double tapped the cell");
        doubleTapRow = tappedRow;
 
        [cell setSelected:NO animated:YES];  // maybe, maybe not
 
        // do something here with doubleTapRow
 
        tapCount = 0;
        tappedRow = -1;
    }
    else if (tapCount == 0)
    {
        // This is the first tap. If there is no tap till tapTimer is fired, it is a single tap
        tapCount = 1;
        tappedRow = indexPath.row;
        tapTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self 
                selector:@selector(tapTimerFired:) 
                userInfo:nil repeats:NO];
    }
    else if (tappedRow != indexPath.row)
    {
        // tap on new row
        tapCount = 0;
        if (tapTimer != nil)
        {
            [tapTimer invalidate];
            tapTimer = nil;
        }
    }
}

As it says in the code, you may or may not want to turn off the cell selection, it is up to you whether you need to do that or not. Also, you may want to play around with the time factor where the tapTimer is created if it does not work as well as you would like.

Missing GCC 4.2 options in Xcode Build tab

I was missing some GCC 4.2 options (such as a nice way to do some preprocessor definitions) in an older project of mine, and I knew that I had done something similar on a newer project.

As it turns out, for some reason it seems to put these options back in the Project Info window if you change your Code Signing Identity option.  I changed it (and the Any iOS option below it) to a different profile, then changed them back to iPhone Developer, and the missing options suddenly appeared in my older project.

Here is a link to the Stack Overflow question and answer:

Missing GCC 4.2 options in Project Info / Build tab in Xcode 3.2.5

By the way, Happy New Year everyone.  I hope you all had a nice 2010 holiday season.