【问题标题】:how to tag UIImage in UITableViewCell如何在 UITableViewCell 中标记 UIImage
【发布时间】:2011-05-13 02:01:05
【问题描述】:
我想切换 UITableViewCell 图像 - cell.imageView.image。 (例如,红色 绿色)
如果当前图像为绿色,则当用户单击 UITableViewCell 时,图像会变为红色。
一旦我设置了图像
cell.imageView.image = [UIImage imageNamed:@"Green.png"];
如何检测cell当前使用的是哪张图片?
感谢您的帮助!
【问题讨论】:
标签:
iphone
objective-c
uitableview
uiimageview
uiimage
【解决方案1】:
在imageView 本身上设置tag:
#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 一起使用
【解决方案2】:
这有点粗略,但你应该可以像下面这样设置一个 if 语句:
if ([cell.imageView.image isEqual:[UIImage imageNamed:@"Green.png"]]) {
// image is green;
} else {
// image is red;
}
我测试了它只是为了确保它可以正常工作
【解决方案3】:
我相信只需添加一个 布尔表达式,如果它是绿色的,则使其为 TRUE,如果为红色,则为 FALSE。
将这个布尔表达式设为extern type,这样它就可以是一个全局表达式。
单击图像时设置布尔值。
我相信这会有所帮助。
【解决方案4】:
我是新手 - 但我想你可以使用 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"];
}
或者您可以非常喜欢并使用ternary operator。类似于以下内容(注意下面的代码可能是错误的 - 但希望能帮助您入门!):
cell.imageView.image = ([UIImage imageNamed:@"Green.png"]) ?
cell.imageView.image = [UIImage imageNamed:@"Green.png"]; :
cell.imageView.image = [UIImage imageNamed:@"Red.png"];
让我们知道你是怎么做的。
科利亚