0

セルが開かれているかどうかを確認する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;
}

ここnameisRead、、isFavは、すべてのデータを格納するplistから取得した値です。もちろん、ユーザーがセルを開くと、「未読」の画像は消えます。

問題は、未読とお気に入りの両方を表示したいということです。上記のコードでは、未読のものだけが表示されます。

これを達成するにはどうすればよいですか?多分愚かな何かが欠けています!

4

5 に答える 5

1

セルにはimageViewが1つだけあります。これを試して

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"];
于 2012-08-19T10:10:03.577 に答える
1

UITableViewCellをサブクラス化して、希望どおりにレイアウトしないのはなぜですか?これは簡単です。私はyaをこれを行う方法を示すプロジェクトにしました。

簡単な要約:

と呼ばれる新しいクラスを作成します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

を保持するVCにそのクラスをインポートしますTableView。の親クラスTableViewCellMyTableViewCell(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;

ここにプロジェクト

于 2012-08-19T19:40:22.233 に答える
0

デフォルトの実装UITableViewCellでは、一度に2つのイメージをサポートしていません。をサブクラス化し、表示したい画像の1つにUITableViewCell独自の画像を追加する必要があります。UIImageView

于 2012-08-19T09:22:15.950 に答える
0

親愛なる友人の助けを借りて解決しました、そして私は次のようなことをすることになりました

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;
    }
于 2012-08-21T13:38:23.117 に答える
-2

テーブルビューセルに2つの画像ビューを追加します。以下のコードをcellForRowAtIndexPathに実装する必要があります(デフォルトでは、1つの画像ビューがあります)。これでは、2つのサブビューを取得し、それらをコンテンツビューとして追加する必要があります。

-(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;

}

于 2012-08-19T10:51:30.817 に答える