2012-06-21 47 views
1

我是新来的iPhone开发者,如何获取UIPopOver选择行文本

在我的应用程序,当我在一个按钮上点击酥料饼。

我想要做的是,当我选择在酥料饼任何一行单元格应标有向右的箭头图像,如:

cell.accessoryType = UITableViewCellAccessoryCheckmark;

,我想获取该选择行的文本。

Inshort我想实现复选框在我的弹出窗口中,如果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在此之后。

在此先感谢!

+0

请张贴您的代码。从问题来看,您似乎希望我们为您实施该代码。 –

+0

我没有写过任何'didSelectRowAtIndexPath'我不知道该写什么。 – Krunal

回答

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

Dude你的代码正在工作,但它应该像复选框,当用户再次勾选勾选行时,它应该是'[cell setAccessoryType:UITableViewCellAccessoryNone];'并从数组中删除该文本。 – Krunal

+0

请看我的新回答 – 2012-06-21 12:56:02

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

} 

请注意,您的阵列应该是全球性的

+0

代码崩溃显示'索引超出范围' – Krunal

+0

请参阅新代码:) – 2012-06-21 12:56:48

+0

属性boolvalue未找到错误 – Krunal