主要是cell加载的时候由于重用池的问题而出现各类的bug,虽然程序没有崩掉,可是大大影响到个人心情,下面是最主要的一个问题 ,就是cell的重用问题, cell由于从重用池中调取,没有及时删除上面的内容而致使内容的各类出现, 这里有几个解决方案.UITableView中的cell能够有不少,通常会经过重用cell来达到节省内存的目的:经过为每一个cell指定一个重用标识符(reuseIdentifier),即指定了单元格的种类,当cell滚出屏幕时,会将滚出屏幕的单元格放入重用的queue中,当某个未在屏幕上的单元格要显示的时候,就从这个queue中取出单元格进行重用。spa
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改成如下的方法 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行,而不是从cell重用队列中取出 if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //...其余代码 }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来惟一肯定cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //...其余代码 }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } else { //删除cell的全部子视图 while ([cell.contentView.subviews lastObject] != nil) { [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; } } //...其余代码 }
我是用了第二种方法搞定的,让cell的标识惟一便可,一会总结下相似于 淘宝促销商品倒计时 最近也是遇到了一些坑,但愿跟你们分享一下.code
做者:姚姚先生
连接:https://www.jianshu.com/p/3e7a0f574929
來源:简书
著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。orm