2012-06-21 45 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