問題描述:
當我們在iOS中實現(xiàn)帶圖片帶列表顯示時UITableViewCell中自帶了一個存放圖片的控件UIImageView,當我們獲取的圖片大小一致時,圖片能夠很整齊大顯示斧蜕,可是有些時候我們獲取的列表圖片的大小并不完全一致极阅,為了保證界面的美觀我們必須調(diào)整圖片大小或位置啤月,可是當我們在定義好的UITableViewCell對象中設(shè)置cell.imageView.bounds、cell.imageView.frame時發(fā)現(xiàn)無論設(shè)置什么值都無法改變圖片的大小和位置眨补。我們只能在繼承至UITableViewCell的類中進行進行重寫旗笔。
重定義一個uitableviewcell,它自動生成的代碼不需要動,只要在.m文件中再加上下面的代碼即可:
//重載layoutSubviews斩箫,對cell里面子控件frame進行設(shè)置
- (void)layoutSubviews {
[super layoutSubviews];
//更改圖片位置吏砂、尺寸
self.imageView.bounds =CGRectMake(0,0,44,44);
self.imageView.frame =CGRectMake(15,10,44,44);
self.imageView.contentMode =UIViewContentModeScaleAspectFill;
//更改標題frame
CGRect tmpFrame = self.textLabel.frame;
tmpFrame.origin.x = 44+15+10;
self.textLabel.frame = tmpFrame;
//更改詳細說明frame
tmpFrame = self.detailTextLabel.frame;
tmpFrame.origin.x = 44+15+10;
self.detailTextLabel.frame = tmpFrame;
//更改分隔線frame
[self setSeparatorInset:UIEdgeInsetsMake(15, 43, screenWith, 1)];
}
實例2:
@implementation MerchantSearchSelect_img_Cell
- (void)layoutSubviews{
[super layoutSubviews];
int addX = _node.nodeLevel*25; //根據(jù)節(jié)點所在的層次計算平移距離
//? ? CGRect imgFrame = _iconView.frame;
//? ? imgFrame.origin.x = 14 + addX;
//? ? _iconView.frame = imgFrame;
self.imageView.x = self.imageView.x + addX;
//? ? CGRect nameFrame = _contextLabel.frame;
//? ? nameFrame.origin.x = 62 + addX;
//? ? _contextLabel.frame = nameFrame;
self.textLabel.x = self.textLabel.x + addX;
}
創(chuàng)建cell后,別忘了加重繪:
[cell setNeedsLayout];//重繪