NSIndexSet indexLessThanIndex
- (NSUInteger)indexLessThanIndex:(NSUInteger)index
Parameters
index
Index being inquired about.
Return Value of [NSIndexSet indexLessThanIndex]
Closest index in the index set less than index; NSNotFound when the index set contains no qualifying index.
NSIndexSet indexLessThanIndex example.
- (BOOL) imageBrowser:(IKImageBrowserView *) aBrowser moveItemsAtIndexes: (NSIndexSet *)indexes toIndex:(NSUInteger)destinationIndex{
NSUInteger index;
NSMutableArray *temporaryArray;
temporaryArray = [[[NSMutableArray alloc] init] autorelease];
for(index=[indexes lastIndex]; index != NSNotFound;
index = [indexes indexLessThanIndex:index])
{
if (index < destinationIndex)
destinationIndex --;
id obj = [mImages objectAtIndex:index];
[temporaryArray addObject:obj];
[mImages removeObjectAtIndex:index];
}
// Insert at the new destination
int n = [temporaryArray count];
for(index=0; index < n; index++){
[mImages insertObject:[temporaryArray objectAtIndex:index]
atIndex:destinationIndex];
}
return YES;
}
NSUInteger index;
NSMutableArray *temporaryArray;
temporaryArray = [[[NSMutableArray alloc] init] autorelease];
for(index=[indexes lastIndex]; index != NSNotFound;
index = [indexes indexLessThanIndex:index])
{
if (index < destinationIndex)
destinationIndex --;
id obj = [mImages objectAtIndex:index];
[temporaryArray addObject:obj];
[mImages removeObjectAtIndex:index];
}
// Insert at the new destination
int n = [temporaryArray count];
for(index=0; index < n; index++){
[mImages insertObject:[temporaryArray objectAtIndex:index]
atIndex:destinationIndex];
}
return YES;
}
Example of [NSIndexSet indexLessThanIndex].
#define GBLBIN(x) [[NSUserDefaults standardUserDefaults] boolForKey:(x)]
- (IBAction)autosizeColumns:(id)sender
{
NSMutableArray * widths = [NSMutableArray array];
NSIndexSet * rows = [table selectedRowIndexes];
NSArray * columns = [table tableColumns];
BOOL deslectAll = NO;
NSNumber * col = nil;
int i, j;
// If no row's selected, then select all, the default
if (![rows count])
{
[table selectAll:sender];
rows = [table selectedRowIndexes];
deslectAll = YES;
}
// Use the selected rows from which we'll use the columns' formatted data widths
for (i=[rows lastIndex]; i != NSNotFound; i=[rows indexLessThanIndex:i])
{
for (j=0; j<[columns count]; j++)
{
NSTableColumn * column = [columns objectAtIndex:j];
id item = [[table dataSource]
tableView:table objectValueForTableColumn:column row:i];
NSCell * cell = [column dataCell];
float width, minWidth=10;
// Depending on the dataCell type (image or text) get the 'width' needed
if ([cell isKindOfClass:[NSTextFieldCell class]])
{
NSFont * font = ([cell font]
? [cell font] : [NSFont controlContentFontOfSize:(-1)]);
NSDictionary * attrs =
[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSFormatter * formatter = [cell formatter];
NSString * string = item;
// We want a string, as IB would have formatted it...
if (![item isKindOfClass:[NSString class]])
if (formatter)
string = [formatter stringForObjectValue:item];
else
string = [NSString stringWithFormat:@"%@", item];
width = [string sizeWithAttributes:attrs].width + minWidth;
}
else
{
// We have NSButtonCell, NSImageCell, etc; get object's width
width = MAX(minWidth,[[cell image] size].width);
}
// First time seeing this column, go with minimum width
if (j == [widths count])
{
[widths addObject:[NSNumber numberWithFloat:minWidth]];
}
col = [widths objectAtIndex:j];
width = MAX([col floatValue], width);
[widths replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:width]];
}
}
// Now go resize each column to its minimum data content width
for (j=0; j<[widths count]; j++)
{
NSTableColumn * column = [columns objectAtIndex:j];
float width = [[widths objectAtIndex:j] floatValue];
if (GBLBIN(@"debug"))
{
NSLog(@"col:%d '%@' %.f", j, [column identifier], width);
}
[column setWidth:width];
}
// Undo select all if we did it
if (deslectAll)
{
[table deselectAll:sender];
}
}
- (IBAction)autosizeColumns:(id)sender
{
NSMutableArray * widths = [NSMutableArray array];
NSIndexSet * rows = [table selectedRowIndexes];
NSArray * columns = [table tableColumns];
BOOL deslectAll = NO;
NSNumber * col = nil;
int i, j;
// If no row's selected, then select all, the default
if (![rows count])
{
[table selectAll:sender];
rows = [table selectedRowIndexes];
deslectAll = YES;
}
// Use the selected rows from which we'll use the columns' formatted data widths
for (i=[rows lastIndex]; i != NSNotFound; i=[rows indexLessThanIndex:i])
{
for (j=0; j<[columns count]; j++)
{
NSTableColumn * column = [columns objectAtIndex:j];
id item = [[table dataSource]
tableView:table objectValueForTableColumn:column row:i];
NSCell * cell = [column dataCell];
float width, minWidth=10;
// Depending on the dataCell type (image or text) get the 'width' needed
if ([cell isKindOfClass:[NSTextFieldCell class]])
{
NSFont * font = ([cell font]
? [cell font] : [NSFont controlContentFontOfSize:(-1)]);
NSDictionary * attrs =
[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSFormatter * formatter = [cell formatter];
NSString * string = item;
// We want a string, as IB would have formatted it...
if (![item isKindOfClass:[NSString class]])
if (formatter)
string = [formatter stringForObjectValue:item];
else
string = [NSString stringWithFormat:@"%@", item];
width = [string sizeWithAttributes:attrs].width + minWidth;
}
else
{
// We have NSButtonCell, NSImageCell, etc; get object's width
width = MAX(minWidth,[[cell image] size].width);
}
// First time seeing this column, go with minimum width
if (j == [widths count])
{
[widths addObject:[NSNumber numberWithFloat:minWidth]];
}
col = [widths objectAtIndex:j];
width = MAX([col floatValue], width);
[widths replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:width]];
}
}
// Now go resize each column to its minimum data content width
for (j=0; j<[widths count]; j++)
{
NSTableColumn * column = [columns objectAtIndex:j];
float width = [[widths objectAtIndex:j] floatValue];
if (GBLBIN(@"debug"))
{
NSLog(@"col:%d '%@' %.f", j, [column identifier], width);
}
[column setWidth:width];
}
// Undo select all if we did it
if (deslectAll)
{
[table deselectAll:sender];
}
}