【问题标题】:UITableViewCell content is shifting its position on scrollingUITableViewCell 内容在滚动时移动其位置
【发布时间】:2013-02-14 12:49:53
【问题描述】:

我只是用UIWebViews 显示UITableView,我的代码是

-(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 objective-c uitableview uiwebview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我认为问题来自使用“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];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-02
        • 1970-01-01
        相关资源
        最近更新 更多