Lefora Free Forum
login join
Loading
686 views

Getting selected Lines of tableView

Page 1
1–2
regular - member
54 posts

I am working with a tableView and need to get the selected line(s). No problem. But if there is no line selected I want to start working from the 1st line, so I tried to reset my index to 0, but the compiler is not happy with me(again), and is telling me the second "selectedRowIndexes" is unused. Do I just need to make the index mutable or is that just a bandaid?

    //get selected lines
    NSIndexSet* selectedRowIndexes = [tableView selectedRowIndexes];
   
    if (0 == selectedRowIndexes)//no line selected start at line 0
    {
        [selectedRowIndexes release];
        NSIndexSet* selectedRowIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [tableView numberOfRows]- 1)];
    }


Thank you very much,
Todd

regular - member
54 posts

I worked it out like this:

    //get selected lines
    NSIndexSet* selectedRowIndexes;
   
    if (0 == [[tableView selectedRowIndexes] count])//no line selected start at line 0
    {
        NSLog(@"No lines Selected");
        selectedRowIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [tableView numberOfRows])];
    }
    else
    {
        selectedRowIndexes = [tableView selectedRowIndexes];
    }
     
   
    unsigned int numSelectedRows = [selectedRowIndexes count];
    unsigned int indexBuffer[numSelectedRows];
    unsigned int bufferIndex;
    unsigned int indexCount=1;
   
    // more complex but as efficient as you choose your buffersize
    NSRange range=NSMakeRange([selectedRowIndexes firstIndex],
                              [selectedRowIndexes lastIndex]-[selectedRowIndexes firstIndex]+1);
   
    while ((indexCount=[selectedRowIndexes getIndexes:indexBuffer maxCount:numSelectedRows inIndexRange:&range])) {
        for (bufferIndex=0;bufferIndex<indexCount;bufferIndex++) {
            unsigned int index=indexBuffer[bufferIndex];
            // do stuff
            DownloadLink *theInfo = [theLinks objectAtIndex: index]; //get the object(s) from selected lines
            NSLog(@"The Selected Line = %@",[theInfo downloadName]);
           
            i = [theInfo percentComplete]; //get the status of link

        }
    }


That worked but THIS was simpler and looks cleaner to me:

    //make sure the tableview is not empty
   if([theLinks count]==0) return;
   
    NSArray *selectedLinks;
    if ([[downloadLinkController selectedObjects] count]==0)//no line selected start at line 0
    {
        NSLog(@"No lines Selected");
        selectedLinks = [NSArray arrayWithObject:[theLinks objectAtIndex:0]]; //get the data from the array
    }
    else
    {
        selectedLinks = [downloadLinkController selectedObjects];//get the data from the arraycontroller
    }

    int i;
    for( i = 0; i < [selectedLinks count]; i++ )
    {
        NSLog(@"Selected Line: %@",[[selectedLinks objectAtIndex:i] linkName]);
        //do something cool with the selected line
    }


Page 1
1–2

Locked Topic


You must be a member to post in this forum

Join Now!