ios - 使用自己的 Nib 子类化 UITableViewCell

标签 ios uitableview interface-builder

我想创建一个自定义的 UITableViewCell 子类,它使用 URL 连接异步加载内容。我有一个处理所有这些的 UITableViewCell 子类和一个定义单元格布局的 Nib 文件,但我在链接两者时遇到了问题。这是我在 tableView:cellForRowAtIndexPath 中使用的代码:

static NSString *FavCellIdentifier = @"FavCellIdentifier";

FavouriteCell *cell = [tableView dequeueReusableCellWithIdentifier:FavCellIdentifier];

if (cell == nil)
{
    cell = [[[FavouriteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FavCellIdentifier] autorelease];
}

cell.requestURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@=%i", URL_GET_POST_STATUS,
                                           URL_PARAM_SERIAL,
                                           [[self.favourites objectAtIndex:indexPath.row] intValue]]];

return cell;

这为 UITableViewCell 子类提供了一个请求 URL,该子类在 setRequestURL 方法中处理加载。

在 FavouriteCell 类中,我保留了 initWithStyle:reuseIdentifier: 方法,在 Nib 中,我将 FavCellIdentifier 设置为标识符,将 FavouriteCell 设置为类。现在如何让 FavouriteCell 类加载 Nib?

最佳答案

为了使用 nib/xib 文件,您将不得不以不同的方式实例化 FavouriteCell

试试这个:

  1. 确保您已将 UITableViewCell类型更改为 FavouriteCell 的子类,而不是默认的 UITableViewCell 在你的 xib 中。通过以下方式执行此操作:
    • 单击 Interface Builder 对象 Pane 中的单元格。
    • 然后,转到 Identity Inspector 选项卡并确保 Custom Class 下的 Class 选择列出 FavouriteCell
  2. File's Owner 属性更改为要在其中显示自定义 UITableViewCellUIViewController(与步骤 #1 几乎相同的过程).
  3. FavouriteCell 类型的 IBOutlet 属性添加到您的 UIViewController。随意命名(我打算将其命名为 cell)。
  4. 返回 UITableViewCell 的 xib,将 File's Owner 中 cell 属性的 IBOutlet 连接到您的自定义 UITableViewCell
  5. 使用此代码以编程方式加载单元格:

- (FavouriteCell *)tableView:(UITableView *)tableView 
       cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellId = @"FavCellId";
    FavouriteCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
    if (!cell) {
        // Loads the xib into [self cell]
        [[NSBundle mainBundle] loadNibNamed:@"FavouriteCellNibName" 
                                      owner:self 
                                    options:nil];
        // Assigns [self cell] to the local variable
        cell = [self cell];
        // Clears [self cell] for future use/reuse
        [self setCell:nil];
    }
    // At this point, you're sure to have a FavouriteCell object
    // Do your setup, such as...
    [cell setRequestURL:[NSURL URLWithString:
                  [NSString stringWithFormat:@"%@?%@=%i", 
                      URL_GET_POST_STATUS, 
                      URL_PARAM_SERIAL, 
                      [[self.favourites objectAtIndex:indexPath.row] intValue]]
     ];
    return cell;
}

关于ios - 使用自己的 Nib 子类化 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10411409/

相关文章:

ios - 如何将连接超时设置为 NSUrlConnection

ios - 如何使用 firebase 从数据库中提取用户并在 TableView 中列出它们?

ios - 获取 UITableViewCell 的高度

uitableview - 嵌入导航栏中时 iOS 7 中奇怪的 UISearchDisplayController View 偏移行为

ios - 如何使用关联数组数据源填充表头? swift XCode 6.4

iOS 8 启动图像文件 - 导航栏和状态栏

ios - 背景音频不适用于 iOS 上的 Ionic3 + Videogular2

ios - 仅在 iOS 13.1.3 (Alamofire) 中网络连接丢失

ios - 具有多行的 UItextField

ios - 具有多行 UILabel 的自定义 Tableview 单元格需要动态高度