Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.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 如何设置此自定义UITableViewCell_Ios_Uitableview - Fatal编程技术网

Ios 如何设置此自定义UITableViewCell

Ios 如何设置此自定义UITableViewCell,ios,uitableview,Ios,Uitableview,我接管了一个iOS项目,必须将视图列表重构为UITableView。我使用的是故事板,并且有子类UITableViewCell。一个子类称为MenuItemCell,它有一个headerLabel、detailLabel和priceLabel,它们是在情节提要中设置并在MenuItemCell中配置的属性。我可以通过cellForAtIndexPath如下操作: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAt

我接管了一个iOS项目,必须将视图列表重构为UITableView。我使用的是故事板,并且有子类UITableViewCell。一个子类称为MenuItemCell,它有一个headerLabel、detailLabel和priceLabel,它们是在情节提要中设置并在MenuItemCell中配置的属性。我可以通过cellForAtIndexPath如下操作:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  static NSString *MenuItemCellIdentifier=@"MenuItemCell";
  id dic=self.tmpMenu.listItems[indexPath.row];
  if([dic isKindOfClass:[MenuItem class]]){
    MenuItemCell *cell = [self.menuTV dequeueReusableCellWithIdentifier:MenuItemCellIdentifier];
    MenuItem *menuItem=(MenuItem *)dic;
    cell.menuItem=menuItem;
    
    cell.headerLabel.text=menuItem.header;
    cell.headerLabel.numberOfLines=0;

    cell.priceLabel.text=menuItem.price;

     // how to handle this custom spotView
     if([menuItem hasInstoreImage]){
        UIView *instoreImageDot=[self circleWithColor:[UIColor redColor] radius:4];
        [cell.spotView addSubview:instoreImageDot];  // ON SCROLLING, this populates to all the different table cells
      }

    return cell;
  }
  return nil;
}

最后一点是有一个名为spotView的自定义UIView。目前,我正在控制器中通过circleWithColor在代码中创建此圆圈,并尝试添加到[cell.spotView],但滚动会导致此圆圈填充到不同的表格单元格中。我应该如何设置?我已向自定义视图添加了一个方法,但这也遇到了同样的问题。

如果要重用单元格,则需要通知tableView删除自定义视图

 if([menuItem hasInstoreImage]){
    UIView *instoreImageDot=[self circleWithColor:[UIColor redColor] radius:4];
    [cell.spotView addSubview:instoreImageDot]; 
  }else{
    //remove it if condition is not met
    //or You can add a place holder view instead
  }

如果重新使用单元格,则需要通知tableView删除自定义视图

 if([menuItem hasInstoreImage]){
    UIView *instoreImageDot=[self circleWithColor:[UIColor redColor] radius:4];
    [cell.spotView addSubview:instoreImageDot]; 
  }else{
    //remove it if condition is not met
    //or You can add a place holder view instead
  }

如果重新使用单元格,则需要通知tableView删除自定义视图

 if([menuItem hasInstoreImage]){
    UIView *instoreImageDot=[self circleWithColor:[UIColor redColor] radius:4];
    [cell.spotView addSubview:instoreImageDot]; 
  }else{
    //remove it if condition is not met
    //or You can add a place holder view instead
  }

如果重新使用单元格,则需要通知tableView删除自定义视图

 if([menuItem hasInstoreImage]){
    UIView *instoreImageDot=[self circleWithColor:[UIColor redColor] radius:4];
    [cell.spotView addSubview:instoreImageDot]; 
  }else{
    //remove it if condition is not met
    //or You can add a place holder view instead
  }

现在发生的情况是,iOS在您滚动时正在重用单元格,并且一些重用的单元格已经将instoreImageDot视图添加为子视图

您真的不应该在CellForRowatineXpath方法中进行布局工作。它只应用于将可重用单元出列,然后为该单元设置数据。所有的布局都应该由单元格本身来处理

不要在控制器中创建instoreImageDot。在自定义单元格中添加一个方法-类似(用C#编写,但应易于翻译):

此外,在自定义单元格中,实现prepareForReuse方法,并在此方法中,从单元格中删除instoreImageDot视图,以便只能添加一次

- (void)prepareForReuse {
    if([self.subviews containsObject:instoreImageDot])
    {
       [instoreImageDot removeFromSuperview];
     }

    [super prepareForReuse]; 
}
现在,您的cellForRowAtIndexPath方法可以如下所示:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  static NSString *MenuItemCellIdentifier=@"MenuItemCell";
  id dic=self.tmpMenu.listItems[indexPath.row];
  if([dic isKindOfClass:[MenuItem class]]){
    MenuItemCell *cell = [self.menuTV dequeueReusableCellWithIdentifier:MenuItemCellIdentifier];
    MenuItem *menuItem=(MenuItem *)dic;

cell.UpdateCell(menuItem, [menuItem hasInstoreImage]);

    return cell;
  }
  return nil;
}

现在发生的情况是,iOS在您滚动时正在重用单元格,并且一些重用的单元格已经将instoreImageDot视图添加为子视图

您真的不应该在CellForRowatineXpath方法中进行布局工作。它只应用于将可重用单元出列,然后为该单元设置数据。所有的布局都应该由单元格本身来处理

不要在控制器中创建instoreImageDot。在自定义单元格中添加一个方法-类似(用C#编写,但应易于翻译):

此外,在自定义单元格中,实现prepareForReuse方法,并在此方法中,从单元格中删除instoreImageDot视图,以便只能添加一次

- (void)prepareForReuse {
    if([self.subviews containsObject:instoreImageDot])
    {
       [instoreImageDot removeFromSuperview];
     }

    [super prepareForReuse]; 
}
现在,您的cellForRowAtIndexPath方法可以如下所示:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  static NSString *MenuItemCellIdentifier=@"MenuItemCell";
  id dic=self.tmpMenu.listItems[indexPath.row];
  if([dic isKindOfClass:[MenuItem class]]){
    MenuItemCell *cell = [self.menuTV dequeueReusableCellWithIdentifier:MenuItemCellIdentifier];
    MenuItem *menuItem=(MenuItem *)dic;

cell.UpdateCell(menuItem, [menuItem hasInstoreImage]);

    return cell;
  }
  return nil;
}

现在发生的情况是,iOS在您滚动时正在重用单元格,并且一些重用的单元格已经将instoreImageDot视图添加为子视图

您真的不应该在CellForRowatineXpath方法中进行布局工作。它只应用于将可重用单元出列,然后为该单元设置数据。所有的布局都应该由单元格本身来处理

不要在控制器中创建instoreImageDot。在自定义单元格中添加一个方法-类似(用C#编写,但应易于翻译):

此外,在自定义单元格中,实现prepareForReuse方法,并在此方法中,从单元格中删除instoreImageDot视图,以便只能添加一次

- (void)prepareForReuse {
    if([self.subviews containsObject:instoreImageDot])
    {
       [instoreImageDot removeFromSuperview];
     }

    [super prepareForReuse]; 
}
现在,您的cellForRowAtIndexPath方法可以如下所示:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  static NSString *MenuItemCellIdentifier=@"MenuItemCell";
  id dic=self.tmpMenu.listItems[indexPath.row];
  if([dic isKindOfClass:[MenuItem class]]){
    MenuItemCell *cell = [self.menuTV dequeueReusableCellWithIdentifier:MenuItemCellIdentifier];
    MenuItem *menuItem=(MenuItem *)dic;

cell.UpdateCell(menuItem, [menuItem hasInstoreImage]);

    return cell;
  }
  return nil;
}

现在发生的情况是,iOS在您滚动时正在重用单元格,并且一些重用的单元格已经将instoreImageDot视图添加为子视图

您真的不应该在CellForRowatineXpath方法中进行布局工作。它只应用于将可重用单元出列,然后为该单元设置数据。所有的布局都应该由单元格本身来处理

不要在控制器中创建instoreImageDot。在自定义单元格中添加一个方法-类似(用C#编写,但应易于翻译):

此外,在自定义单元格中,实现prepareForReuse方法,并在此方法中,从单元格中删除instoreImageDot视图,以便只能添加一次

- (void)prepareForReuse {
    if([self.subviews containsObject:instoreImageDot])
    {
       [instoreImageDot removeFromSuperview];
     }

    [super prepareForReuse]; 
}
现在,您的cellForRowAtIndexPath方法可以如下所示:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  static NSString *MenuItemCellIdentifier=@"MenuItemCell";
  id dic=self.tmpMenu.listItems[indexPath.row];
  if([dic isKindOfClass:[MenuItem class]]){
    MenuItemCell *cell = [self.menuTV dequeueReusableCellWithIdentifier:MenuItemCellIdentifier];
    MenuItem *menuItem=(MenuItem *)dic;

cell.UpdateCell(menuItem, [menuItem hasInstoreImage]);

    return cell;
  }
  return nil;
}

thx-这更有意义,让我更新我的自定义单元格,看看剩余部分是否工作thx-这更有意义,让我更新我的自定义单元格,看看剩余部分是否工作thx-这更有意义,让我更新我的自定义单元格,看看剩余部分是否工作thx-这更有意义,让我更新一下我的自定义手机,看看是否还能用。你还需要帮助吗?thx meda-我想我已经解决了。非常感谢你的帮助;将所有布局移到自定义视图中。你还需要帮助吗?thx meda-我想我已经解决了。非常感谢你的帮助;将所有布局移到自定义视图中。你还需要帮助吗?thx meda-我想我已经解决了。非常感谢你的帮助;将所有布局移到自定义视图中。你还需要帮助吗?thx meda-我想我已经解决了。非常感谢你的帮助;将所有布局移动到自定义视图中