2010-09-23 26 views
0

當用戶通過點擊accessoryButtonTappedForRowWithIndexPath選擇所需的單元格時,我需要在UITableViewCell下顯示UIWebView。當我點擊accessoryButtonTappedForRowWithIndexPath時如何在UITableViewCells之間插入UIWebView?

例如我有三行,即家庭,辦公室和歷史,它們以表格形式顯示。如果用戶選擇Office,則webview中的詳細信息應顯示在辦公室的行或辦公室的單元格下。

如果用戶選擇了首頁,則webview中的詳細信息應顯示在首頁的行或首頁的單元格中。

我希望你明白我的意圖,我真的想說什麼。

謝謝, Madan Mohan。

回答

0

您將不得不計算單元格相對於主視圖(tableView座標所在的位置)的位置。

認爲你有一個像

mainView 
|--myTable (UITableView*) 
| |--Cell1 
| |--Cell2 
| |--Cell3 
|--myWebView (UIWebView*) 

一個層次是像

float y = myTable.frame.origin.y+selectedCell.frame.origin.y+selectedCell.frame.size.height; 

myWebView.frame = CGRectMake(myTable.frame.origin.x, y, mainView.bounds.size.width, mainView.bounds.size.height-y) 
+0

你能解釋更多...怎麼樣細胞之間插入web視圖 – 2010-09-23 08:57:14

+0

我不知道你想插入單元格之間的webView但下細胞。 – VdesmedT 2010-09-23 15:09:57

0

另一種方法

@implementation TableViewController 

-(id) initWithStyle:(UITableViewStyle)style { 
    if (self = [super initWithStyle:style]) { 
     self.editing = NO; 
     selectedCell = -1; 
    } 
    return self; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return 3; 
} 

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.row == selectedCell) { 
     return 150.0; 
    } 
    return 50.0; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *CellIdentifier = @"Cell"; 
    if (indexPath.row == selectedCell) { 
     CellIdentifier = @"CustomCell"; 
    } 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     if (indexPath.row == selectedCell) { 
      cell = [[[CellWithWebView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     } else { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     } 

     cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    } 

    cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", indexPath.row]; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    selectedCell = indexPath.row; 
    [tableView beginUpdates]; 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:NO]; 
    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:NO]; 
    [tableView endUpdates]; 
    [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 
} 

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 
    selectedCell = -1; 
    [tableView beginUpdates]; 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:NO]; 
    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:NO]; 
    [tableView endUpdates]; 
    [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 
} 
相關問題