contentView.subViews

本文详细解析了UITableView在iOS应用开发中的重用机制问题。针对自定义cellView时出现的界面元素重叠现象,提供了有效的解决方案,并展示了如何正确地分离界面加载与数据加载过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近项目中要用到UITableView,在网上搜了一遍看大家的用法都差不多,我是通过[cell.contentView addSubview:cellView]来添加我自定义的cellView,但我发现了一个问题,在UITableView滚动过一屏后选中后面的一项时会发现和前面的竟然重叠到一起了,后面通过打印[cell.contentView.subviews count]发现随着滚动条的滚动count不断增大。最后通过以下语句解决问题:

             for(UIView *view in cell.contentView.subViews)

               {

                   if([view isKindOfClass:[UIView class]])

                    {

                         [view removeFromSuperview];

                     }

                 }

问题虽然解决了但在和同事的讨论时发现这个问题可能是用法不当造成的。网上找到的代码一般都是

    static NSString *tag = @"cell";

         if(cell == nil)

       {

           cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tag]autorelease];

        }

  ……

然后在后面向cell添加数据,网上都是向cell中自带的textLabel添加数据所以不会出现重叠的现象,而我则是向自定义的cellView添加数据,这样只要有滚动它就向里面添加cellView,所以cellView越加越多。后面通过将界面加载与数据加载解决,具体实现如下:

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

{

     static NSString *tag = @"cell";//由于用了两种界面模式所以两个tag

     static NSString *tag1 = @"cell1";

 

   UITableViewCell * cell;

   CGRect cellFram = CGRectMake(0 , 0, 320 ,100);

    if([indexPath row] == [cells count] )//numberOfRowsInSection中我返回的是[cells count]+1,最后有一个“加载更多。。。”项

   {

       cell = [tableView dequeueReusableCellWithIdentifier: tag1];

      if( cell == nil )

       {

         cell = [[[UITableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier: tag1] autorelease];

         cell.frame = cellFrame;

         cell.backgroundColor = [UIColor clearColor];

        //界面加载

          cell.textLabel.frame = cellFrame;

          cell.textLabel.textAlignment = UITextAlignmentCenter;

           cell.textLabel.backgroundColor = [UIColor clearColor];

           cell.textLabel.tag = 50;

 

           UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(75,40,24,24)];

           [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];

            activityIndicator.tag = 51;

            [cell.contentView addSubview: activityIndicator];

            [activityIndicator release];

        }

//数据加载

   if([indexPath row] == [items count])  //最后一条

     {

           UILabel *more = (UILabel *)[cell.contentView viewWithTag: 50];

            more.text = @"已经是最后一条";

           

            UIActivityIndicatorView *activity = (UIActivityIndicatorView *)[cell.contentView viewWithTag:51];

            [activity stopAnimating];

      }

    else

     {

           UILabel *more = (UILabel *)[cell.contentView viewWithTag: 50];

            more.text = @"正在加载更多……";

           

            UIActivityIndicatorView *activity = (UIActivityIndicatorView *)[cell.contentView viewWithTag:51];

            [activity startAnimating];

     }

    }

else

{

      cell = [tableView dequeueReusableCellWithIdentifier: tag];

        if(cell == nil)

          { 

             cell = [[[UITableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier: tag] autorelease];

             cell.frame = cellFrame;

             cell.backgroundColor = [UIColor clearColor];

              //界面加载

               RWXZCellView *cellView = [[RWXZCellView alloc] initWithFrame:cellFrame];

              cellView.backgroundColor = [UIColor clearColor];

                cellView.tag = 100;

               [cell.contentView addSubview:cellView];

                [cellView release];

            }

                //加载数据

               RWXZCellView *cellView =(RWXZCellView *)[cell.contentView viewWithTag:100];

              [cellView setItemInfo:[cells objectAtIndex:[indexPath row]]];

   }

 

return cell;

}

 这样就不会出现重叠啦!

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值