解决方案 »
- 推荐两本书给iphone入门以及在云里雾里彷徨的童鞋...
- 如何组装一台电脑安装mac os10.5.7
- objective-c中,如何实现定时执行代码
- 判断近似圆。。。
- 各位大侠,4S激活不了,有办法吗?
- ios用json格式传数据post路径和URL应该如何写?
- 求救,如何在最短的时间内学会ios并开发相关功能
- Presenting view controllers on detached view controllers is discouraged 警告
- AppStore审核问题
- iOS版手机qq,后台杀掉应用,然后收到推送,还可以撤销,是怎么做到的呢
- 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时传递它们相应的数据。