您的位置:首页 > 移动开发 > IOS开发

iOS开发 多个cell在初始化时注意重用池

2017-02-05 00:31 399 查看
多个cell在
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
方法中一定要分开来,用if或者switch,每一次上滑下拉都会调用这个方法,所以init初始化前面也必须加上

if (cell0 == nil)
来判断,不然会init多个cell
下面是一个实例-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0 && hadPublish) {
static NSString *cellIndentifier0 = @"headCell0";
HeadCurrent_View_Cell *cell0 = [tableView dequeueReusableCellWithIdentifier:cellIndentifier0];
if (cell0 == nil) {
cell0 = [[HeadCurrent_View_Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier0];
}
。。。。
return cell0;
}
else if (indexPath.section == 0 &&!hadPublish){
static NSString *cellIndentifier1 = @"headCellDefault";
UITableViewCell *cell0 = [tableView dequeueReusableCellWithIdentifier:cellIndentifier1];
if (cell0 == nil) {
cell0 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier1];
。。。。
}

cell0.selected = NO;
cell0.selectionStyle = UITableViewCellSelectionStyleNone;
return cell0;
}

static NSString *cellIndentifier = @"passengerOrderCell";
PassengerOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil) {
cell = [[PassengerOrderTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier];
}
。。。。
return cell;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息