1. 對象創建;
1.1 TableView初始化
#pragma 懶加載
- (UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.cycleScrollView2.frame.size.height)];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
return _tableView;
}
1.2 復用cell
從 iOS 6 以后,我們在 UITableView 和 UICollectionView 中可以復用 Cell以及各個 Section 的 Header 和 Footer。
確保TableviewCell/Header/Footer使用了復用機制, 而不是每一次都創建;
@interface HomeVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
static NSString *cellId = @"Cell";
@implementation HomeVC
- (void)viewDidLoad {
[super viewDidLoad];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
//第一種注冊cell<nib文件類HomeTableViewCell>
[self.tableView registerNib:[UINib nibWithNibName:@"HomeTableViewCell" bundle:nil] forCellReuseIdentifier:cellId];
//第二種注冊Cell<純手工打造的HomeVC>
// [self.tableView registerClass:[HomeVC class]forCellReuseIdentifier:cellId];
}
#pragma 代理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//獲取重用池中的cell
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
//如果沒有取到,就初始化
if (!cell) {
cell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.cellNameLab.text = @"Name";
return cell;
}
1.2.1 以下為重用相關API
// 復用 Cell:
- [UITableView dequeueReusableCellWithIdentifier:];
- [UITableView registerNib:forCellReuseIdentifier:];
- [UITableView registerClass:forCellReuseIdentifier:];
- [UITableView dequeueReusableCellWithIdentifier:forIndexPath:];
// 復用 Section 的 Header/Footer:
- [UITableView registerNib:forHeaderFooterViewReuseIdentifier:];
- [UITableView registerClass:forHeaderFooterViewReuseIdentifier:];
- [UITableView dequeueReusableHeaderFooterViewWithIdentifier:];
2. TabelView 代理
2.1 避免快速滑動情況下開過多線程。
cell中的圖片開線程異步加載SDWebImage(異步操作)。但是線程開過多了會造成資源浪費,內存開銷過大。圖片過多時可以不要一滾動就走cellForRow方法,可以在scrollview的代理方法中做限制,當滾動開始減速的時候才加載顯示在當前屏幕上的cell(通過tableview的dragging和declearating兩個狀態也能判斷)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
//如果沒有取到,就初始化
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.textLabel.text = @"Name";
BOOL canLoad = !self.tableView.dragging && !_tableView.decelerating;
if (canLoad) {
//開始loaddata,異步加載圖片
NSLog(@"開始加載圖片");
}
return cell;
}
// 滾動停止時,觸發該函數
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//刷新tableview
// [self.tableView reloadData];
//在此刷新的是屏幕上顯示的cell的內容
NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
//獲取到的indexpath為屏幕上的cell的indexpath
[self.tableView reloadRowsAtIndexPaths:visiblePaths withRowAnimation:UITableViewRowAnimationRight];
}
3. 圖片圓角
3.1 layer.cornerRadius
imageView.layer.cornerRadius = imageView.frame.size.width/2.0;
// 隱藏邊界
imageView.layer.masksToBounds = YES;
3.2 頭像使用蒙版+貝塞爾曲線加圓角
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.icon.width / 2, self.icon.height / 2) radius:self.icon.width / 2 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
layer.path = path.CGPath;
self.icon.layer.mask = layer;
3.3 stackoverflow
UIImageView *iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:iconImage];
// Get your image somehow
UIImage *image = [UIImage imageNamed:@"image.jpg"];
// Begin a new image that will be the new image with the rounded corners
// (here with the size of an UIImageView)
UIGraphicsBeginImageContextWithOptions(iconImage.bounds.size, NO, [UIScreen mainScreen].scale);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:iconImage.bounds cornerRadius:10.0] addClip];
// Draw your image
[image drawInRect:iconImage.bounds];
// Get the image, here setting the UIImageView image
iconImage.image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
4. 異步加載圖片
第一種方法: SDWebImage的使用
[imageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
imageView.image = image;
}];
5 優化UITableViewCell高度計算
Tip
1. 使用不透明視圖, 如非必要, 可以設置opaque為yes;
2. 圖片的alpha盡量不設置透明度;
3. cellForRowAtIndexPath不要做耗時操作,
* 讀取文件/寫入文件;
* 盡量不要去添加和移除view;