1

我是 iPhone 开发者的新手,

在我的应用程序中,当我单击按钮时会出现弹出框。

我想要做的是,当我在弹出窗口中选择任何行时,该单元格应标有右箭头图像,如:

cell.accessoryType = UITableViewCellAccessoryCheckmark;

我想获取该选定行的文本。

简而言之,我想在我的弹出框中实现复选框,如果选择了 5 行,那么我想获取这 5 行的文本并将其存储在数组中。

这是我的代码:

-(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];
} 

在此之后,我将我的数组传递给 UITableView。

提前致谢 !

4

2 回答 2

1

请在您的 didSelectedRow 方法中实现以下代码:

- (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];
       }
  }
于 2012-06-21T08:58:58.080 回答
0

请找到我编辑过的新代码:

  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];
         }
   }

}

请注意,您的数组应该是全局的

于 2012-06-21T12:32:52.270 回答