Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.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 5中的自定义单元格问题_Ios_Uitableview_Custom Controls - Fatal编程技术网

iOS 5中的自定义单元格问题

iOS 5中的自定义单元格问题,ios,uitableview,custom-controls,Ios,Uitableview,Custom Controls,我有一个自定义的UITableViewCell子类,我读到这应该是为iOS 5加载自定义单元格的正确方法: - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomCell *cell = [tv dequeueReusableCellWithIdentifier:@"customCell"]; if (cell == nil) {

我有一个自定义的
UITableViewCell
子类,我读到这应该是为iOS 5加载自定义单元格的正确方法:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomCell *cell = [tv dequeueReusableCellWithIdentifier:@"customCell"];

   if (cell == nil) {
      cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"customCell"];

      // Configure cell
      cell.nameLabel.text = self.customClass.name;
   }

   return cell;
}
但当我运行应用程序时,标签文本不会显示。不过,我也试过这样做:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"customCell"];

   if (cell == nil) {
      NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];

   for (UIView *view in views) {
        if([view isKindOfClass:[UITableViewCell class]])
        {
            cell = (CustomCell *)view;
        }
    }

    // Configure cell
    ((CustomCell *)cell).nameLabel.text = self.customClass.name;
}

return cell;
}

这样会显示标签,但在
loadNibName:
方法中会设置任何
reuseIdentifier
。加载自定义单元格的最佳方式应该是什么?我需要支持iOS 5+。第一种方法可能不起作用,因为我需要在
initWithStyle:
方法中配置单元格的标签和样式,而不是在表视图的方法中


谢谢

只需使用此代码并重试即可

static NSString *cellIdentifier=@"cell";
    YourCustomCell *cell = (YourCustomCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        NSString *customeCellName = [NSString stringWithFormat:@"%@",[YourCustomCell class]];

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:customeCellName owner:self options:nil];

        for (id currentObject in topLevelObjects)
        {
            if ([currentObject isKindOfClass:[UITableViewCell class]])
            {
                cell =  (YourCustomCell *) currentObject;
                break;
            }
        }
    }
    cell.nameLabel.text = self.customClass.name; // Make sure self.customclass.name have value.
    return cell;

代码中的错误是,
cell.namelab.text
仅在
if(cell==nil){…}
情况下设置,通常不设置

加载自定义单元的最简单方法是

  • 如果在nib文件中定义了单元格,请使用注册表项nb:forCellReuseIdentifier:,或者
  • 如果单元格是以编程方式创建的,请使用注册表类:forCellReuseIdentifier:,或者
  • 使用故事板并将“CustomCell”定义为表视图的“Prototype Cell”
例如(在
viewDidLoad
中):

然后
dequeueReusableCellWithIdentifier
将始终返回一个单元格,以便简化为

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomCell *cell = [tv dequeueReusableCellWithIdentifier:@"customCell"];
   cell.nameLabel.text = self.customClass.name;
   return cell;
}
使用此代码(NewCustomCell是您的自定义UITableViewCell类名)


快乐编码…:)

检查是否在此self.customClass.name中获取数据与调用这些方法相关的问题。。。例如,如果要在
UITableViewCell
子类中为自定义单元格设置自定义字体或其他与样式相关的内容,而不是在
cellforrowatinexpath:
方法中进行设置,您可以如何进行设置?@AppsDev:如果单元格是从nib文件加载的,您可以使用编码器覆盖
initWithCoder:
awakeFromNib
。要修改单元格中的UI元素,
awakeFromNib
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomCell *cell = [tv dequeueReusableCellWithIdentifier:@"customCell"];
   cell.nameLabel.text = self.customClass.name;
   return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NewCustomCell *cell = (NewCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"NewCustomCell" owner:self options:nil];
        cell=[nib objectAtIndex:0];
    }

    return cell;
}