//prepareForReuse會在cell被reuse時呼叫,在這裡把cell初始化就不會出現重覆顯示的問題
-(void)prepareForReuse{
[super prepareForReuse];
self.accessoryType = UITableViewCellAccessoryNone;
}
下面寫的方法雖然好用,但卻會造成Cell不被重覆使用的問題,造成大資料量時會吃光記憶體,所以正確的邏輯應該是,重覆使用相同的Cell,在prepareForReuse中把Cell的顯示畫面清空,再重新給他應該要有的畫面,而資料的正確對應關係,可以用Index.row與Data Array的index來對應!
//給UITableView裡的每個Cell一個唯一個Identifier
-(void)prepareForReuse{
[super prepareForReuse];
self.accessoryType = UITableViewCellAccessoryNone;
}
下面寫的方法雖然好用,但卻會造成Cell不被重覆使用的問題,造成大資料量時會吃光記憶體,所以正確的邏輯應該是,重覆使用相同的Cell,在prepareForReuse中把Cell的顯示畫面清空,再重新給他應該要有的畫面,而資料的正確對應關係,可以用Index.row與Data Array的index來對應!
//給UITableView裡的每個Cell一個唯一個Identifier
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];
//如果TableView裡還有Cell的話,把cell叫出來使用
MerchandiseCell *cell = (MerchandiseCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//如果叫不出來cell,就new一個新的cell
if (cell == nil)
{
cell = [[MerchandiseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//其中的MerchandiseCell就是我用來客製化的cell
//你可以新增一個屬於自己的TableViewCell,並在其中任意新增或修改View到你想到的排版,最後再像上面這code的MerchandiseCell的方式加入TableView即可。
//點選cell的事件,依舊可在TableView的delegate中didSelected觸發
//下面列出從客製化cell要取用自己寫的view的範例
-(void)setCustomViewWithMerchandiseInfo:(NSDictionary *)merchandiseInfo
{
MerchandiseView *merchandiseView = (MerchandiseView *)[self.contentView viewWithTag:_merchandiseViewTag];
BOOL _isMakeNew = NO;
if( !merchandiseView )
{
//merchandiseView = [MerchandiseView new];
merchandiseView = [[[NSBundle mainBundle] loadNibNamed:@"MerchandiseView" owner:self.contentView options:nil] objectAtIndex:0] ;
[merchandiseView setTag:_merchandiseViewTag];
//設定圓角
[merchandiseView.backGroundView.layer setCornerRadius:10];
[merchandiseView.backGroundView.layer setMasksToBounds:YES];
//設定圖片的url位址
if (![[merchandiseInfo objectForKey:@"pictures"]isEqual:[NSNull null]]) {
NSArray* pictures = [merchandiseInfo objectForKey:@"pictures"][0];
NSLog(@"picture:%@",pictures);
//Api有提供5個圖片尺寸,這裡取用第4個:320*320
NSURL *url = [NSURL URLWithString:[pictures[3] objectForKey:@"url"]];
UIImage *urlImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];
merchandiseView.pictureImageView.image = urlImage;
}
//設定商品名稱
merchandiseView.nameLabel.text = [NSString stringWithFormat:@"%@",[merchandiseInfo objectForKey:@"name"]];
_isMakeNew = YES;
}
[merchandiseView setUserInteractionEnabled:YES];
if( _isMakeNew )
{
[self.contentView addSubview:merchandiseView];
}
}
-(CGFloat)getCustomViewHeight
{
MerchandiseView *merchandiseView = (MerchandiseView *)[self.contentView viewWithTag:_merchandiseViewTag];
return merchandiseView.frame.size.height;
}
[super layoutSubviews];
self.imageView.frame = CGRectMake(2,2,40,40);
}
//給不同的identifier不同的nib
[self.tableView registerNib:[UINib nibWithNibName:@"YourCell" bundle:nil]; forCellReuseIdentifier:PhotoCellIdentifier];
//其中的MerchandiseCell就是我用來客製化的cell
//你可以新增一個屬於自己的TableViewCell,並在其中任意新增或修改View到你想到的排版,最後再像上面這code的MerchandiseCell的方式加入TableView即可。
//點選cell的事件,依舊可在TableView的delegate中didSelected觸發
//下面列出從客製化cell要取用自己寫的view的範例
-(void)setCustomViewWithMerchandiseInfo:(NSDictionary *)merchandiseInfo
{
MerchandiseView *merchandiseView = (MerchandiseView *)[self.contentView viewWithTag:_merchandiseViewTag];
BOOL _isMakeNew = NO;
if( !merchandiseView )
{
//merchandiseView = [MerchandiseView new];
merchandiseView = [[[NSBundle mainBundle] loadNibNamed:@"MerchandiseView" owner:self.contentView options:nil] objectAtIndex:0] ;
[merchandiseView setTag:_merchandiseViewTag];
//設定圓角
[merchandiseView.backGroundView.layer setCornerRadius:10];
[merchandiseView.backGroundView.layer setMasksToBounds:YES];
if (![[merchandiseInfo objectForKey:@"pictures"]isEqual:[NSNull null]]) {
NSArray* pictures = [merchandiseInfo objectForKey:@"pictures"][0];
NSLog(@"picture:%@",pictures);
//Api有提供5個圖片尺寸,這裡取用第4個:320*320
NSURL *url = [NSURL URLWithString:[pictures[3] objectForKey:@"url"]];
UIImage *urlImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];
merchandiseView.pictureImageView.image = urlImage;
}
//設定商品名稱
merchandiseView.nameLabel.text = [NSString stringWithFormat:@"%@",[merchandiseInfo objectForKey:@"name"]];
}
if( _isMakeNew )
{
[self.contentView addSubview:merchandiseView];
}
}
{
MerchandiseView *merchandiseView = (MerchandiseView *)[self.contentView viewWithTag:_merchandiseViewTag];
return merchandiseView.frame.size.height;
}
layoutSubviews:
1、init不會觸發layoutSubviews
2、addSubview會觸發layoutSubviews
3、設置view的Frame會觸發layoutSubviews,前提是frame的值有所變動
4、捲動UIScrollView會觸發layoutSubviews
5、旋轉Screen會觸發父UIView上的layoutSubviews事件
6、改變一個UIView大小的時候也會觸發父UIView上的layoutSubviews事件
- (void)layoutSubviews {2、addSubview會觸發layoutSubviews
3、設置view的Frame會觸發layoutSubviews,前提是frame的值有所變動
4、捲動UIScrollView會觸發layoutSubviews
5、旋轉Screen會觸發父UIView上的layoutSubviews事件
6、改變一個UIView大小的時候也會觸發父UIView上的layoutSubviews事件
[super layoutSubviews];
self.imageView.frame = CGRectMake(2,2,40,40);
//給不同的identifier不同的nib
[self.tableView registerNib:[UINib nibWithNibName:@"YourCell" bundle:nil]; forCellReuseIdentifier:PhotoCellIdentifier];
沒有留言:
張貼留言