Iphone 如何在UITableViewCell中标记UIImage
我想切换UITableViewCell image-cell.imageView.image。(如红绿) 如果当前图像为绿色,则当用户单击UITableViewCell时,图像将变为红色 一旦我设置了图像Iphone 如何在UITableViewCell中标记UIImage,iphone,objective-c,uitableview,uiimageview,uiimage,Iphone,Objective C,Uitableview,Uiimageview,Uiimage,我想切换UITableViewCell image-cell.imageView.image。(如红绿) 如果当前图像为绿色,则当用户单击UITableViewCell时,图像将变为红色 一旦我设置了图像 cell.imageView.image = [UIImage imageNamed:@"Green.png"]; 如何检测单元格当前使用的图像 谢谢你的帮助 在imageView本身上设置: #define IMAGE_TAG_GREEN 50 #define IMAGE_TAG_RED
cell.imageView.image = [UIImage imageNamed:@"Green.png"];
如何检测单元格当前使用的图像
谢谢你的帮助 在imageView
本身上设置:
#define IMAGE_TAG_GREEN 50
#define IMAGE_TAG_RED 51
-(UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
static NSString *CELL_ID = @"some_cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID];
if(cell == nil) {
//do setup here...
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_ID] autorelease];
cell.imageView.tag = //some logic here...
}
if(cell.imageView.tag == IMAGE_TAG_GREEN) {
//...
} else {
//...
}
return cell;
}
由于tag
是从UIView
继承的属性,因此不能将其与UIImage
本身一起使用,但可以与UIImageView
一起使用,在imageView
本身上设置:
#define IMAGE_TAG_GREEN 50
#define IMAGE_TAG_RED 51
-(UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
static NSString *CELL_ID = @"some_cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID];
if(cell == nil) {
//do setup here...
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_ID] autorelease];
cell.imageView.tag = //some logic here...
}
if(cell.imageView.tag == IMAGE_TAG_GREEN) {
//...
} else {
//...
}
return cell;
}
由于
tag
是从UIView
继承的属性,因此不能将其与UIImage
本身一起使用,但可以与UIImageView
一起使用,这有点粗糙,但您应该能够设置如下所示的if语句:
if ([cell.imageView.image isEqual:[UIImage imageNamed:@"Green.png"]]) {
// image is green;
} else {
// image is red;
}
我对它进行了测试,只是为了确保它工作正常它有点粗糙,但您应该能够设置如下所示的if语句:
if ([cell.imageView.image isEqual:[UIImage imageNamed:@"Green.png"]]) {
// image is green;
} else {
// image is red;
}
我对它进行了测试,只是为了确保它工作正常我对这方面还不熟悉,但我想你可以使用if/then语句
If (cell.imageView.image = [UIImage imageNamed:@"Green.png"]) {
cell.imageView.image = [UIImage imageNamed:@"Green.png"];
} else
{
cell.imageView.image = [UIImage imageNamed:@"Red.png"];
}
或者你可以去真正的幻想和使用。类似于以下内容(请注意,下面的代码可能是错误的,但希望能帮助您入门!)
让我们知道你是怎么做的
Kolya我对这一点还不熟悉,但我想你可以使用if/then语句
If (cell.imageView.image = [UIImage imageNamed:@"Green.png"]) {
cell.imageView.image = [UIImage imageNamed:@"Green.png"];
} else
{
cell.imageView.image = [UIImage imageNamed:@"Red.png"];
}
或者你可以去真正的幻想和使用。类似于以下内容(请注意,下面的代码可能是错误的,但希望能帮助您入门!)
让我们知道你是怎么做的
Kolya我相信只要添加一个布尔表达式,如果它是绿色,则为真;如果它是红色,则为假。 将此布尔表达式设为外部类型,使其成为全局表达式。 单击图像时设置布尔值。
我相信这会有所帮助。我相信只要添加一个布尔表达式,如果它是绿色,则为真;如果它是红色,则为假。 将此布尔表达式设为外部类型,使其成为全局表达式。 单击图像时设置布尔值。
我相信这会有所帮助。这比像我这样简单地比较
int
s要贵得多。这比像我这样简单地比较int
s要贵得多。正如前面所说的,不要比较图像,这是昂贵的,甚至在桌面上也是愚蠢的。标签是首选的方式,正如苹果在这里使用的例子:如前所述,不要比较图像,即使是在桌面上,也绝对是愚蠢的。标记是首选的方式,例如苹果在这里使用: