首页 > 代码库 > iOS用SegmentController来切换tableView的时候 reloadData会造成数据重复创建 解决办法
iOS用SegmentController来切换tableView的时候 reloadData会造成数据重复创建 解决办法
把在Cell上面创建的控件放在Cell==nil的判断里面,这样,每次进来就会先判断有没有,就可以避免数据重复创建,导致视图越盖越厚
if (cell==nil) {
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIDD];
self.inviteLabel=[[UILabelalloc]initWithFrame:CGRectMake(10,5,[UIScreenmainScreen].bounds.size.width-20,120)];
self.inviteLabel.numberOfLines=0;
self.inviteLabel.text=cell_info.title;
self.inviteLabel.textColor=LABEL_TEXTCOLOR;
int width=100;
int height=40;
self.button=[[UIButtonalloc]initWithFrame:CGRectMake([UIScreenmainScreen].bounds.size.width/2-width/2,120+10, width, height)];
self.button.backgroundColor=[UIColorredColor];
[self.buttonsetBackgroundImage:[UIImageimageNamed:cell_info.cp]forState:UIControlStateNormal];
NSLog(@"cell_info.cp:%@",cell_info.cp);
//增加长按手势
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)];
[self.inviteLabeladdGestureRecognizer:longPress];
longPress.minimumPressDuration=1.0;
[cell.contentViewaddSubview:self.button];
[cell.contentViewaddSubview:self.inviteLabel];
cell.backgroundColor=CELL_BACK_COLOR;
}
iOS用SegmentController来切换tableView的时候 reloadData会造成数据重复创建 解决办法