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

    xiaoxiao2021-03-26  21

    多个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; }

    转载请注明原文地址: https://ju.6miu.com/read-659548.html

    最新回复(0)