我在nib文件中创建了一个tableview单元格,并向其中添加了一个按钮,并为名为actionButton的按钮创建了一个插座。现在,基于某些条件,我希望隐藏或取消隐藏按钮。我使用了下面的代码,所以当模型,object.hasButton属性为YES时,我取消隐藏按钮,否则显示。这段代码对我来说看起来很简单,我不认为应该有重用的问题,因为它有一个或/else条件,所以它应该隐藏为假布尔值,而取消隐藏为真布尔值条件。但是,所有的单元格,无论它们的值是什么,都会显示按钮。有没有人可以帮帮我,我一直在试着调试这个程序,但是我似乎没有弄清楚问题所在。
- (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;可能这就是问题所在,所以我把它包装在@synchronized(myObject)块中以更新hasButton bool,现在看起来没问题了。
@synchronzied(myObject){
myButton.hasButton = hasButton;
}会不会是这玩意儿?
发布于 2013-11-11 01:39:27
调用if (object.hasButton)只是检查属性hasButton是否存在。它可能确实存在,所以它返回YES!
您需要的是检查存储在该属性中的值,如下所示:
if (object.hasButton == YES)发布于 2013-11-11 02:02:35
如果您的MyObject是NSManagedObject的子类,那么它的hasButton属性类型是NSNumber *而不是BOOL。因为NSNumber是一个现有对象-非nil -无论它的值是YES还是NO,myObject.hasButton在布尔表达式中的计算结果都是TRUE。请改用[myObject.hasButton booleanValue]。同样在设置该值时使用anObject.hasButton = @(hasButton)。
https://stackoverflow.com/questions/19892561
复制相似问题