[英]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.