代码之家  ›  专栏  ›  技术社区  ›  ohho

如何将SubView添加到cell.contentView?

  •  25
  • ohho  · 技术社区  · 14 年前

    A(新创建单元时):

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
            CGRect frame = CGRectMake(0, 0, 160, 50);
            UILabel *label = [[UILabel alloc] initWithFrame:frame];
            label.textAlignment = UITextAlignmentRight;
            label.text = @"9:00am";
            [cell.contentView addSubview:label];
            [label release];
        }
    
        return cell;
    }
    

    或b(每次找到单元格时):

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];    
        }
    
        CGRect frame = CGRectMake(0, 0, 160, 50);
        UILabel *label = [[UILabel alloc] initWithFrame:frame];
        label.textAlignment = UITextAlignmentRight;
        label.text = @"9:00am";
        [cell.contentView addSubview:label];
        [label release];
    
        return cell;
    }
    

    A还是B?谢谢!

    更新 解决方案(感谢您的回答):

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    
        static NSString *CellIdentifier = @"Cell";    
        UILabel *label;
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
            CGRect frame = CGRectMake(0, 0, 160, 50);
            label = [[UILabel alloc] initWithFrame:frame];
            label.textAlignment = UITextAlignmentRight;
            label.tag = 1;
            [cell.contentView addSubview:label];
            [label release];
        } else {
            label = (UILabel *) [cell viewWithTag:1];
        }
    
        label.text = [NSString stringWithFormat:@"%d", [indexPath row]];
    
        return cell;
    }
    
    2 回复  |  直到 14 年前
        1
  •  11
  •   Jacob Relkin    14 年前

    一切都是为了表现。使用a,您可以将单元与其所有子视图一起重用;使用b,您只能重用原始单元,并在每个迭代中添加一个新的子视图,而imho的性能不如re:performance。

    我说要么创造一个 UITableView 子类或使用解决方案A。

        2
  •  11
  •   Gary    14 年前

    当您像在A中一样创建单元格时,您应该只添加子视图,但是每次像在B中那样为标签等分配值。

    如果您创建自己的UITableViewCell子类,并添加自己的子视图,这个解决方案自然会失效。

    像这样。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
            CGRect frame = CGRectMake(0, 0, 160, 50);
            UILabel *label = [[UILabel alloc] initWithFrame:frame];
            label.textAlignment = UITextAlignmentRight;
            [cell.contentView addSubview:label];
            [label release];
        }
    
        // Get a reference to the label here
    
        label.text = @"9:00am";
    
        return cell;
    }
    

    这样,您就可以获得只分配一次子视图的性能优势,并且可以根据需要在子视图上设置适当的属性。