Monday, June 17, 2013

NSIndexSet containsIndex example in Objective C (iOS).


NSIndexSet containsIndex

Indicates whether the index set contains a specific index.

- (BOOL)containsIndex:(NSUInteger)index

Parameters
index
Index being inquired about.

Return Value of [NSIndexSet containsIndex]
YES when the index set contains index, NO otherwise.

NSIndexSet containsIndex example.
-(void)highlightSelectionInClipRect:(NSRect)theClipRect
{
    NSRange visibleRowIndexes = [self rowsInRect:theClipRect];
    NSIndexSet *selectedRowIndexes = [self selectedRowIndexes];
    NSUInteger endRow = visibleRowIndexes.location + visibleRowIndexes.length;
    NSUInteger row;

    for (row=visibleRowIndexes.location; row<endRow; row++)
    {
        if([selectedRowIndexes containsIndex:row])
        {
            NSRect rowRect = NSInsetRect([self rectOfRow:row], 3, 4);
            NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rowRect xRadius:4.0 yRadius:4.0];
            [[NSColor colorWithCalibratedRed:0.474 green:0.588 blue:0.743 alpha:1] set];
            [path fill];
        }
    }
}

Example of [NSIndexSet containsIndex].
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
    // Maybe a test on the table column is recommanded if some cells should not be modified
    NSIndexSet *selection = [aTableView selectedRowIndexes];
    if ([selection containsIndex:rowIndex]) {
        [aCell setFont:[NSFont boldSystemFontOfSize:12]];
    } else {
        [aCell setFont:[NSFont systemFontOfSize:12]];
    }
}

NSIndexSet containsIndex example.
FOTableView.h

#import <Cocoa/Cocoa.h>

@interface FOTableView : NSTableView

@property (strong,nonatomic) NSMutableIndexSet *markedRows;

@end
FOTableView.m

#import "FOTableView.h"

@implementation FOTableView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }

    return self;
}

-(NSMutableIndexSet *) markedRows
{
    if (!_markedRows) {
        _markedRows = [NSMutableIndexSet new];
    }
    return _markedRows;
}

- (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect
{
    if ([self.markedRows containsIndex:row]) {
        NSRect clipRect = [self rectOfRow:row];
        NSColor *color =  [NSColor colorWithCalibratedRed:0.932 green:0.046 blue:0.960 alpha:1.000];
        [color setFill];
        NSRectFill(clipRect);
    }

    [super drawRow:row clipRect:clipRect];
}

- (void)keyDown:(NSEvent *)theEvent
{
    NSString *keyString;
    unichar  keyChar;

    keyString = [theEvent charactersIgnoringModifiers];
    keyChar = [keyString characterAtIndex:0];
    NSInteger row = [self selectedRow];
    switch(keyChar){           
        case 32:
        {
             if (row != -1)
             {
                 if ([self.markedRows containsIndex:row]) {
                     [self.markedRows removeIndex:row];
                 }
                 else {
                     [self.markedRows addIndex:row];
                 }
             }
            [self selectRowIndexes:[NSIndexSet indexSetWithIndex:++row] byExtendingSelection:NO];
            [self setNeedsDisplay:YES];
            break;
        }

        default:
            [super keyDown:theEvent];
    }

    NSLog(@"key pressed: (%hu)%@", keyChar,keyString);
}

- (void)rightMouseDown:(NSEvent *)theEvent
{
    NSInteger row = [self rowAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
    if ([self.markedRows containsIndex:row]) {
        [self.markedRows removeIndex:row];
    }
    else {
        [self.markedRows addIndex:row];
    }

    [self setNeedsDisplay:YES];
}

@end

End of NSIndexSet containsIndex example article.