[英]Toggle 2 different images in a tableView's cell
我有一个tableView来检查是否已打开一个单元格:如果是,它将显示一个空单元格(没有图像),如果否,它将显示一个小图像以表明该单元格尚未被读取(打开)。
现在,我在我的应用程序中具有“收藏夹”功能,我想在tableView中显示一个小图像,指出该单元格是一个收藏夹,因此用户可以从表中快速识别那些添加到收藏夹的图像。
我正在尝试在cellForRowAtIndexPath
方法中
NSDictionary *item = [rows objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:@"name"];
if ([[item valueForKey:@"isRead"] boolValue] == NO) {
cell.imageView.image = [UIImage imageNamed:@"unread.png"];
} else {
if ([[item valueForKey:@"isFav"] boolValue] == YES){
cell.imageView.image = [UIImage imageNamed:@"favorite.png"];
}
else{
cell.imageView.image = nil;
}
cell.imageView.image = nil;
}
其中name
, isRead
, isFav
是从我存储所有数据的plist中isFav
的值。 当然,当用户打开一个单元格时,“未读”图像就会消失。
现在的问题是我想同时显示未读和收藏夹。 在上面的代码中,它仅显示未读的内容。
我该怎么做呢? 我想念一些愚蠢的东西!
单元格只有一个imageView。 尝试这个
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIImageView *imageView1=[[UIImageView alloc]init];
imageView1.frame=CGRectMake(0, 0,50,cell.frame.size.height);
imageView1.tag=1000;
[cell.contentView addSubview:imageView1];
UIImageView *imageView2=[[UIImageView alloc]init];
imageView2.frame=CGRectMake(cell.frame.size.width-50, 0, 50,cell.frame.size.height);
imageView2.tag=2000;
[cell.contentView addSubview:imageView2];
}
UIImageView *imageView=(UIImageView *)[cell.contentView viewWithTag:1000];
imageView.backgroundColor=[UIColor redColor];
imageView.image=[UIImage imageNamed:@"unread.png"];
UIImageView *imageView2=(UIImageView *)[cell.contentView viewWithTag:2000];
imageView2.backgroundColor=[UIColor greenColor];
imageView2.image=[UIImage imageNamed:@"favorite"];
为什么不只是继承UITableViewCell并按照您想要的方式进行布局? 这很容易做到。 我做了一个项目,说明了如何做到这一点。
简要总结:
创建一个名为MyTableViewCell
的新类。
MyTableViewCell.h:
#import <UIKit/UIKit.h>
@interface MyTableViewCell : UITableViewCell
@property (nonatomic, strong) IBOutlet UIImageView *image1;
@property (nonatomic, strong) IBOutlet UIImageView *image2;
@end
MyTableViewCell.m:
#import "MyTableViewCell.h"
@implementation MyTableViewCell
@synthesize image1, image2;
@end
将该类导入保存TableView
的VC中。 将TableViewCell
的父类更改为MyTableViewCell
(在Interface Builder中)。 确保重用标识符的名称正确,然后在cellForRowAtIndexPath
:
static NSString *CellIdentifier = @"TwoImageCell";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell.image1 setImage:[UIImage imageNamed:@"lion.png"]];
[cell.image2 setImage:[UIImage imageNamed:@"mtlion.png"]];
// Configure the cell...
return cell;
默认实现UITableViewCell
不一次支持两个图像。 您需要对UITableViewCell
进行子类化,并为想要显示的图像之一添加自己的UIImageView
。
我在亲爱的朋友的帮助下解决了问题,最终我做了类似的事情
int state = 0;
if ([[item valueForKey:@"isRead"] boolValue] == NO)
state += 1;
if ([[item valueForKey:@"isFav"] boolValue] == YES)
state += 2;
switch (state){
case 1:
cell.imageView.image = [UIImage imageNamed:@"unread.png"];
break;
case 2:
cell.imageView.image = [UIImage imageNamed:@"favorite.png"];
break;
case 3:
cell.imageView.image = [UIImage imageNamed:@"unreadAndFavorite.png"];
break;
default:
cell.imageView.image = nil;
break;
}
在表视图单元格中添加两个图像视图。 您必须在cellForRowAtIndexPath中实现以下代码(默认情况下只有一个图像视图)。 在此,您必须获取两个子视图并将其添加为内容视图。
-(UITableViewCell *)tableView:(UITableView *)tableViewL cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableViewL dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIImageView *imageView1=[[UIImageView alloc]init];
imageView1.frame=CGRectMake(0, 0,50,cell.frame.size.height);
imageView1.tag=1000;
[cell.contentView addSubview:imageView1];
UIImageView *imageView2=[[UIImageView alloc]init];
imageView2.frame=CGRectMake(cell.frame.size.width-50, 0, 50,cell.frame.size.height);
imageView2.tag=2000;
[cell.contentView addSubview:imageView2];
}
UIImageView *imageView=(UIImageView *)[cell.contentView viewWithTag:1000];
imageView.image=[UIImage imageNamed:@"myImage1.png"];
UIImageView *imageView2=(UIImageView *)[cell.contentView viewWithTag:2000];
imageView2.image=[UIImage imageNamed:@"myImage2.png"];
return cell;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.