UITableView- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellID = @"cellID";
UITableViewCell * cell = [tabView dequeueReusableCellWithIdentifier:cellID];
if( nil == cell )
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
} {
UIImageView * goodsImageView;
goodsImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(10.5, 14, 297.5, 209)]autorelease];
goodsImageView.opaque = YES;
[cell.contentView addSubview:goodsImageView];
goodsImageView.image = [UIImage imageNamed:@"img_topic"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell;
}图片并不是从网上下载的 ,刚开始的时候滚动还挺流畅的,但是滚动了 一段事件后,没有刚开始这么流畅了,特别是在减速的时候,可以看到一卡一卡的现象应该如何解决?
{
return 100;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellID = @"cellID";
UITableViewCell * cell = [tabView dequeueReusableCellWithIdentifier:cellID];
if( nil == cell )
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
} {
UIImageView * goodsImageView;
goodsImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(10.5, 14, 297.5, 209)]autorelease];
goodsImageView.opaque = YES;
[cell.contentView addSubview:goodsImageView];
goodsImageView.image = [UIImage imageNamed:@"img_topic"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell;
}图片并不是从网上下载的 ,刚开始的时候滚动还挺流畅的,但是滚动了 一段事件后,没有刚开始这么流畅了,特别是在减速的时候,可以看到一卡一卡的现象应该如何解决?
解决方案 »
- 如何只设置某个controller视图为横屏
- 请教:UITextView在载入大文件时(超过8M)如何读文件才会不卡呢?
- xcode的nib文件
- 练手小作 UnitConvert1.1 发个20个码
- Cocos2d-x游戏引擎实战开发炸弹超人(TexturePacker打包、地图、LevelHelper关卡)
- Xcode6中segue问题
- 大家好 我是IOS小白
- 有没有大神写出了ios的模拟器,想在电脑上玩ios区游戏的
- gpuimage 人脸识别
- iOS app上架被拒问题5.3.4
- malloc: *** error for object 0x6c3c5a4: incorrect checksum for freed object - ob
- objective c 的对象指针到底是什么
我在做的时候没遇到卡的现象
其次,这里还是用自定义UITableViewCell吧,你这样也会影响效率。
像你这样,重用机制几乎没起到应有的效果。
再者,图片如果预先加载好了,比如UIImage都先加载好放到内存里,比如NSArray,用的时候直接拿出来用,不要临时从资源或目录中加载,也会快点。
240000/1024=234.375MB
你程序运行到最后时会有这么大的内存开销
再者就是可以使用一下“代理”来处理图像的加载,图像未显示前可以显示一个占位图,图片加载完成后,再显示出图片,主要原理还是异步加载,后台处理图片的加载,加载完成后更新主线程上的UI
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
UIImageView * goodsImageView;
goodsImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(10.5, 14, 297.5, 209)]autorelease];
goodsImageView.opaque = YES;
[cell.contentView addSubview:goodsImageView];
}
goodsImageView.image = [UIImage imageNamed:@"img_topic"];//依据你对应行对应图片,设置
这样就不会了。问题:没有充分理解tablecell的实现机制,建议仔细阅读uitableview 的文档。
你的cell的contentView上面在不停的堆UIImageView,刚开始还行,越滑动堆得越多,不卡才怪。
不过上述代码你直接复制过去肯定编译不过,因为goodsImageView是局部变量。常见的方法是通过view的tag来访问goodsImageView。
if (!cell)
{
...
goodsImageView.tag = indexPath.hash; // 这里猜测你每个indexPath中显示的都是不同的内容。不过最好你能自己定义一下tag的值,保证其唯一性。
...
}
[(UIImageView *)[cell.contentView viewWithTag:indexPath.tag] setImage:[UIImage imageNamed:@"img_topic"]];
注意:
1、不是100 cell造成的,因为你使用cell的重用机制,所以实际中就创建了几个cell,你可以在if(nil==cell)中输出log看看几个;
2、也不是加载图片的问题,因为你使用imageNamed:这种方式加载图片,图片自动会被加到内存中,便于下次使用,这是系统做的有话。
修改代码如下,请参考:
if( nil == cell )
{
// 打印创建了几个cell。
NSLog(@"index = %d", indexPath.row);
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone; UIImageView *goodsImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(10.5, 14, 297.5, 209)];
goodsImageView.opaque = YES;
*goodsImageView.tag = indexPath.row + 1;
goodsImageView.image = [UIImage imageNamed:@"img_topic"];
[cell.contentView addSubview:goodsImageView];
[goodsImageView release];
}
// 用下面的方法修改图片。
UIImageView *goodsImageView = (UIImageView *)[cell.contentView viewWithTag:indexPath.row + 1];
goodsImageView.image = [UIImage imageName:@"new_topic"];