Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在同一个xib中放置自定义单元格和表视图?_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 如何在同一个xib中放置自定义单元格和表视图?

Ios 如何在同一个xib中放置自定义单元格和表视图?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个包含UITableView的XIB,在我创建UITableViewCell的同一个XIB中,我没有将任何类设置为cell,我只是将identifier设置为cell 现在我想将该单元格加载到tableview委托方法cellforrowatinexpath 我所做的是: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { s

我有一个包含
UITableView
的XIB,在我创建UITableViewCell的同一个XIB中,我没有将任何类设置为cell,我只是将identifier设置为cell

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

我所做的是:

- (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中获取该单元格呢?

如果您要创建自定义单元格,那么最好创建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的名称。

请在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];

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

注意:如果使用原型单元格,则不需要注册该单元格。因为原型单元格不是自定义单元格

- (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或自定义单元格

UINib *cellNib = [UINib nibWithNibName:@"YourUITableViewCell" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"cell"];
2.或者在CellForRowatineXpath中,您可以这样编写下面的代码

- (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;
}

看,我没有将任何类设置为单元格,我想使用与表视图相同的类。@rmaddyI不明白你在问什么?你能问清楚一点吗?Dx Android看到下面的答案。我没有将任何类设置为单元格,我想使用与表视图相同的类。我已经编辑了答案,要使用UITableViewCell类而不创建自定义类,只需调用cellForRowAtIndexPath无限时间。。!!!没有停止。我在numberOfRowsInSection中添加了静态10。我想这是因为加载了nib,每次都加载了tableview。