1

如何使添加到 UITableViewCell 内容视图的 UIView 适合其范围?

也就是说,我创建了一个 NIB 文件(上面有 3 个标签),并希望将其用于 UITableView 中每个单元格的显示。我在 cellForRowAtIndexPath 方法中将基于自定义 NIB 的视图添加到单元格的内容视图中,但是我看到的最终结果是一 (1) 个基于自定义 NIB 的视图(不像我预期的那样在 tableview 中有多个)。

如何安排每个自定义视图整齐地适合每个 UITableViewCell?另请注意,自定义 NIB 视图中的标签有自动换行。

我是否必须为自定义 NIB 视图创建一个框架,但在这种情况下,我不确定如何设置坐标。它会与 UITableViewCell 相关吗?如果自定义视图高度可以由于它的 UILabel 自动换行而改变,那么我假设我必须单独在 UITableView 中手动计算这个高度?(也许我应该去创建动态/自定义视图并放弃使用 InterfaceBuilder/NIB 的概念)

- (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];
    }

    UIView *detailedView = [[[NSBundle mainBundle] loadNibNamed:@"DetailedAppointView" owner:self options:nil] objectAtIndex:0];
    [cell.contentView addSubview:detailedView];  // DOESN'T SEE TO WORK

    return cell;
}
4

1 回答 1

1

如果我需要实现自定义 UITableViewCell,我就是这样做的。

UITableViewCell我为我的自定义单元格创建了一个子类。“我的自定义单元”。然后我为它创建一个NIB文件,在这个NIB文件中我插入UITableViewCell并将它的类型更改为“MyCustomCell”,并给它一个与类名同名的标识符。然后我将所有子视图插入到我的单元格中。全部在IB。

在我把我的手机拉到我喜欢的地方之后:-)我以下列方式使用它:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCustomCell";
    static NSString *CellNib = @"MyCustomCell";

    MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (MyCustomCell *)[nib objectAtIndex:0];
    }

    //Manipulate your custom cell here

    return cell;
}
于 2011-06-12T21:51:51.333 回答