Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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 setImage在UITableViewCell上不工作_Ios_Objective C_Uitableview - Fatal编程技术网

Ios setImage在UITableViewCell上不工作

Ios setImage在UITableViewCell上不工作,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个UITableViewCell,带有一个imageview,它充当一个“复选框”,单击时,它将选中或取消选中。我在我的UITableViewCell -(void) toggleCheck { if(checked) { checked = NO; [self.imageView setImage:[UIImage imageNamed: @"empty-check.png"]]; } else { checked = YE

我有一个
UITableViewCell
,带有一个
imageview
,它充当一个“
复选框
”,单击时,它将
选中
取消选中
。我在我的
UITableViewCell

-(void) toggleCheck {
    if(checked) {
        checked = NO;
        [self.imageView setImage:[UIImage imageNamed: @"empty-check.png"]];
    } else {
        checked = YES;
    [self.imageView setImage:[UIImage imageNamed: @"check.png"]];
    }
}
然后在我的
视图控制器中执行以下操作:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        CheckCell *cell = (CheckCell *)[tableView dequeueReusableCellWithIdentifier:@"CheckCell"];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CheckCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

        [cell toggleCheck]; // check the box
    }
}
但是UIImageView图像没有改变。我也尝试过做
[tableView reloadData]
,但运气不好


你知道为什么不更新吗?

试着这样做吧。。。因为当您在委托方法之外访问
单元格.imageView
时,可能未设置图像

我也不确定你是否访问了正确的图像。如果您使用的是
self.imageView
您需要首先将imageView声明为a
@属性。
这样做会给u带来问题,因为当表中的行数增加时,u正在重用表单元格

这里也不需要编写单独的函数切换,因此您可以跳过它并将其写入
didSelect
方法

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
           CheckCell *cell = (CheckCell *)[tableView dequeueReusableCellWithIdentifier:@"CheckCell"];
        if (cell == nil) 
        {
             NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CheckCell" owner:self options:nil];
             cell = [nib objectAtIndex:0];
        }
        if(checked) 
        {
           checked = NO;
           [cell.imageView setImage:[UIImage imageNamed: @"empty-check.png"]];
        }
        else 
        {
           checked = YES;
           [cell.imageView setImage:[UIImage imageNamed: @"check.png"]];
        }
    }

您不应该在
didSelectRowAtIndexPath
方法中创建新单元格。
在方法中创建新单元格没有任何用处。
您可以使用
[yourtable cellforrowatinexpath:indexPath]获取所选单元格
考虑这个代码,例如


您的实现中存在问题。选中状态不应是UITableViewCell实例的一部分。它应该是你模型的一部分。请注意,“表视图”将重用单元格,因此在“表视图”单元格中保持选中状态将无法按预期工作。必须将选中状态存储在模型中,并使用该模型的属性更新单元格的选中状态。“表格视图”单元格应仅用于绘制单元格,而不用于维护任何状态,因为当表格中有许多单元格时,它将被重用。

为什么要在
toggleCheck
方法中修改
checked
呢?checked是一个BOOL,用于跟踪是否选中t以及在哪里定义了imageView?是否应该将其添加到单元格而不是self?您是否中断了代码的这一部分以确保其已被调用?是的,正在调用“toggleView”方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        CheckCell *cell = (CheckCell *)[tableView cellForRowAtIndexPath:indexPath];
        [cell toggleCheck]; // check the box
    }
}