表格選中效果展示,表格選中展示

來源:互聯網
上載者:User

表格選中效果展示,表格選中展示

 

原始碼地址:https://github.com/EizoiOS/ImagePickerClass

如下:

 

在儲存格上增加一張圖片,此處的執行個體是把背景陰影也一起做為一張圖片,平時可以通過一個VIEW來設定它的透明度;還要儲存格給它賦於相應的點擊事件;

 1:此為儲存格視圖,用於處理選中的效果 

EiaoAsset.h檔案

@interface EizoAsset : UIView{ UIImageView * selectedView; // 建立一個ImageView -- 用來添加到選中的這個圖片上 BOOL selected; // 建立一個bool值 -- 用來標識有無選中}@property (strong,nonatomic)ALAsset * asset; // 通過ALAsset獲得相應的資源@property (assign , nonatomic) id parent;-(id)initWithAsset:(ALAsset*)asset;-(BOOL)selected;-(void)toggleSelection;@end
EiaoAsset.m檔案:#import "EizoAsset.h"@interface EizoAsset(){    UIImageView * assetImageView; // 展示這個每一張圖片的ImageView}@end@implementation EizoAsset-(id)initWithAsset:(ALAsset *)asset {    if (self = [super initWithFrame:CGRectMake(0, 0, 0, 0)]) {        self.asset = asset;        assetImageView = [[UIImageView alloc]init];        assetImageView.contentMode = UIViewContentModeScaleAspectFill;        assetImageView.image = [UIImage imageWithCGImage:[self.asset thumbnail]];        // ALAsset的thumbnail是圖片的縮圖        [self addSubview:assetImageView];        selectedView = [[UIImageView alloc]init];        selectedView.image = [UIImage imageNamed:@"Select.png"];        selectedView.hidden = YES;        [self addSubview:selectedView];    }    return self;}- (BOOL)selected {    return !selectedView.hidden;}-(void)setSelected:(BOOL)_selected {    [selectedView setHidden:!_selected];}// 在儲存格 也就是上一個層面添加了一個手勢 點擊變化選中的狀態 也就是改變這個bool值- (void)toggleSelection {    selectedView.hidden = !selectedView.hidden;}-(void)setFrame:(CGRect)frame {    [super setFrame:frame];    assetImageView.frame = self.bounds;    selectedView.frame = self.bounds;}@end

注意:這邊已經把選中的效果去加上去,只是讓它先隱藏起來,還設定它點擊觸發的事件;ALAsset就是資料模型,用來存一些圖片的資料;

 

2:儲存格檔案:

eiaoAssetCell.h檔案

#import <UIKit/UIKit.h>#define topMargin 5@interface EizoAssetCell : UITableViewCell// 儲存格的建立方法-(instancetype)initWithAssets:(NSArray *)assets reuseIdentifier:(NSString *)identifier;// 傳入這個assets的方法-(void)setAssets:(NSArray *)assets;// @property (nonatomic,retain) NSArray * linesAssets; // 傳入的圖片的數組 -- 應該是所有的圖片@endeiaoAssetCell.m 檔案:#import "EizoAssetCell.h"#import "EizoAsset.h"@implementation EizoAssetCell-(instancetype)initWithAssets:(NSArray *)assets reuseIdentifier:(NSString *)identifier { if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]) { self.linesAssets = assets; } return self;}-(void)layoutSubviews { CGFloat h = self.bounds.size.height - topMargin; CGFloat margin = (self.bounds.size.width - 4 * h) / 5.0; CGRect frame = CGRectMake(margin,topMargin, h, h); for(EizoAsset * eizoAsset in self.linesAssets) { eizoAsset.frame = frame; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:eizoAsset action:@selector(toggleSelection)]; [eizoAsset addGestureRecognizer:tap]; [self addSubview:eizoAsset]; frame.origin.x = frame.origin.x + frame.size.width + margin; }}@end

注意:這邊最為重要的是在layoutSubviews裡面的代碼,UITapGestureRecognizer把target指向上面建立的EizoAsset視圖,這樣便可以調用上面建立的選中及反選的事件;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.