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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.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 单元格重用,隐藏根据某些条件显示单元格内的按钮_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 单元格重用,隐藏根据某些条件显示单元格内的按钮

Ios 单元格重用,隐藏根据某些条件显示单元格内的按钮,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我在nib文件中创建了一个tableview单元格,并在其中添加了一个按钮,还创建了一个名为actionButton的按钮出口。现在,基于某些条件,我希望按钮被隐藏或取消隐藏。我使用了下面的代码,因此当模型object.hasButton属性为YES时,我将取消隐藏按钮,并以其他方式显示。这段代码在我看来很简单,我认为不应该存在重用问题,因为它具有“要么/要么”条件,所以它应该为假布尔条件隐藏,为真布尔条件取消隐藏。但是,所有单元格,无论其值是多少,都会显示按钮。有人能帮我一下吗?我一直在试着调

我在nib文件中创建了一个tableview单元格,并在其中添加了一个按钮,还创建了一个名为actionButton的按钮出口。现在,基于某些条件,我希望按钮被隐藏或取消隐藏。我使用了下面的代码,因此当模型object.hasButton属性为YES时,我将取消隐藏按钮,并以其他方式显示。这段代码在我看来很简单,我认为不应该存在重用问题,因为它具有“要么/要么”条件,所以它应该为假布尔条件隐藏,为真布尔条件取消隐藏。但是,所有单元格,无论其值是多少,都会显示按钮。有人能帮我一下吗?我一直在试着调试这个,但我似乎没有找到问题所在

- (UITableViewCell*)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  MyObject * object = [[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER forIndexPath:indexPath];

  cell.delegate = self;
  if (cell == nil){
    cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_IDENTIFIER];
    cell.delegate = self;

  }


  cell.tableItem = object;
  UIButton *button = cell.actionButton;

  if(object.hasButton){
    [button setHidden:NO];
  }else{
    [button setHidden:YES];

  }

  return cell;

}
问题似乎出在线程上。我在managedObjectContext performBlock:andWait方法中做了一些操作,如下所示

 [newChildContext performBlockAndWait:^{
  count = [newChildContext countForFetchRequest:req error:NULL];
      if(count > 0)
        hasButton = YES;
      else
        hasButton = NO;
  }];
    myObject.hasButton = hasButton;
然后像这样更新模型

 [newChildContext performBlockAndWait:^{
  count = [newChildContext countForFetchRequest:req error:NULL];
      if(count > 0)
        hasButton = YES;
      else
        hasButton = NO;
  }];
    myObject.hasButton = hasButton;
可能这就是问题所在,所以我将它包装在@synchronized(myObject)块中以更新hasButton bool,现在看起来一切正常

  @synchronzied(myObject){
      myButton.hasButton = hasButton;
    }
可能是这个东西吗

调用if(object.hasButton)只是检查属性hasButton是否存在。它可能确实存在,所以它返回YES

您需要检查存储在该属性中的值,如下所示:

if (object.hasButton == YES)

如果您的
MyObject
NSManagedObject
的子类,那么它的
hasButton
属性类型是
NSNumber*
而不是
BOOL
。由于
NSNumber
是一个现有对象-非零-无论其值是
YES
还是
NO
myObject。在布尔表达式中,hasButton
的计算结果为
TRUE
。改用
[myObject.hasButton booleanValue]
。另外,在设置该值时,使用
anObject.hasButton=@(hasButton)

如果表未重新加载,则可能会出现问题。请更新完整代码以进行调试。如果您已检查interface builder中的插座连接和
对象中的值,请检查上述更正。我不确定这是否是一个确切的问题。我想你说的是objective c对象,但我的hasButton是一个基本类型,所以我认为它不会像你说的那样工作。当且仅当hasObject属性为true或BOOL YES时,条件if(object.hasButton)将计算为true。当设置断点并观察状态时,事实是否证实了您的假设?ie,else块是否运行过?抱歉,我花了一些时间进行调试。我修复了它,我想它与并发性有关。正如你所说,我尝试了调试,它显示了那些我没有设置任何值的对象的真实bool值。显然,这似乎是同步的问题,因此同步化块解决了这个问题。myObject不是NSManagedObject子类,请参见上文ManagedObject上下文仅用于计算此处的对象数。myObject是NSObject的一个子类,因此hasButton是BOOL属性。基于计数,它只需为myObject设置hasButton的值。