【问题标题】:How to add a custom subview to a UITableViewCell derived class when the UITableViewCell is loaded using loadNIB使用 loadNIB 加载 UITableViewCell 时,如何将自定义子视图添加到 UITableViewCell 派生类
【发布时间】:2011-01-24 14:05:54
【问题描述】:

我正在尝试将自定义视图控件添加到我在界面构建器中设计的自定义 UITableViewCell。

现在,加载我正在使用的 UITableCellView

NSArray * loadedViews = [[NSBundle mainBundle] loadNibNamed:@"CustomSearchResultsCell" owner:self options:nil];

CustomSearchResultsCell 类的什么方法会被调用 Nib 加载视图并初始化它。我尝试使用 viewDidLoad,但 UITableViewCell 不响应此方法。在这种情况下也不会调用 initWithStyle。

TIA 尼丁

【问题讨论】:

    标签: iphone cocoa-touch uitableview


    【解决方案1】:

    从 nib 加载的视图使用initWithCoder: 进行初始化,您可以通过类似initWithFrame: 的方式实现。

    【讨论】:

      【解决方案2】:

      另外,请查看名为 AdvancedTableViewCells 的 Apple 示例代码。在此示例中,他们使用以下代码从 XIB 加载表格视图单元格:

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString *CellIdentifier = @"ApplicationCell";
      
          ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      
          if (cell == nil) {
      
              [[NSBundle mainBundle] loadNibNamed:@"IndividualSubviewsBasedApplicationCell" owner:self options:nil];
              cell = tmpCell;
              self.tmpCell = nil;     
          }
      
          return cell;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多