UITableViewCell 重用 备忘

uitableviewcell重用备忘

目前遇到的重用分两种情况
1. tableviewcell布局一样,内容不一样
这种情况一般自定义一个uitableviewcell,加载所有控件,比如uiimageview,uilabel等等,
imageview和label的赋值放在if (cell == nil)外面操作即可,这是最基本重用

2. tableviewcell布局不一样,有2种或者多种布局
这种情况需要自定义多种cell,根据不同情况获取对应布局的cell,代码如下:

static NSString *RootCellIdentifier = @"DirectoryViewRootCell";
    static NSString *LeafCellIdentifier = @"DirectoryViewLeafCell";

    NSMutableDictionary *dic = [arrayDirectory objectAtIndex:indexPath.row];
    int level = [[dic valueForKey:@"level"] intValue];
    
    DoubleSeparatorCell *cell;
    
    if (level > 1) {
        cell = [tableView dequeueReusableCellWithIdentifier:LeafCellIdentifier];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:RootCellIdentifier];
    }
    
    if (cell == nil) {
        if (level > 1) {
            cell = [[[DoubleSeparatorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LeafCellIdentifier] autorelease];
            //blahblahblah....进行各自布局
            [cell addSubview: A];
} else {
            cell = [[[DoubleSeparatorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RootCellIdentifier] autorelease];
//blahblahblah....进行各自布局
             [cell addSubview: B];
}

if (level > 1) {
//根据tag取出控件,进行赋值
//取出A赋值
} else {
//根据tag取出控件,进行赋值
//取出B赋值
}

猜你喜欢

转载自zl4393753.iteye.com/blog/1752468