繁体   English   中英

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

[英]how to make a UIView added to UITableViewCell content view fit within its bounds?

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

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

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

我是否必须为自定义 NIB 视图创建一个框架,但在这种情况下,我不确定如何设置坐标。 它会与 UITableViewCell 相关吗? 如果自定义视图高度由于它的 UILabel 自动换行而发生变化,那么我假设我必须单独在 UITableView 中手动计算这个高度? (也许我应该 go 来创建动态/自定义视图并放弃使用 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;
}

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

我为我的自定义单元格创建了UITableViewCell的子类。 “我的自定义单元”。 然后我为其创建一个 NIB 文件,并在此 NIB 文件中插入UITableViewCell并将其类型更改为“MyCustomCell”,并为其提供一个与 class 名称同名的标识符。 然后我将所有子视图插入到我的单元格中。 全部在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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM