iphone - 如何使添加到 UITableViewCell 内容 View 的 UIView 适合其范围?

标签 iphone ios uitableview addsubview

如何使添加到 UITableViewCell 内容 View 的 UIView 适合其边界?

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

我该如何安排,以便每个自定义 View 都整齐地适合每个 UITableViewCell?另请注意,自定义 NIB View 中的标签具有自动换行功能。

我是否必须为自定义 NIB View 创建框架,但在那种情况下我不确定如何设置坐标。它会与 UITableViewCell 相关吗?如果自定义 View 高度由于它的 UILabel 自动换行而改变,那么我假设我必须分别在 UITableView 中手动计算这个高度? (也许我应该转到动态/自定义 View 创建并放弃使用 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,我就是这样做的。

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

关于iphone - 如何使添加到 UITableViewCell 内容 View 的 UIView 适合其范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6324831/

相关文章:

ios - 快速将 UIPageControl 添加到 titleView

iphone - 将 Twilo sdk 集成到 iOS 时出现异常

ios - 这个核心情节代码可以在 iPhone 上运行,但不能在 iPad 上运行

ios - 在tableview ios6.1中加载图像

ios - 动态生成按钮时应用程序移动缓慢

iphone - iOS环境下的可靠时差

iphone - 如何检测 iPhone 状态栏中的触摸?

ios - 如何在不分配标签属性的情况下识别 View 对象

ios - 使 UITextView 父级成为它自己的 inputAccessoryView

ios - 如何在单击表格 View 中的单元格时获取 map View 上的注释?