好奇怪的问题! uitablecell中的uiimageview ,有的响应gesture recognizer,有的不响应 uiimageviewuitableview 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 [cell addGestureRecognizer:self.changeAlbumTap]; UILabel * label = (UILabel *)[cell.contentView viewWithTag:1];在这两句中间加上一句[[cell subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; 加上这一句之后,cell就被清空了。 你生成cell的逻辑有问题应该先执行tablecellCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];这一句,然后在判断cell是不是存在if (!cell){ //开始创建cell}你上面那种写法每次都会创建一个新的cell,就算手势不出问题,很快内存也会爆满 不好说。向下滑动时,经常会出现点击uiimageview没反应,向上滑动时,点击uiimageview,大部分都可以,也会出现点击没输出的现象。很奇怪。 tablecellCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];这句话就是生成cell吧。sdk是ios7 , dequeueReusableCellWithIdentifier 貌似会自动判断需不需要生成新的。 你没弄明白,你的代码tablecellCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];这一句第一次运行的时候没问题,因为没有可以重用的cell,这时候的cell是空的.滚动之后再执行到这一句时就不是空的了,这个cell是重用出来的,里面已经包含了你添加的那些label,imageview什么的接下的代码你又是UILabel * label = (UILabel *)[cell.contentView viewWithTag:1]; 生成新的label,image view添又加到cell上,你每滚动一次就重复一份,不信你可以试试 你程序刚运行起来的时候,不要滚动表格试一下手势,如果这时候手势是正常的,滚动之后手势就不正常,那基本上就是cell重用的问题了 UIImageView * imageView = (UIImageView*)[cell.contentView viewWithTag:2];imageview 是重新生成的话,是不是会导致单击事件不响应?我试了一下,第一次展示的cell,没有点击没效果的问题。 tablecellCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];如果我想向cell里边添加UIImageView,应该怎么避免cell重用呢? [biv addGestureRecognizer:self.refreshAlbumTap];你这个初始化应该写在,- (void)viewDidLoad 里之类的吧,你试着给每个cell创建一个UITapGestureRecognizer应该就可以了- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"cell"; if([indexPath section]==0){ return [tableView dequeueReusableCellWithIdentifier:@"loadcell" forIndexPath:indexPath]; } else if([indexPath row]<self.data.count){ tablecellCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; cell.userInteractionEnabled = YES; [cell addGestureRecognizer:self.changeAlbumTap]; UILabel * label = (UILabel *)[cell.contentView viewWithTag:1]; [label setText:[self.data objectAtIndex:[indexPath row]]]; UIImageView * imageView = (UIImageView*)[cell.contentView viewWithTag:2]; [imageView setContentMode:UIViewContentModeScaleAspectFit]; [imageView setBackgroundColor:[UIColor grayColor]];// [imageView setImageWithURL:[NSURL URLWithString:@"http://caipiao.tao3c.com/pages/index/imagesNew/element.png"]// placeholderImage:[UIImage imageNamed:@"Default.png"]// ]; UIImageView * biv = [[UIImageView alloc]init]; CGRect fbiv = CGRectMake(20, label.frame.origin.y+50, 60, 60); biv.frame = fbiv; [biv setImage:[UIImage imageNamed:@"Default.png"]]; biv.userInteractionEnabled = YES; biv.multipleTouchEnabled = YES; [biv setTag:[indexPath row]]; UITapGestureRecognizer *refreshAlbumTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refreshAlbumTap:)]; [biv addGestureRecognizer:refreshAlbumTap1]; [cell addSubview:biv]; return cell; } else { return [tableView dequeueReusableCellWithIdentifier:@"loadcell" forIndexPath:indexPath]; }} 我在4楼不是有说了吗你要你先判断cell有没重用出来,没有重用出来才添加label那些东西tablecellCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];if (!cell){ UILabel *...... [cell.contenview addsubview ....}另外楼上说的也是一种情况,苹果的文档里明确说明了不能多个view同时添加一个手势,你要为每个cell都创建一个手势 理解。学习了。跟您确认一下,dequeueReusableCellWithIdentifier 在有storyboard里边的teblecell对应的时候,是不是不会返回nil。但是如果没有storyboard里边的teblecell,这个就有可能返回nil? 还是说一下吧,你这个创建cell的地方需要注意的几个地方。1.cell通过复用拿到以后要做 if (nil==cell) 的判断。第一行cell在渲染时,肯定是没有可以复用的cell,需要重新实例化。2.不需要再写代码给cell加什么手势,在uitableview的delegate协议方法中有didSelectedRowAtIndexPath来处理cell的点击动作,除非是你想用其它的手势来控制对cell的处理,如长按,如轻扫。3.self.refreshAlbumTap 不可以在外部定义,不是不可以,而是不合理。因为你在外部定义好后,无法在点击每一个cell时传递它们相应的数据。 有关iphone地图开发问题 Hybrid App? Object-C菜鸟求教:消息发送和类指针的区别 IOS应用发布时出现的添加ICON的问题 视图切换,怎样将view始终固定在底部? 怎样实现在分享到微信的链接中有拼接isappinstalled字段 iOS 苹果上架退回原因求解 ios10远程通信socket,会不定时断线 ios11 虚拟定位 求解决方法 ios appdelegate.m 里如何调用js UI页面效果图,请分析一下View的设计和布局控件。 iphone 传json 字符串到webSercie
UILabel * label = (UILabel *)[cell.contentView viewWithTag:1];在这两句中间加上一句
[[cell subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
加上这一句之后,cell就被清空了。
应该先执行
tablecellCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
这一句,然后在判断cell是不是存在
if (!cell)
{
//开始创建cell
}你上面那种写法每次都会创建一个新的cell,就算手势不出问题,很快内存也会爆满
tablecellCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
这句话就是生成cell吧。
sdk是ios7 , dequeueReusableCellWithIdentifier 貌似会自动判断需不需要生成新的。
tablecellCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
这一句第一次运行的时候没问题,因为没有可以重用的cell,这时候的cell是空的.
滚动之后再执行到这一句时就不是空的了,这个cell是重用出来的,里面已经包含了你添加的那些label,imageview什么的
接下的代码你又是UILabel * label = (UILabel *)[cell.contentView viewWithTag:1]; 生成新的label,image view添又加到cell上,你每滚动一次就重复一份,不信你可以试试
UIImageView * imageView = (UIImageView*)[cell.contentView viewWithTag:2];imageview 是重新生成的话,是不是会导致单击事件不响应?
我试了一下,第一次展示的cell,没有点击没效果的问题。
tablecellCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
如果我想向cell里边添加UIImageView,应该怎么避免cell重用呢?
你这个初始化应该写在,- (void)viewDidLoad 里之类的吧,你试着给每个cell创建一个UITapGestureRecognizer应该就可以了- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
if([indexPath section]==0){
return [tableView dequeueReusableCellWithIdentifier:@"loadcell" forIndexPath:indexPath];
}
else if([indexPath row]<self.data.count){
tablecellCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.userInteractionEnabled = YES;
[cell addGestureRecognizer:self.changeAlbumTap];
UILabel * label = (UILabel *)[cell.contentView viewWithTag:1];
[label setText:[self.data objectAtIndex:[indexPath row]]];
UIImageView * imageView = (UIImageView*)[cell.contentView viewWithTag:2];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setBackgroundColor:[UIColor grayColor]];
// [imageView setImageWithURL:[NSURL URLWithString:@"http://caipiao.tao3c.com/pages/index/imagesNew/element.png"]
// placeholderImage:[UIImage imageNamed:@"Default.png"]
// ];
UIImageView * biv = [[UIImageView alloc]init];
CGRect fbiv = CGRectMake(20, label.frame.origin.y+50, 60, 60);
biv.frame = fbiv;
[biv setImage:[UIImage imageNamed:@"Default.png"]];
biv.userInteractionEnabled = YES;
biv.multipleTouchEnabled = YES;
[biv setTag:[indexPath row]];
UITapGestureRecognizer *refreshAlbumTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refreshAlbumTap:)];
[biv addGestureRecognizer:refreshAlbumTap1];
[cell addSubview:biv];
return cell;
} else {
return [tableView dequeueReusableCellWithIdentifier:@"loadcell" forIndexPath:indexPath];
}
}
你要你先判断cell有没重用出来,没有重用出来才添加label那些东西
tablecellCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell)
{
UILabel *......
[cell.contenview addsubview ....
}另外楼上说的也是一种情况,苹果的文档里明确说明了不能多个view同时添加一个手势,你要为每个cell都创建一个手势
但是如果没有storyboard里边的teblecell,这个就有可能返回nil?
1.cell通过复用拿到以后要做
if (nil==cell)
的判断。第一行cell在渲染时,肯定是没有可以复用的cell,需要重新实例化。2.不需要再写代码给cell加什么手势,在uitableview的delegate协议方法中有didSelectedRowAtIndexPath来处理cell的点击动作,除非是你想用其它的手势来控制对cell的处理,如长按,如轻扫。3.self.refreshAlbumTap 不可以在外部定义,不是不可以,而是不合理。因为你在外部定义好后,无法在点击每一个cell时传递它们相应的数据。