Krunal
Krunal

Reputation: 6490

How to Fetch UIPopOver Selected row text

I am new to iPhone developer,

In my application when i click on a button popover appears.

what i want to do is, when i select any row in popover that cell should marked with right arrow image like:

cell.accessoryType = UITableViewCellAccessoryCheckmark;

and i want to fetch text of that selected row.

Inshort i want to implement checkbox in my popover, if 5 rows are selected then text of that 5 rows i want to fetch and store it in array.

Here is my code:

-(void)btnClicked{
    UIViewController* popoverContent = [[UIViewController alloc]init];
    UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(110, 0, 500, 4)];

    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 250, 665) style:UITableViewStylePlain];

    [table setDelegate:(id<UITableViewDelegate>)self]; 
    [table setDataSource:(id<UITableViewDataSource>)self]; 
     [self.view addSubview:table];
    [table release];

    [popoverView addSubview:table];
    popoverContent.view = popoverView;
    popoverContent.contentSizeForViewInPopover = CGSizeMake(250, 600);
    self.popoverController = [[UIPopoverController alloc]
                              initWithContentViewController:popoverContent];

    [self.popoverController  presentPopoverFromRect:CGRectMake(100,0, 535, 35) 
                                             inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];


    [popoverView release];
    [popoverContent release];
} 

After this i am passing my array to UITableView.

Thanks In Advance !

Upvotes: 1

Views: 279

Answers (2)

user1458284
user1458284

Reputation:

Please find the new code I have edited the previous one:

  if(cells == nil)
       cells = [[NSMutableArray alloc] init];

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath]
{
   if(indexPath.row < cells.count)
   {
      NSMutableDictionary *dict = [cells objectAtIndex:indexPath.row];
      BOOL isChecked = [[dict objectForKey:"isChecked"] boolValue];

      if(!isChecked)
      {
         [cells removeAllObjects];
         for(int index=0;index<indexPath.row;index++)
         {
             UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
             [cell setAccessoryType:UITableViewCellAccessoryCheckmark];

             dict = [[NSMUtableDictionary alloc] init]
             [dict setObject:@"true" forKey:@"isChecked"];
             [dict setObject:cell.textLabel.text forKey:@"rowText"];
             [cells addObject:dict];
             [dict release];
         }
      }
      else
      {
         UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
         [cell setAccessoryType:UITableViewCellAccessoryNone];
         [cells removeObjectAtIndex:indexPath.row];
      }
   }
   else
   {
         [cells removeAllObjects];
         for(int index=0;index<indexPath.row;index++)
         {
             UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
             [cell setAccessoryType:UITableViewCellAccessoryCheckmark];

             dict = [[NSMUtableDictionary alloc] init]
             [dict setObject:@"true" forKey:@"isChecked"];
             [dict setObject:cell.textLabel.text forKey:@"rowText"];
             [cells addObject:dict];
             [dict release];
         }
   }

}

Please note that your array should be global

Upvotes: 0

user1458284
user1458284

Reputation:

Please implement below code in your didSelectedRow method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
  {
       NSMutableArray *cells = [[NSMutableArray alloc] init];
       for(int index=0;index<indexPath.row;index++)
       {
           UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
           [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
           [cells addObject:cell.textLabel.text];
       }
  }

Upvotes: 1

Related Questions