views:

86

answers:

2

Hi Guys,

I need to display the UIWebView under the UITableViewCell when user selects the required cell by tapping on the accessoryButtonTappedForRowWithIndexPath.

for example I have three rows, Home, Office and History, which are displayed in table view. If user selects Office then the details in webview should displayed under office's row or office's cell.

if user selectes the Home,then the details in webview should displayed under Home's row or Home's cell.

I hope that you understand my intention what I really want to say.

Thank you, Madan Mohan.

A: 

You will have to calculate the position of the cell relative to the main view (the one where your tableView sits).

Supposed you have a hierarchy like

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

It is something like

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)
VdesmedT
Could you explain more...like how to insert webView between cells
Madan Mohan
I dod not understood that you wanted to insert the webViews between cells but under cells.
VdesmedT
A: 

Another approach

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