简体   繁体   中英

UITableViewCell content is shifting its position on scrolling

I'm just showing a UITableView with UIWebView s and my code is

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath 
    *)indexPath

   {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;

        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 400, 30)];
        [lable setBackgroundColor:[UIColor clearColor]];
        [lable setTextColor:[UIColor darkGrayColor]];
        lable.tag = 333;
        [lable setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0f]];
        //[lable setText:[arrayOfSectionTitles objectAtIndex:indexPath.row]];
        [cell addSubview:lable];


        if (indexPath.row == 0) {

            UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(15, 45, 600, 250)];
            webView.tag = 1001;
            [webView setBackgroundColor:[UIColor greenColor]];
            NSString *urlAddress = @"http://www.roseindia.net";
            NSURL *url = [NSURL URLWithString:urlAddress];
            NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
            [webView loadRequest:requestObj];
            [cell.contentView addSubview:webView];
            [webView setHidden:YES];

        }
        else if (indexPath.row == 1){

            UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(15, 45, 600, 100)];
            webView.tag = 1002;
            [webView setBackgroundColor:[UIColor greenColor]];
            NSString *urlAddress = @"http://www.roseindia.net";
            NSURL *url = [NSURL URLWithString:urlAddress];
            NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
            [webView loadRequest:requestObj];
            [cell.contentView addSubview:webView];
            [webView setHidden:YES];

        }
        else if (indexPath.row == 2){

            UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(15, 45, 600, 200)];
            webView.tag = 1003;
            [webView setBackgroundColor:[UIColor greenColor]];
            NSString *urlAddress = @"http://www.roseindia.net";
            NSURL *url = [NSURL URLWithString:urlAddress];
            NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
            [webView loadRequest:requestObj];
            [cell.contentView addSubview:webView];
            [webView setHidden:YES];

        }

    }

    UILabel* label = (UILabel*)[cell viewWithTag:333];

[label setText:[arrayOfSectionTitles objectAtIndex:indexPath.row]];

    tableview.separatorColor = [UIColor clearColor];

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BOOL isSelected = ![self cellIsSelected:indexPath];

    // Store cell 'selected' state keyed on indexPath
    NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
    [selectedIndexes setObject:selectedIndex forKey:indexPath];

    UITableViewCell *cell = (UITableViewCell *)[tableview cellForRowAtIndexPath:indexPath];


    if (indexPath.row == 0) {
        UIWebView* webView = (UIWebView*)[cell viewWithTag:1001];

        if (!cellSelected1) {
            [webView setHidden:NO];
            cellSelected1 = YES;
        }
        else {
            [webView setHidden:YES];
            cellSelected1 = NO;
        }
    }
    else if (indexPath.row == 1) {
        UIWebView* webView = (UIWebView*)[cell viewWithTag:1002];

        if (!cellSelected2) {
            [webView setHidden:NO];
            cellSelected2 = YES;
        }
        else {
            [webView setHidden:YES];
            cellSelected2 = NO;
        }

    }
    else if (indexPath.row == 2) {
        UIWebView* webView = (UIWebView*)[cell viewWithTag:1003];

        if (!cellSelected3) {
            [webView setHidden:NO];
            cellSelected3 = YES;
        }
        else {
            [webView setHidden:YES];
            cellSelected3 = NO;
        }

    }


    [tableview reloadData];
}

When I click on first cell, the UIWebView should show, click again to hide...it's working.

But my problem is if I scroll the UITableView , the content ( UIWebView ) is changing its indexPath and showing in last indexPath of the tableview .

Why is that happening?

It is happening because once you initialized the cells, they are just being dequed in a different order. iOS does this automatically. If you want them to stop the dequeing process, either init them everytime, or treat them individually.

EDIT

Here is an example of what I meant:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;

    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 400, 30)];
    [lable setBackgroundColor:[UIColor clearColor]];
    [lable setTextColor:[UIColor darkGrayColor]];
    lable.tag = 333;
    [lable setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0f]];
    //[lable setText:[arrayOfSectionTitles objectAtIndex:indexPath.row]];
    [cell addSubview:lable];


    if (indexPath.row == 0) {

        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(15, 45, 600, 250)];
        webView.tag = 1001;
        [webView setBackgroundColor:[UIColor greenColor]];
        NSString *urlAddress = @"http://www.roseindia.net";
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [webView loadRequest:requestObj];
        [cell.contentView addSubview:webView];
        [webView setHidden:YES];

    }
    else if (indexPath.row == 1){

        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(15, 45, 600, 100)];
        webView.tag = 1002;
        [webView setBackgroundColor:[UIColor greenColor]];
        NSString *urlAddress = @"http://www.roseindia.net";
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [webView loadRequest:requestObj];
        [cell.contentView addSubview:webView];
        [webView setHidden:YES];

    }
    else if (indexPath.row == 2){

        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(15, 45, 600, 200)];
        webView.tag = 1003;
        [webView setBackgroundColor:[UIColor greenColor]];
        NSString *urlAddress = @"http://www.roseindia.net";
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [webView loadRequest:requestObj];
        [cell.contentView addSubview:webView];
        [webView setHidden:YES];

    }

}

UILabel* label = (UILabel*)[cell viewWithTag:333];

[label setText:[arrayOfSectionTitles objectAtIndex:indexPath.row]];

tableview.separatorColor = [UIColor clearColor];

return cell;
}

I think problem comes from using "dequeueReusableCellWithIdentifier":

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

You should store all visited cells in NSMutableDictionary, then you can use them:

NSNumber * cellKey = [NSNumber numberWithInt:indexPath.row];
UITableViewCell *cell = [allCells objectForKey:cellKey];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    // add all you need to cell

    // save cell in dictionary
    [allCells setObject:cell forKey:cellKey];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM