【问题标题】:How to get place custom cell and table view in same xib?如何在同一个xib中放置自定义单元格和表格视图?
【发布时间】:2016-03-26 04:23:52
【问题描述】:

我有包含 UITableView 的 XIB,并且在同一个 XIB 中我创建了 UITableViewCell,我没有为单元格设置任何类,我只是为单元格设置了标识符。

现在我想将该单元格加载到 tableview 委托方法 cellForRowAtIndexPath

我做的是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    return cell;
}

但它会创建不包含我创建的 xib 的新单元格。

那么我如何从表格视图具有的同一个 xib 中获取该单元格?

【问题讨论】:

  • 我没有为单元格设置任何类,我想使用与表格视图相同的类。@rmaddy
  • 我不明白你在问什么?你能问清楚一点吗?
  • Dx Android 看下面的答案。

标签: ios objective-c uitableview


【解决方案1】:

如果您正在创建自定义单元格,那么最好创建 UITableViewCell 类型的自定义类,然后在方法中实例化您的 UITableViewCell 类。如果您不想创建自定义类,那么下面是可以帮助您的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CustomCellIdentifier = @"SimpleTableItem";
   UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
   if (cell == nil)
   {
     cell = (UITableViewCell *)[[[NSBundle mainBundle] loadNibNamed:@"TMPartnerLoyalty" owner:self options:nil] objectAtIndex:0];
   }
   return cell;

}  

这里,TMPartnerLoyalty 是 xib 的名称。

【讨论】:

  • 我没有为单元格设置任何类,我想使用与表格视图相同的类。
  • 我已经编辑了答案,使用 UITableViewCell 类而不创建自定义类。
  • 它只是调用 cellForRowAtIndexPath 无限时间..!!!没有停止。我将静态 10 添加到 numberOfRowsInSection。我认为这是因为加载 nib,每次都加载 tableview。
【解决方案2】:

请在 viewdidload 中写下这段代码,我希望它对你有用。

NSString *nibName = @"give your xib name here that contain Tablecell";
NSString *simpleTableIdentifier = @"SimpleTableItem";
UINib *customCellNib = [UINib nibWithNibName:nibName bundle:nil];
[tableView registerNib:customCellNib forCellReuseIdentifier:simpleTableIdentifier];

【讨论】:

    【解决方案3】:

    如果使用原型单元格,标识符名称必须与 dequeueReusableCellWithIdentifier 名称相同

    注意:如果使用prototype cell,则不需要注册cell。因为Prototype cell不是CustomCell

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    {
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
       if(cell == nil)
       {
         cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
       }
       cell.textLabel.text = @"";
       return cell;
    }
    

    如果您使用 UITableViewcell(自定义单元格),请按照以下步骤操作

    1.在 viewDidLoad 中你必须注册 UITableViewCell 或 Custom Cell

    UINib *cellNib = [UINib nibWithNibName:@"YourUITableViewCell" bundle:[NSBundle mainBundle]];
    [self.tableView registerNib:cellNib forCellReuseIdentifier:@"cell"];
    

    2.OR 在 cellForRowAtIndexPath 你可以这样写下面的代码

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      static NSString *customTableIdentifier=@"cell";
      YourUITableViewCell *cell=(YourUITableViewCell*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier];
      if (cell==nil)
      {
        NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:@"YourUITableViewCell" owner:self options:nil];
        cell=[nibs objectAtIndex:0];
      } 
      cell.yourTableViewCellLabel.text = @"";
      cell.yourTableViewCellTextField.text = @"";
      cell.yourTableViewCellButton.text = @"";
      return cell;
    }
    

    【讨论】:

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