UITableView复用导致数据折叠
2015-10-26 15:49
393 查看
之前一直xib自定义cell了,今天心血来潮整了个纯代码自定义cell,上下滑动时发现复用的cell原数据未清空,导致cell展示的数据发生重叠,贴一下出错代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier=@"OrderListDetailCell";
UITableViewCell *cell = [self.tableview dequeueReusableCellWithIdentifier:identifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
UIImageView *goodsImg = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100, 70)];
UILabel *goodsName = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5, 0, UIScreenWidth-(5+goodsImg.frame.size.width+5+5), 40)];
goodsName.font = [UIFont systemFontOfSize:14];
goodsName.numberOfLines = 2;
UILabel * goodsCount = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5, 50, 100, 15)];
goodsCount.font = [UIFont systemFontOfSize:14];
UILabel *goodsPrice = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5+100+5, 50, 180, 20)];
goodsPrice.font = [UIFont systemFontOfSize:14];
NSDictionary *goodsDic = (NSDictionary *)[_cellGoodsInfo objectAtIndex:indexPath.row];
//展示商品详情
[goodsImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LoadImageURL,[goodsDic objectForKey:@"goodsImage"]]]];
goodsName.text =[NSString stringWithFormat:@"%@",[goodsDic objectForKey:@"goodsName"]];
goodsCount.text =[NSString stringWithFormat:@"共:%@ 元",[goodsDic objectForKey:@"goodsTotalPrice"]];
goodsPrice.text =[NSString stringWithFormat:@"件数:%@",[goodsDic objectForKey:@"goodsCount"]];
[cell.contentView addSubview:goodsImg];
[cell.contentView addSubview:goodsName];
[cell.contentView addSubview:goodsPrice];
[cell.contentView addSubview:goodsCount];
cell.backgroundColor = RGBCOLOR(236, 236, 236, 1);
return cell;
}
修改后为如下代码,问题解决:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier=@"OrderListDetailCell";
UITableViewCell *cell = [self.tableview dequeueReusableCellWithIdentifier:identifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIImageView *goodsImg = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100, 70)];
UILabel *goodsName = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5, 0, UIScreenWidth-(5+goodsImg.frame.size.width+5+5), 40)];
goodsName.font = [UIFont systemFontOfSize:14];
goodsName.numberOfLines = 2;
UILabel * goodsCount = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5, 50, 100, 15)];
goodsCount.font = [UIFont systemFontOfSize:14];
UILabel *goodsPrice = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5+100+5, 50, 180, 20)];
goodsPrice.font = [UIFont systemFontOfSize:14];
[goodsImg setTag:1];
[goodsName setTag:2];
[goodsCount setTag:3];
[goodsPrice setTag:4];
[cell.contentView addSubview:goodsImg];
[cell.contentView addSubview:goodsName];
[cell.contentView addSubview:goodsPrice];
[cell.contentView addSubview:goodsCount];
}
UIImageView *a1 = (UIImageView *)[cell.contentView viewWithTag:1];
UILabel *a2 = (UILabel *)[cell.contentView viewWithTag:2];
UILabel *a3 = (UILabel *)[cell.contentView viewWithTag:3];
UILabel *a4 = (UILabel *)[cell.contentView viewWithTag:4];
NSDictionary *goodsDic = (NSDictionary *)[_cellGoodsInfo objectAtIndex:indexPath.row];
//展示商品详情
[a1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LoadImageURL,[goodsDic objectForKey:@"goodsImage"]]]];
a2.text =[NSString stringWithFormat:@"%@",[goodsDic objectForKey:@"goodsName"]];
a3.text =[NSString stringWithFormat:@"共:%@ 元",[goodsDic objectForKey:@"goodsTotalPrice"]];
a4.text =[NSString stringWithFormat:@"件数:%@",[goodsDic objectForKey:@"goodsCount"]];
cell.backgroundColor = RGBCOLOR(236, 236, 236, 1);
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier=@"OrderListDetailCell";
UITableViewCell *cell = [self.tableview dequeueReusableCellWithIdentifier:identifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
UIImageView *goodsImg = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100, 70)];
UILabel *goodsName = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5, 0, UIScreenWidth-(5+goodsImg.frame.size.width+5+5), 40)];
goodsName.font = [UIFont systemFontOfSize:14];
goodsName.numberOfLines = 2;
UILabel * goodsCount = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5, 50, 100, 15)];
goodsCount.font = [UIFont systemFontOfSize:14];
UILabel *goodsPrice = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5+100+5, 50, 180, 20)];
goodsPrice.font = [UIFont systemFontOfSize:14];
NSDictionary *goodsDic = (NSDictionary *)[_cellGoodsInfo objectAtIndex:indexPath.row];
//展示商品详情
[goodsImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LoadImageURL,[goodsDic objectForKey:@"goodsImage"]]]];
goodsName.text =[NSString stringWithFormat:@"%@",[goodsDic objectForKey:@"goodsName"]];
goodsCount.text =[NSString stringWithFormat:@"共:%@ 元",[goodsDic objectForKey:@"goodsTotalPrice"]];
goodsPrice.text =[NSString stringWithFormat:@"件数:%@",[goodsDic objectForKey:@"goodsCount"]];
[cell.contentView addSubview:goodsImg];
[cell.contentView addSubview:goodsName];
[cell.contentView addSubview:goodsPrice];
[cell.contentView addSubview:goodsCount];
cell.backgroundColor = RGBCOLOR(236, 236, 236, 1);
return cell;
}
修改后为如下代码,问题解决:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier=@"OrderListDetailCell";
UITableViewCell *cell = [self.tableview dequeueReusableCellWithIdentifier:identifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIImageView *goodsImg = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100, 70)];
UILabel *goodsName = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5, 0, UIScreenWidth-(5+goodsImg.frame.size.width+5+5), 40)];
goodsName.font = [UIFont systemFontOfSize:14];
goodsName.numberOfLines = 2;
UILabel * goodsCount = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5, 50, 100, 15)];
goodsCount.font = [UIFont systemFontOfSize:14];
UILabel *goodsPrice = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5+100+5, 50, 180, 20)];
goodsPrice.font = [UIFont systemFontOfSize:14];
[goodsImg setTag:1];
[goodsName setTag:2];
[goodsCount setTag:3];
[goodsPrice setTag:4];
[cell.contentView addSubview:goodsImg];
[cell.contentView addSubview:goodsName];
[cell.contentView addSubview:goodsPrice];
[cell.contentView addSubview:goodsCount];
}
UIImageView *a1 = (UIImageView *)[cell.contentView viewWithTag:1];
UILabel *a2 = (UILabel *)[cell.contentView viewWithTag:2];
UILabel *a3 = (UILabel *)[cell.contentView viewWithTag:3];
UILabel *a4 = (UILabel *)[cell.contentView viewWithTag:4];
NSDictionary *goodsDic = (NSDictionary *)[_cellGoodsInfo objectAtIndex:indexPath.row];
//展示商品详情
[a1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LoadImageURL,[goodsDic objectForKey:@"goodsImage"]]]];
a2.text =[NSString stringWithFormat:@"%@",[goodsDic objectForKey:@"goodsName"]];
a3.text =[NSString stringWithFormat:@"共:%@ 元",[goodsDic objectForKey:@"goodsTotalPrice"]];
a4.text =[NSString stringWithFormat:@"件数:%@",[goodsDic objectForKey:@"goodsCount"]];
cell.backgroundColor = RGBCOLOR(236, 236, 236, 1);
return cell;
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- IOS开发第三方语音-微信语音
- 解析iOS开发中的FirstResponder第一响应对象