1

我只是显示一个UITableViewwith UIWebViews 而我的代码是

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

当我单击第一个单元格时,UIWebView应该显示,再次单击以隐藏...它正在工作。

但我的问题是,如果我滚动UITableView,内容(UIWebView)正在改变它indexPath并显示在最后indexPath一个tableview.

为什么会这样?

4

2 回答 2

0

发生这种情况是因为一旦初始化了单元格,它们就会以不同的顺序出队。iOS 会自动执行此操作。如果您希望它们停止出队过程,请每次都初始化它们,或者单独处理它们。

编辑

这是我的意思的一个例子:

-(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;
}
于 2013-02-14T13:22:24.273 回答
-1

我认为问题来自使用“dequeueReusableCellWithIdentifier”:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

您应该将所有访问过的单元格存储在 NSMutableDictionary 中,然后您可以使用它们:

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];
}
于 2013-02-14T14:01:40.260 回答