您的位置:首页 > 其它

关于tableview的一些bug

2015-03-24 18:39 246 查看
about tableview bug

内存压力而奔溃

原因:cell没有被复用

一般来说,对于cell的复用是这样子的:

关于cell的复用:这里根据屏幕的高度,先创建第一个cell,第二个cell。当tabbleview向上滑动的时候,第一个cell渐渐移除屏幕,创建第三个cell,从屏幕下方进入显示。当第一个cell全部移除屏幕的时候会进入复用池,第四个cell就会复用第一个cell。这里tablveiw会创建3个cell,然后其他的就会复用。

(1)cell xib上的设置



(2)代码里面设置表的cell,给cell添加identifier,cell xib和这个identifier要一致

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *identifier = @"starShowCell";
StarShowTableViewCell *cell = [starTableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StarShowTableViewCell" owner:nil options:nil];
for (id oneObject in nib) {
if ([oneObject isKindOfClass:[StarShowTableViewCell class]]) {
cell = (StarShowTableViewCell*)oneObject;
}
}

}
}


可我的问题是:

可是,我的代码里面这样子写,cell并没有复用,而是每次都创建了新的cell,每次都会进入if(cell==nil)里面去创建新的cell。
原因:StarShowTableViewCell
*cell = [starTableView
dequeueReusableCellWithIdentifier:identifier];
我用xib关联了一个starTableView,然后写了一个实例变量 uitableview
*starTableView;这里拿到的tableview和我显示tableview不是一样的。本来当前页面只有一个tableview,但是代码里面有两个了。







erminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray insertObject:range:: Out of bounds'
*** First throw call stack:

数组越界。cell复用,没有重置数据。没有刷新reloaddata

使用tableview的时候老是出现cell的数据重复,是因为我们复用cell却没有重置数据。就像tableview你给了他数据,还要reloaddata才行。

- (void)awakeFromNib
{

}
-(void)setStarModel:(StarShowModel
*)starModel{

_starModel = starModel;

}

-(void)layoutSubviews{

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: