ios - UITableViewCell 内容在滚动时改变了它的位置

标签 ios objective-c uitableview uiwebview

我只是展示一个带有UIWebViewUITableView,我的代码是

-(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 并显示在最后一个 tableview 的 indexPath

为什么会这样?

最佳答案

之所以会发生这种情况,是因为一旦您初始化了单元格,它们就会以不同的顺序出列。 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;
}

关于ios - UITableViewCell 内容在滚动时改变了它的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14875234/

相关文章:

ios - 将 block 存储在实例变量中有什么用

ios - 通过 Url Scheme 重新打开 iOS 应用程序 - 状态保存

iphone - iOS 上可以使用本地存储来持久化缓存吗

iphone - 如何检测何时在 uikeyboard 类型 "ABC"上点击 "UIKeyboardTypeNumbersAndPunctuation"按钮

ios - Restkit 映射字符串数组

swift: prepareForSegue indexPathForSelectedRow

ios - 尝试为同一索引路径使多个单元格出队,这是 Swift 不允许的

ios - alloc 和 facebook init 的问题

ios - Flutter-带有搜索栏的自定义条形应用栏

ios - 以编程方式使用 Autolayout 布局自定义 UITableViewCell